954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

string/char array not working as intended.. :D

Well, in this program I want it to take in a string or character array I really don't care which, for this instance I wouldn't know which is better. Afterwords, it compares those in if statements and sets a variable to a 1(female) or 2(male).

Anyways, here is what I have so far:

#include <iostream>
#include <string>

using namespace std;

void intro();
void questions();

char name[50];
int age;
int gender;
int orientation;

int main()
{
    questions();
    
    return 0;
}

void questions()
{
     
     string gendercheck;
     gender = 0;
     
     cout << "What is your first name?" << endl;
     cin >> name;
     
     cout << endl << endl << "Ah " << name << ", I think I am starting remember you now..." << endl
          << "How old are you again?..." << endl;
     cin >> age;
     
     cout << endl << endl << "Interesting... mmm." << endl;
     
     do
     {
         cout << "... Are you a male or a female?..." << endl;
         cin >> gendercheck;
         
         
         if(gendercheck != "male" || gendercheck != "female")
         {
                    cout << endl << "I'm sorry, but I do not recognise the statement \"" << gendercheck << "\"..." << endl;
         }
         else if(gendercheck == "male")
         {
                    gender = 2;
         }
         else if(gendercheck == "female")
         {
                    gender = 1;
         }
     }while(gender != 1 || gender != 2);
}


In the do while statement, no matter what I enter, its always saying the sorry message, even if i say male or female, so I assume im doing something stupid, thats causeing the entered male to be different than "male" in the comparison. I would really appreciate if this could be explained... :D Thanks in advance!

globberbob
Newbie Poster
10 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This should do it...your condition loops because it will only check the first if staement..i think

do
     {
         cout << "... Are you a male or a female?..." << endl;
         cin >> gendercheck;
         
         
        
         if(gendercheck == "male")
         {
                    gender = 2;
         }
         else if(gendercheck == "female")
         {
                    gender = 1;
         }
		 else
         {
                    cout << endl << "I'm sorry, but I do not recognise the statement \"" << gendercheck << "\"..." << endl;
         }
     }while(gender != 1 && gender !=2 );
}
Anyzen
Newbie Poster
24 posts since Jul 2010
Reputation Points: 10
Solved Threads: 2
 

Hand trace through the do/while loop with different conditions and you'll see that even if gender was equal to one, it would not be equal to 2 and vice versa, therefore your loop condition is true and the loop continues.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

Thanks a ton! :D It worked beautifully. I thought it wasn't even comparing, but in fact it was just comparing it stricter than I thought. Anyzen your change worked, and jonsca, it was your sentence that made me see the logic in it. Also I set while(gender < 1 || gender > 2); Fixed it all, thank you guys.

globberbob
Newbie Poster
10 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: