不能作为函数使用,返回前的最后一行是错误的


#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
using namespace std;
int main(void)
{
int day, month;
string season;
cout << "Enter the month today: ";
cin >> month;
while (month < 0 || month > 12) {
cout << "Enter valid month between 1 to 12: ";
cin >> month;
}
cout << "Enter the day today: ";
cin >> day;
while (day < 0 || day > 30) {
cout << "Enter valid day between 1 to 30: ";
cin >> day;
}
string Months[{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
if (month >= 1 && month <= 3)
season = "Winter";
else if (month >= 4 && month <= 6)
season = "Spring";
else if (month >= 7 && month <= 9)
season = "Summer";
else if (month >= 10 && month <= 12)
season = "Fall";
if (month % 3 == 0 && day >= 21) {
if (season == "Winter")
season = "Spring";
else if (season == "Spring")
season = "Summer";
else if (season == "Summer")
season = "Fall";
else
season = "Winter";
}
cout << Months(month - 1) << day << " is " << season << " season! ";
return 0;
}

最后一行打印应为月、日、季。最后一行是错误的,它说'Months'不能用作函数。我该怎么解决这个问题呢?1月1日是冬季的例子就是结果。

在你的代码中有两个语法问题,一个是在Months数组的定义,它应该是:

string Months[] = {"January","February","March","April","May","June","July","August","September","October","November","December"};

另一个是使用Months作为数组,索引需要方括号:

cout << Months[month - 1] << ...

代码可能存在其他问题,但至少应用这些修复可以使其编译。祝你的程序顺利。

相关内容

最新更新