Why does this tell me that "gender" is an undeclared identifier on line

255 Views Asked by At
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;
}

When I compile this code, it gives me an error on line 23 telling me that the variable "gender" (that I put as an argument in the function "printResults") is an undeclared identifier, even though I clearly declared it 11 lines before that (line 12). Why is this so?

1

There are 1 best solutions below

0
lorro On

Change this:

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

To this:

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

Or even better this:

int gender = 0; // default: 0
if (y == 1)
    gender = askGender();