Keep asking for user input.

Hey there. I'm designing a program that will ask the user for an ice cream flavor, a sauce flavor, and whether or not they want sprinkles. I am having two problems:
1. Since chocolate is both an option under sauce and icecream, if I select chocolate for a sauce, it changes the ice cream flavor instead. I can't figure out how to lock in the input then progress to the next one.

2. I can't seem to get the sprinkles option to ask the question. Also if you can see by my code, is this a good way to go about doing 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
using namespace std;

int main() 
{

	string icecream = "", sauce = "", sprinkles = "", answer;


do
{
	if (icecream == "")
	cout << "Do you want chocolate, vanilla or twist?" << endl;

	if (sauce == "")
	cout << "Do you want hot fudge, chocolate or strawberry sauce?" << endl;

	if (sprinkles == "")
	cout << "Do you want sprinkles (yes/no)? " << endl;
		getline(cin, answer);

	if ((answer == "chocolate") || (answer == "vanilla") || (answer == "twist"))
		icecream = answer;

	if ((answer == "hot fudge") || (answer == "chocolate") || (answer == "strawberry"))
		sauce = answer;

	if (answer == "no")
	{
		sprinkles = answer;
		sprinkles = "without";
	}
	else 
	{
		sprinkles = "and";
	}




} while ((icecream == "") || (sauce == "") || (sprinkles == ""));

	cout << "You ordered " << icecream << " ice cream with " << sauce << " sauce " << sprinkles << " sprinkles. " << endl;

}


		

Greatly appreciate any input!
The way your program is set up currently, it only asks for input once; so the user can only enter one of the answers to your questions. You should probably ask for input three times (once after each question) and set the field immediately rather than after all the questions.
I notice that when I change to the sprinkles option to this:
1
2
	if ((answer == "no") || (answer == "yes"))
	sprinkles = answer;

It lets me ask all three options and ask for input for each. However, if I try to put those in an if..else format, it won't. Could this be because I made sprinkles == ""?
In psuedocode, I would say you should adjust the program to look like so:
while loop
print flavor question
get flavor input
set flavor
print sauce question
get sauce input
set sauce
print sprinkles question
get sprinkles input
set sprinkles
end while loop
print results


Currently, your program works like so:
while loop
print flavor question
print sauce question
print sprinkles question
get input (which somehow is supposed to answer all the questions)
set flavor based on input
set sauce based on same input (why don't they get to input a sauce?)
set sprinkles based on same input (why don't they get to input a sprinkles?)
end while loop
print results

Which seems odd to me. It's like like having a form you are filling out a form with three entries but only one space to write a response.
I am thinking my instructor wants this a specific way. Here's the exact question:

Write a program that asks the user to order an ice cream treat. The user selects the type of ice cream, the sauce, and whether or not to add sprinkles. Use the screen shots below as a guide. Note that the user can answer the questions in any order and can change his/her mind. Also note that the "chocolate" may refer to either the ice cream or the sauce; assume the answer "chocolate" refers to sauce if the ice cream flavor has already been selected, otherwise assume it refers to the ice cream flavor.

This is why it's only having me ask one question at a time.
Last edited on
This is frustrating me :x
Figured it out, had to set up some restraints. For example:

1
2
3
if ((sprinkles == "") && (sauce == "") && (icecream !=""))
    if ((answer == "hot fudge") || (answer == "chocolate") || (answer == "strawberry"))
       sauce = answer;


It's working now :)
Topic archived. No new replies allowed.