Condition Returning Both If and Else.

This is a discussion on Condition Returning Both If and Else. within the C++ Programming forums, part of the General Programming Boards category; The following is a snippet from an application program. It ran fine through a few tests but when I used ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    17

    Condition Returning Both If and Else.

    The following is a snippet from an application program. It ran fine through a few tests but when I used a 19 year old male (which should have satisfied the very first condition and been done with it) I additionally have the else statement output. Essentially, this input ends up satisfying the if and returning the proper output, but also the else returning that output as well. Silly program.

    Code:
    int age, pushups;
           string name, military;
           char gender;
           cout << "What is your full name? "; getline (cin, name);
           cout << "How old are you? "; cin >> age;
           cout << "Were you ever in the military (yes/no)? "; cin >> military;
           cout << "How many pushups can you do in a row? "; cin >> pushups;
           cout << "Are (m)ale or (f)emale? "; cin >> gender;
           if ((age >= 18 && age <= 30 && gender == 'm')||(age >= 18 && age <= 35 && gender == 'm' && (military == "yes" || pushups >= 50)))
           {
               cout << "Yes, " << name << ", you may apply." << endl;
           }
           if ((age >= 18 && age <= 32 && gender == 'f')||(age >= 18 && age <= 40 && gender == 'f' && (military == "yes" || pushups >= 30)))
           {
               cout << "Yes, " << name << ", you may apply." << endl;
           }
           else cout << "Sorry, " << name << ", you are not eligible." << endl;
    Don't ask what this application is for by the way...it's super secret!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    if

    else if

    else
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    17
    Done and works, thanks, we had not learned to combine else and if like that yet in class. The super secret government program of heroes thanks you....but I have said too much.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21