string variables and values problem

When the user select chocolate for icecreamFlavor, since it is among the same possible values for sauceFlavor, the program assigns it to both. If no icecreamFlavor has been picked, I want chocolate to be assigned to icecreamFlavor first. Right now even if user picks vanilla it will still display chocolate as icecreamFlavor.

Also I need to convert the "yes" or "no" answer for the sprinkles to "with sprinkles" and without sprinkles" in my final cout...

Why cant I assign it like this?
1
2
3
4
5
6
7
if (order=="yes")
{sprinkles=="with sprinkles";
sprinkles=order;}
else
if (order=="no")
{sprinkles=="without sprinkles";
sprinkles=order;}


Here is my code so far if you want to look at it

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
string icecreamFlavor="", sauceFlavor="", sprinkles="";
string order;
do
{
if (icecreamFlavor=="")
cout << "Do you want chocolate, vanilla or twist?" << endl;
if (sauceFlavor=="")
cout << "Hot fudge, chocolate or strawberry sauce?" << endl;
if (sprinkles=="")
cout << "Do you want sprinkles (yes/no)?" << endl;

getline (cin, order);

if ((order=="chocolate") || (order=="vanilla") || (order=="twist"))
icecreamFlavor=order;

if ((order=="hot fudge") || (order=="chocolate") || (order=="strawberry"))
sauceFlavor=order;

if ((order=="yes") || (order=="no"))
sprinkles=order;
}

while ((icecreamFlavor=="") || (sauceFlavor=="") || (sprinkles==""));

cout << "You ordered " << icecreamFlavor << " ice cream with " << sauceFlavor << " sauce and " << sprinkles << endl;
1
2
3
if (order=="yes")
{sprinkles=="with sprinkles";
sprinkles=order;}


This code wouldn't do what you might be thinking. Line 1 does what you expect, it checks to see if the user input "yes".

However line 2 is a comparison (==). You're comparing 'sprinkles' to "with sprinkles". But then you're not doing anything with the result of that comparison. Typcally, comparisons are put inside of if() statements, or the result is assigned to a bool variable. Here you're not doing either of those, so this line is effectively doing nothing. You probably meant to assign sprinkles (=... not ==).

Line 3 is assigning sprinkes so that it is the same as order, which doesn't make a whole lot of sense. You've already established that order=="yes" with the above if statement, therefore line 3 is setting sprinkles to "yes". I'm not sure whare you're trying to do here, but this line isn't necessary.

What you probably are meaning to do is this:

1
2
3
4
if(order == "yes")   // did the user input "yes"?
  spinkles = "with sprinkles";  // if so, set sprinkles string to "with sprinkles"
if(order == "no")    // did they input "no"?
  sprinkles = "without sprinkles";  // then set sprinkles to "without sprinkles" 


note the difference between == (comparison) and = (assignment).
Thanks for the reply Disch and for pointing me to understand the difference between == and =
Program works fine now, except I need to figure out how to tell it that when the user input chocolate the first time it does not mean it refers to the second variable sauceFlavor too just because the two variable share that same value...
any hint?
You should get the input for every request or get a single line and break it in three parts
Ok so I should not use getline (cin, order) ? but instead cin every choice one at the time?
I've tried this
getline (cin, icecreamFlavor);
getline (cin, sauceFlavor);
getline (cin, sprinkles);
but my do while was not looping anymore...
Program is working fine will all the other options...except chocolate
It makes sense because it automatically assigns chocolate to both icecreamFlavor and sauceFlavor...

here is my updated code...

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
# include <iostream>
# include <string>
using namespace std;
int main()
{
string icecreamFlavor="", sauceFlavor="", sprinkles="";
string order;

do
{
if (icecreamFlavor=="")
cout << "Do you want chocolate, vanilla or twist?" << endl;
if (sauceFlavor=="")
cout << "Hot fudge, chocolate or strawberry sauce?" << endl;
if (sprinkles=="")
cout << "Do you want sprinkles (yes/no)?" << endl;

getline (cin, order);

if ((order=="chocolate") || (order=="vanilla") || (order=="twist"))
icecreamFlavor=order;

if ((order=="hot fudge") || (order=="chocolate") || (order=="strawberry"))
sauceFlavor=order;

if(order == "yes")
sprinkles = "and sprinkles";

if(order == "no")
sprinkles = "without sprinkles";
}

while ((icecreamFlavor=="") || (sauceFlavor=="") || (sprinkles==""));

cout << "You ordered " << icecreamFlavor << " ice cream with " << sauceFlavor << " sauce " << sprinkles << endl;

}
You could add "else " (the keyword "else" and a space) to the beginning of lines 13, 15, 23, 26, and 29 so that it only asks one question at a time... That's still kind of nuts, though. It doesn't help the two chocolates...

Why not break it up into three simple functions (or parts, as Bazzy suggested): getflavor, getsauce, and getsprinkles? Then the two chocolates will work.
Last edited on
Hi Seymore
thanks
I will try again and post my progresses...
Can you tell me why when I try to split in three my do...while loop doesn't work anymore?
Also I tried to add the else keyword as suggested but then the program asks one question at the time...I need it to ask all the three questions in the beginning...only when the user has answered to all the three questions he gets the results of the order...otherwise it should keep looping with the remaining unanswered questions...

here is my new code if you want to take a look

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
string icecreamFlavor="", sauceFlavor="", sprinkles="";
string icecreamOrder="", sauceOrder="", sprinklesOrder="";
do
{
if (icecreamFlavor=="")
cout << "Do you want chocolate, vanilla or twist?" << endl;
if (sauceFlavor=="")
cout << "Hot fudge, chocolate or strawberry sauce?" << endl;
if (sprinkles=="")
cout << "Do you want sprinkles (yes/no)?" << endl;

getline (cin, icecreamFlavor);
getline (cin, sauceFlavor);
getline (cin, sprinkles);

if ((icecreamFlavor=="chocolate") || (icecreamFlavor=="vanilla") || (icecreamFlavor=="twist"))
icecreamFlavor=icecreamOrder;

if ((sauceFlavor=="hot fudge") || (sauceFlavor=="chocolate") || (sauceFlavor=="strawberry"))
sauceFlavor=sauceOrder;

if(sprinkles == "yes")
sprinklesOrder = "and sprinkles";

if(sprinkles == "no")
sprinklesOrder = "without sprinkles";
}

while ((icecreamOrder=="") && (sauceOrder=="") && (sprinklesOrder==""));
getline doesn't get empty lines so after the 1st time the loop is executed the three variables will have a value different from "".
I suggest you getting input after each question and loop there
eg:
1
2
3
4
5
do
{
    cout << "Do you want chocolate, vanilla or twist?" << endl;
    getline (cin, icecreamFlavor);
}while( icecreamFlavor!="chocolate" && icecreamFlavor!="vanilla" && icecreamFlavor!="twist" );

Last edited on
getline doesn't get empty lines
?????

Yes sorry, that was >>
Hi Bazzy thank you for the hint...
I changed my code and the program is working but I need the program to ask all the three question together until the user input all the answers...

example:

question about icecream flavor
question about sauce
question about sprinkles

if user input sauce then the program show only two questions:

question about icecream flavor
question about sprinkles

and then if user input sauce then the program will ask the last question:

sprinkes yes or no?

I believe I should accomplish this using the while condition and that is why before I used
while( icrecreamFlavor == "" && sauceFlavor=="" && sprinkles=="" );
but this way I will still have the problem with the chocolate....

I will keep working on my code
if you have more hints please for them here
thank you for all the help guys

Sorry forgot to include my new code:
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
string icecreamFlavor="", sauceFlavor="", sprinkles="";
string icecreamOrder="", sauceOrder="", sprinklesOrder="";

do
{
cout << "Do you want chocolate, vanilla or twist?" << endl;
getline (cin, icecreamFlavor);
}
while( icecreamFlavor!="chocolate" && icecreamFlavor!="vanilla" && icecreamFlavor!="twist" );
do
{
cout << "Hot fudge, chocolate or strawberry sauce?" << endl;
getline (cin, sauceFlavor);
}
while( sauceFlavor!="hot fudge" && sauceFlavor!="chocolate" && sauceFlavor!="strawberry" );
do
{
cout << "Do you want sprinkles (yes/no)?" << endl;
getline (cin, sprinkles);
}
while( sprinkles!="yes" && sprinkles!="no" );

if(sprinkles == "yes")
sprinklesOrder = "and sprinkles";
if(sprinkles == "no")
sprinklesOrder = "without sprinkles";

cout << "You ordered " << icecreamFlavor << " ice cream with " << sauceFlavor << " sauce " << sprinklesOrder << endl;
Last edited on
Topic archived. No new replies allowed.