Ice Cream Program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 3
Reputation: star4ker is an unknown quantity at this point 
Solved Threads: 0
star4ker star4ker is offline Offline
Newbie Poster

Ice Cream Program

 
0
  #1
Mar 16th, 2009
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:

  1. int main()
  2. {
  3. string flavor, topping, sprinkles, choice;
  4. int terminate = 0;
  5.  
  6. while( sprinkles == "")
  7. {
  8. if (terminate++ > 30)
  9. {
  10. break;
  11. }
  12.  
  13. if( flavor == "")
  14. {
  15. cout << "Do you want chocolate, vanilla or twist?" << endl;
  16. }
  17.  
  18. if( topping == "")
  19. {
  20. cout << "Hot fudge, chocolate or strawberry sauce?" << endl;
  21. }
  22.  
  23. if( sprinkles == "")
  24. {
  25. cout << "Do you want sprinkles (yes/no)?" << endl;
  26. }
  27.  
  28. getline(cin, choice);
  29.  
  30. if( sprinkles == "" && topping != "" && flavor != "")
  31. {
  32. if( choice == "yes" || choice =="no" )
  33. {
  34. sprinkles = choice;
  35. }
  36. }
  37.  
  38. if( sprinkles == "" && topping == "" && flavor != "")
  39. {
  40. if( choice == "hot fudge" || choice == "chocolate" || choice == "strawberry" )
  41. {
  42. topping = choice;
  43. }
  44. }
  45.  
  46.  
  47. if( sprinkles == "" && topping == "" && flavor == "")
  48. {
  49. if( choice == "chocolate" || choice == "vanilla" || choice == "twist" )
  50. {
  51. flavor = choice;
  52. }
  53. }
  54.  
  55. }
  56.  
  57. if (sprinkles == "yes")
  58. {
  59. cout << "You ordered " << flavor << " ice cream with " << topping << " sauce and sprinkles." << endl;
  60. }
  61.  
  62. else
  63. {
  64. cout << "You ordered " << flavor << " ice cream with " << topping << " sauce without sprinkles." << endl;
  65. }
  66.  
  67. }
Attached Images
File Type: bmp screenshot 1.bmp (663.5 KB, 3 views)
File Type: bmp screenshot 2.bmp (664.8 KB, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,827
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Ice Cream Program

 
0
  #2
Mar 16th, 2009
Originally Posted by star4ker View Post
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:

  1. int main()
  2. {
  3. string flavor, topping, sprinkles, choice;
  4. int terminate = 0;
  5.  
  6. while( sprinkles == "")
  7. {
  8. if (terminate++ > 30)
  9. {
  10. break;
  11. }
  12.  
  13. if( flavor == "")
  14. {
  15. cout << "Do you want chocolate, vanilla or twist?" << endl;
  16. }
  17.  
  18. if( topping == "")
  19. {
  20. cout << "Hot fudge, chocolate or strawberry sauce?" << endl;
  21. }
  22.  
  23. if( sprinkles == "")
  24. {
  25. cout << "Do you want sprinkles (yes/no)?" << endl;
  26. }
  27.  
  28. getline(cin, choice);
  29.  
  30. if( sprinkles == "" && topping != "" && flavor != "")
  31. {
  32. if( choice == "yes" || choice =="no" )
  33. {
  34. sprinkles = choice;
  35. }
  36. }
  37.  
  38. if( sprinkles == "" && topping == "" && flavor != "")
  39. {
  40. if( choice == "hot fudge" || choice == "chocolate" || choice == "strawberry" )
  41. {
  42. topping = choice;
  43. }
  44. }
  45.  
  46.  
  47. if( sprinkles == "" && topping == "" && flavor == "")
  48. {
  49. if( choice == "chocolate" || choice == "vanilla" || choice == "twist" )
  50. {
  51. flavor = choice;
  52. }
  53. }
  54.  
  55. }
  56.  
  57. if (sprinkles == "yes")
  58. {
  59. cout << "You ordered " << flavor << " ice cream with " << topping << " sauce and sprinkles." << endl;
  60. }
  61.  
  62. else
  63. {
  64. cout << "You ordered " << flavor << " ice cream with " << topping << " sauce without sprinkles." << endl;
  65. }
  66.  
  67. }

You ask three questions right of the bat and don't stop for an answer:

  1. if( flavor == "")
  2. {
  3. cout << "Do you want chocolate, vanilla or twist?" << endl;
  4. }
  5.  
  6. if( topping == "")
  7. {
  8. cout << "Hot fudge, chocolate or strawberry sauce?" << endl;
  9. }
  10.  
  11. if( sprinkles == "")
  12. {
  13. cout << "Do you want sprinkles (yes/no)?" << endl;
  14. }
  15.  
  16. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Ice Cream Program

 
0
  #3
Mar 16th, 2009
maybe you need to rearrange the program so that it does this
  1. Do you want chocolate, vanilla or twist?
  2. chocolate
  3. hot fudge, chocolate or strawberry sauce?
  4. hot fudge
  5. Do you want sprinkles (yes/no)?
  6. no
  7. You ordered chocolate ice cream with hot fudge sauce without sprinkles.
  8. 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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 3
Reputation: star4ker is an unknown quantity at this point 
Solved Threads: 0
star4ker star4ker is offline Offline
Newbie Poster

Re: Ice Cream Program

 
0
  #4
Mar 16th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,827
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Ice Cream Program

 
0
  #5
Mar 16th, 2009
		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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 40
Reputation: seanhunt is an unknown quantity at this point 
Solved Threads: 6
seanhunt seanhunt is offline Offline
Light Poster

Re: Ice Cream Program

 
0
  #6
Mar 16th, 2009
Originally Posted by star4ker View Post
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:

  1. int main()
  2. {
  3. string flavor, topping, sprinkles, choice;
  4. int terminate = 0;
  5.  
  6. while( sprinkles == "")
  7. {
  8. if (terminate++ > 30)
  9. {
  10. break;
  11. }
  12.  
  13. if( flavor == "")
  14. {
  15. cout << "Do you want chocolate, vanilla or twist?" << endl;
  16. }
  17.  
  18. if( topping == "")
  19. {
  20. cout << "Hot fudge, chocolate or strawberry sauce?" << endl;
  21. }
  22.  
  23. if( sprinkles == "")
  24. {
  25. cout << "Do you want sprinkles (yes/no)?" << endl;
  26. }
  27.  
  28. getline(cin, choice);
  29.  
  30. if( sprinkles == "" && topping != "" && flavor != "")
  31. {
  32. if( choice == "yes" || choice =="no" )
  33. {
  34. sprinkles = choice;
  35. }
  36. }
  37.  
  38. if( sprinkles == "" && topping == "" && flavor != "")
  39. {
  40. if( choice == "hot fudge" || choice == "chocolate" || choice == "strawberry" )
  41. {
  42. topping = choice;
  43. }
  44. }
  45.  
  46.  
  47. if( sprinkles == "" && topping == "" && flavor == "")
  48. {
  49. if( choice == "chocolate" || choice == "vanilla" || choice == "twist" )
  50. {
  51. flavor = choice;
  52. }
  53. }
  54.  
  55. }
  56.  
  57. if (sprinkles == "yes")
  58. {
  59. cout << "You ordered " << flavor << " ice cream with " << topping << " sauce and sprinkles." << endl;
  60. }
  61.  
  62. else
  63. {
  64. cout << "You ordered " << flavor << " ice cream with " << topping << " sauce without sprinkles." << endl;
  65. }
  66.  
  67. }
The way the code is structured is somewhat strange, in that you do not allow user input after each question (which is why you get all 3 questions output at once). A more intuitive approach would be to get user input where you are asking the question, for example;

  1. if( flavor == "")
  2. {
  3. // do-while loops always go through the code once
  4. do{
  5. cout << "Do you want chocolate, vanilla or twist?" << endl;
  6. // actually don't really need the 'choice' variable, can use the 'flavor' string directly
  7. getline(cin, choice);
  8. }
  9. // no valid response, iterate through again
  10. while (choice != "chocolate" && choice != "vanilla" && choice != "twist");
  11. flavor = choice;
  12. }

and do this for each question.

Thanks...
Sean
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC