gpt4 book ai didi

c++ - 为什么这告诉我 “gender”是在线上未声明的标识符

转载 作者:行者123 更新时间:2023-12-02 11:15:52 24 4
gpt4 key购买 nike

int main()
{
introduction();
int x;
cin >> x;
if (x == 1)
cout << endl << "1. Only state true information." << endl << "2. Do not copy this servey and distribute it AT ALL." << endl << "3. Do not falsely advertise this survey anywhere." << endl << endl;
cout << "(Press 1 to start survey)" << endl;
int y;
cin >> y;
if (y == 1)
int gender = askGender();
int job = askJobOrNot();
int sport = askFavSport();
int music = askFavMusicGenre();
int birth = askBirthPlace();
int colour = askFavColour();
cout << endl << "Thank you, you have successfully completed the survey! (: " << endl;
cout << "(Press 1 to show results, and press 2 to quit)" << endl;
int s;
cin >> s;
if (s == 1)
printResults(gender, job, sport, music, birth, colour);
if (s == 2)
quitProgram();
return 0;
}

当我编译这段代码时,它在第23行给了我一个错误,告诉我变量“gender”(我在函数“printResults”中作为参数放置)是一个未声明的标识符,即使我在11行之前明确声明了它也是如此。 (第12行)。为什么会这样呢?

最佳答案

更改此:

if (y == 1)
int gender = askGender();

对此:
int gender;
if (y == 1)
gender = askGender();

甚至更好的是:
int gender = 0; // default: 0
if (y == 1)
gender = askGender();

关于c++ - 为什么这告诉我 “gender”是在线上未声明的标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37907644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com