All possible "if" statements displayed when only one is desired

Hello Everyone,

This is my first post here. Really digging what I've seen so far in the CPlusPlus.com forums, in terms of the apparent helpfulness of the community. As my competencies with C++ and programming in general mature, I certainly intend to become one who nurtures. With that, onto my "issue"...

I purchased a copy of Bjarne Stroustrup's "Programming: Principles and Practice Using C++" and have been working on it in a self study manner. His style of writing and teaching definitely works for me, but needless to say I occasionally encounter something that is simply beyond my grasp.

As part of a drill assigned in the book, you're supposed to setup a program that prompts the user to select a specific character from multiple options, and then create a unique output based on their choice. What happens when I enter the code below into my IDE (Visual C++ 2008 Express on Windows XP SP2) is that all the options are displayed rather than just the particular one desired. As an additional restriction, it's required that you use the char variable and initialize it to "0", and use if statements.


cout<<"Please press 'h' for hot or 'm' for mild.\n";
char flava_choice='0';
cin>>flava_choice;
if (flava_choice ='h')
cout<<"Hot and Spicy\n";
if (flava_choice ='m')
cout<<"Mild and Rich\n";


Spent a couple hours experimenting and tweaking things. I feel I'm quite close, but some help would be MUCH appreciated!

Thanks in advance!!!
A common mistake.
if (flava_choice ='h')
That is assigning flava_choice 'h' and the same for all of it. Which means, if it can assign, then do that, which always works.

To check for equality, you use == instead.

P.S. I would change from if().. if() to if().. else if()..
Last edited on
Thanks for the tip bluezor, but I got the same result as before: all options, rather than the one desired, output to the screen.

I should note that an additional restriction is strictly using if statements. Else is not yet introduced and thus not allowed to be utilized.
Is it if (flava_choice == 'h')?
Whoa, I got totally off base. It runs perfectly now...thanks a ton!!!
Topic archived. No new replies allowed.