Problem with my simple Options Program

Hello, I'm new to the forums and c++ so sorry if I seem "special".

I'm trying to write a simple program that asks a question and then prints out what your answer was.

My problem is this: No matter what my answer is equal to it always gives me Dog even if the answer doesn't equal dog.


***If you have any other critiques or you see anything else wrong with my code please say something. I just started learning c++ and programming 3 days ago so i'm sure lots is wrong.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
int main()
{
    string answer1;
   cout<<"Welcome to the Menu Program!"<<endl;
   cout<<"\n\nPlease answer the following question\ntyping the Letter that the option corresponds too."<<endl;
   cout<<"\n\nWhat is your favorite animal?"<<endl;
   cout<<"A. Dog \t B.Cat \t C. Hampster"<<endl;
   getline(cin, answer1);

   while ((answer1 != "a") && (answer1 != "b") && (answer1 != "c") && (answer1 != "A") && (answer1 != "B") && (answer1 != "C"))
   {
       cout<<"Incorrect answer, please try again."<<endl;
       cout<<"Select either a,b or c."<<endl;
       cin>>answer1;
   }

    if (answer1 == "a","b","c","A","B","C")
    {
        cout<<"\n\nThank you for you selection."<<endl;
    }
    if (answer1 == "a","A")
    {
       cout<<"Your favorite animal is a dog."<<endl;
    }

    else if (answer1 == "b","B")
    {
        cout<<"Your favorite animal is a Cat."<<endl;
    }

    else if (answer1 == "c" || "C")
    {
        cout<<"Your favorite animal is a Hampster."<<endl;
    }
    cout<<"\n\n"<<answer1;

}
There are a couple of issues here:

Instead of using a string to store an answer, just use a char. It will be simpler when comparing everything.

When you do compare chars, use a single apostrophe instead of quotation marks.

there is a function which translates uppercase to lower case, and it is called

int tolower(int)
http://cplusplus.com/reference/cctype/tolower/

When the user responds, try using this to lower the case so you don't need to check for both conditions.

Once you use tolower, you won't need to check for both conditions anymore, but just so you know, when checking for multiple conditions you must explicitly write the condition for each check. so:
 
 if (answer1 == "a","A")

becomes
 
 if (answer1 == 'a' || answer1 == 'A')


You don't need to use getline here:
 
getline(cin, answer1);


just use cin:
 
cin>>answer1;


whats happening is that getline gets all of the chars in the line, including the newline character that gets inputted.
Last edited on
Topic archived. No new replies allowed.