| | |
Ice Cream Program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2009
Posts: 3
Reputation:
Solved Threads: 0
I am having trouble with this program which was solved before, but I am still having an issue with the final code. Here is the assignment prompt:
Write a program that asks the user to order an ice cream treat. The user selects the type of ice cream, the type of sauce, and whether or not to add sprinkles. NOTE: "chocolate" may refer to either the ice cream or the sauce; assume it refers to the ice cream if an ice cream flavor has not yet been selected.
I have also attached pictures of what it should look like.
My problem is that when I run the program, and then I say "yes" or "no", trying to answer the sprinkles question, it ignores my response and still repeats all three questions, despite me having answered the sprinkles question. How can I alleviate this? I could really use the help...Thank you very much in advance for anyone that helps me.
Here is my code so far:
Write a program that asks the user to order an ice cream treat. The user selects the type of ice cream, the type of sauce, and whether or not to add sprinkles. NOTE: "chocolate" may refer to either the ice cream or the sauce; assume it refers to the ice cream if an ice cream flavor has not yet been selected.
I have also attached pictures of what it should look like.
My problem is that when I run the program, and then I say "yes" or "no", trying to answer the sprinkles question, it ignores my response and still repeats all three questions, despite me having answered the sprinkles question. How can I alleviate this? I could really use the help...Thank you very much in advance for anyone that helps me.
Here is my code so far:
C++ Syntax (Toggle Plain Text)
int main() { string flavor, topping, sprinkles, choice; int terminate = 0; while( sprinkles == "") { if (terminate++ > 30) { break; } if( flavor == "") { cout << "Do you want chocolate, vanilla or twist?" << endl; } if( topping == "") { cout << "Hot fudge, chocolate or strawberry sauce?" << endl; } if( sprinkles == "") { cout << "Do you want sprinkles (yes/no)?" << endl; } getline(cin, choice); if( sprinkles == "" && topping != "" && flavor != "") { if( choice == "yes" || choice =="no" ) { sprinkles = choice; } } if( sprinkles == "" && topping == "" && flavor != "") { if( choice == "hot fudge" || choice == "chocolate" || choice == "strawberry" ) { topping = choice; } } if( sprinkles == "" && topping == "" && flavor == "") { if( choice == "chocolate" || choice == "vanilla" || choice == "twist" ) { flavor = choice; } } } if (sprinkles == "yes") { cout << "You ordered " << flavor << " ice cream with " << topping << " sauce and sprinkles." << endl; } else { cout << "You ordered " << flavor << " ice cream with " << topping << " sauce without sprinkles." << endl; } }
•
•
Join Date: Jan 2008
Posts: 3,827
Reputation:
Solved Threads: 501
•
•
•
•
I am having trouble with this program which was solved before, but I am still having an issue with the final code. Here is the assignment prompt:
Write a program that asks the user to order an ice cream treat. The user selects the type of ice cream, the type of sauce, and whether or not to add sprinkles. NOTE: "chocolate" may refer to either the ice cream or the sauce; assume it refers to the ice cream if an ice cream flavor has not yet been selected.
I have also attached pictures of what it should look like.
My problem is that when I run the program, and then I say "yes" or "no", trying to answer the sprinkles question, it ignores my response and still repeats all three questions, despite me having answered the sprinkles question. How can I alleviate this? I could really use the help...Thank you very much in advance for anyone that helps me.
Here is my code so far:
C++ Syntax (Toggle Plain Text)
int main() { string flavor, topping, sprinkles, choice; int terminate = 0; while( sprinkles == "") { if (terminate++ > 30) { break; } if( flavor == "") { cout << "Do you want chocolate, vanilla or twist?" << endl; } if( topping == "") { cout << "Hot fudge, chocolate or strawberry sauce?" << endl; } if( sprinkles == "") { cout << "Do you want sprinkles (yes/no)?" << endl; } getline(cin, choice); if( sprinkles == "" && topping != "" && flavor != "") { if( choice == "yes" || choice =="no" ) { sprinkles = choice; } } if( sprinkles == "" && topping == "" && flavor != "") { if( choice == "hot fudge" || choice == "chocolate" || choice == "strawberry" ) { topping = choice; } } if( sprinkles == "" && topping == "" && flavor == "") { if( choice == "chocolate" || choice == "vanilla" || choice == "twist" ) { flavor = choice; } } } if (sprinkles == "yes") { cout << "You ordered " << flavor << " ice cream with " << topping << " sauce and sprinkles." << endl; } else { cout << "You ordered " << flavor << " ice cream with " << topping << " sauce without sprinkles." << endl; } }
You ask three questions right of the bat and don't stop for an answer:
C++ Syntax (Toggle Plain Text)
if( flavor == "") { cout << "Do you want chocolate, vanilla or twist?" << endl; } if( topping == "") { cout << "Hot fudge, chocolate or strawberry sauce?" << endl; } if( sprinkles == "") { cout << "Do you want sprinkles (yes/no)?" << endl; } getline(cin, choice);
Which of the three questions is the user answering here with the
getline line? I'd have the code where the user enters input be directly after the cout prompt. maybe you need to rearrange the program so that it does this
[edit]Same as ^^^ suggested
[/edit]
C++ Syntax (Toggle Plain Text)
Do you want chocolate, vanilla or twist? chocolate hot fudge, chocolate or strawberry sauce? hot fudge Do you want sprinkles (yes/no)? no You ordered chocolate ice cream with hot fudge sauce without sprinkles. Press any key to continue . . .
[edit]Same as ^^^ suggested
[/edit] Last edited by Ancient Dragon; Mar 16th, 2009 at 4:11 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Mar 2009
Posts: 3
Reputation:
Solved Threads: 0
The assignment states that you need to ask all three questions right off the bat, continually ask the questions until one of the correct responses is given, when a correct response is given the program checks to see what questions was answered, then takes that questions away and asks the remaining questions again. i know that's a bit confusing, it was to me at first, but here is an example (each >> is user input):
Do you want vanilla, chocolate, or twist?
Hot fudge, chocolate, or strawberry sauce?
Do you want sprinkles (yes/no)?
>>Hello
Do you want vanilla, chocolate, or twist?
Hot fudge, chocolate, or strawberry sauce?
Do you want sprinkles (yes/no)?
>>vanilla
Hot fudge, chocolate, or strawberry sauce?
Do you want sprinkles (yes/no)?
>>hello
Hot fudge, chocolate, or strawberry sauce?
Do you want sprinkles (yes/no)?
>>strawberry
Do you want sprinkles (yes/no)?
>>yes
You chose vanilla ice cream with strawberry sauce and. sprinkles.
As you can see, the program will eliminate questions already answered, and continue to ask until one of the responses is given.
My problem is that with my current code, the program will not stop asking the sprinkles question even if it already answered and only if it is the last questions answered, so it looks like this:
Do you want vanilla, chocolate, or twist?
Hot fudge, chocolate, or strawberry sauce?
Do you want sprinkles (yes/no)?
>>yes
Do you want vanilla, chocolate, or twist?
Hot fudge, chocolate, or strawberry sauce?
Do you want sprinkles (yes/no)?
>>yes
Do you want vanilla, chocolate, or twist?
Hot fudge, chocolate, or strawberry sauce?
Do you want sprinkles (yes/no)?
etc......
What makes this assignment even more boggling is that I can't simply have the questions asked up front, because there are TWO "chocolate" responses, one for ice cream and one for the sauce. Since the program HAS to ask all the questions up front, I needed to figure out a way to tell the program that the first "chocolate" input by the user is for the ice cream ONLY and not for the sauce. When the user inputs "chocolate" again, it is for the sauce.
Here is a link to the same assignment that was asked before on this forum, but did not answer my questions despite it being checked as "solved":
http://www.daniweb.com/forums/thread150790.html
Do you want vanilla, chocolate, or twist?
Hot fudge, chocolate, or strawberry sauce?
Do you want sprinkles (yes/no)?
>>Hello
Do you want vanilla, chocolate, or twist?
Hot fudge, chocolate, or strawberry sauce?
Do you want sprinkles (yes/no)?
>>vanilla
Hot fudge, chocolate, or strawberry sauce?
Do you want sprinkles (yes/no)?
>>hello
Hot fudge, chocolate, or strawberry sauce?
Do you want sprinkles (yes/no)?
>>strawberry
Do you want sprinkles (yes/no)?
>>yes
You chose vanilla ice cream with strawberry sauce and. sprinkles.
As you can see, the program will eliminate questions already answered, and continue to ask until one of the responses is given.
My problem is that with my current code, the program will not stop asking the sprinkles question even if it already answered and only if it is the last questions answered, so it looks like this:
Do you want vanilla, chocolate, or twist?
Hot fudge, chocolate, or strawberry sauce?
Do you want sprinkles (yes/no)?
>>yes
Do you want vanilla, chocolate, or twist?
Hot fudge, chocolate, or strawberry sauce?
Do you want sprinkles (yes/no)?
>>yes
Do you want vanilla, chocolate, or twist?
Hot fudge, chocolate, or strawberry sauce?
Do you want sprinkles (yes/no)?
etc......
What makes this assignment even more boggling is that I can't simply have the questions asked up front, because there are TWO "chocolate" responses, one for ice cream and one for the sauce. Since the program HAS to ask all the questions up front, I needed to figure out a way to tell the program that the first "chocolate" input by the user is for the ice cream ONLY and not for the sauce. When the user inputs "chocolate" again, it is for the sauce.
Here is a link to the same assignment that was asked before on this forum, but did not answer my questions despite it being checked as "solved":
http://www.daniweb.com/forums/thread150790.html
•
•
Join Date: Jan 2008
Posts: 3,827
Reputation:
Solved Threads: 501
if( sprinkles == "" && topping != "" && flavor != "")
{
if( choice == "yes" || choice =="no" )
{
sprinkles = choice;
}
}Look at your if statement. Is this what you want? Will it pass the if test if all three variables are empty strings?
•
•
Join Date: Oct 2008
Posts: 40
Reputation:
Solved Threads: 6
•
•
•
•
I am having trouble with this program which was solved before, but I am still having an issue with the final code. Here is the assignment prompt:
Write a program that asks the user to order an ice cream treat. The user selects the type of ice cream, the type of sauce, and whether or not to add sprinkles. NOTE: "chocolate" may refer to either the ice cream or the sauce; assume it refers to the ice cream if an ice cream flavor has not yet been selected.
I have also attached pictures of what it should look like.
My problem is that when I run the program, and then I say "yes" or "no", trying to answer the sprinkles question, it ignores my response and still repeats all three questions, despite me having answered the sprinkles question. How can I alleviate this? I could really use the help...Thank you very much in advance for anyone that helps me.
Here is my code so far:
C++ Syntax (Toggle Plain Text)
int main() { string flavor, topping, sprinkles, choice; int terminate = 0; while( sprinkles == "") { if (terminate++ > 30) { break; } if( flavor == "") { cout << "Do you want chocolate, vanilla or twist?" << endl; } if( topping == "") { cout << "Hot fudge, chocolate or strawberry sauce?" << endl; } if( sprinkles == "") { cout << "Do you want sprinkles (yes/no)?" << endl; } getline(cin, choice); if( sprinkles == "" && topping != "" && flavor != "") { if( choice == "yes" || choice =="no" ) { sprinkles = choice; } } if( sprinkles == "" && topping == "" && flavor != "") { if( choice == "hot fudge" || choice == "chocolate" || choice == "strawberry" ) { topping = choice; } } if( sprinkles == "" && topping == "" && flavor == "") { if( choice == "chocolate" || choice == "vanilla" || choice == "twist" ) { flavor = choice; } } } if (sprinkles == "yes") { cout << "You ordered " << flavor << " ice cream with " << topping << " sauce and sprinkles." << endl; } else { cout << "You ordered " << flavor << " ice cream with " << topping << " sauce without sprinkles." << endl; } }
C++ Syntax (Toggle Plain Text)
if( flavor == "") { // do-while loops always go through the code once do{ cout << "Do you want chocolate, vanilla or twist?" << endl; // actually don't really need the 'choice' variable, can use the 'flavor' string directly getline(cin, choice); } // no valid response, iterate through again while (choice != "chocolate" && choice != "vanilla" && choice != "twist"); flavor = choice; }
and do this for each question.
Thanks...
Sean
![]() |
Other Threads in the C++ Forum
- Previous Thread: Deleting and Locking/Unlocking An Array
- Next Thread: reading from a file
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






