我在需要打印当天的部分遇到问题。我尝试制作第二个变量,但它不起作用。基本上我为他们的生日带了一个用户输入。然后我试着调用这个函数来确定出生日期(它确定代表一天的数字)。然后我试图将这个数字发送到接受数字的函数并用单词打印生日。我现在得到'int day2'重新定义的错误。
这是我的代码:
void determineDayOfBirth() {
int day;
int month;
int year;
char backslash;
char backslash2;
cout << "Enter your date of birth" << endl;
cout << "format: month / day / year -->" << endl;
cin >> month >> backslash >> day >> backslash2 >> year;
if (isValidDate(month, day, year)) {
int day2;
cout << "You were born on a: ";
int day2 = determineDay(month, day, year);
printDayOfBirth(day2);
cout << endl;
cout << "Have a great birthday!!!";
}
else {
cout << "Invalid date";
}
return;
}
答案 0 :(得分:1)
从第二个分配中删除int
,您无法在同一个块中定义两次变量。
要修复您的代码,请替换:
int day2;
cout << "You were born on a: ";
int day2 = determineDay(month, day, year);
使用:
cout << "You were born on a: ";
int day2 = determineDay(month, day, year);
答案 1 :(得分:1)
你已经放了两次“int day2”,这确实是一个重新定义。你只需要定义一次“day2”:
if (isValidDate(month, day, year)) {
int day2;
cout << "You were born on a: ";
day2 = determineDay(month, day, year); // REMOVE "int"
printDayOfBirth(day2);
cout << endl;
cout << "Have a great birthday!!!";
}
else {
cout << "Invalid date";
}
return;
答案 2 :(得分:0)
问题的原因是
int day2;
cout << "You were born on a: ";
int day2 = determineDay(month, day, year);
第二个是重新定义day2
。
从该行中删除int
关键字,它将成为一个简单的作业。
答案 3 :(得分:0)
您不能在同一范围内声明两个变量,因此day2在if块中声明两次。 你可以直接写:
//if(){
int day2 = determineDay(month, day, year);
//}