943,866 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 975
  • C++ RSS
Mar 16th, 2009
0

Ice Cream Program

Expand 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:

C++ Syntax (Toggle Plain Text)
  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, 30 views)
File Type: bmp screenshot 2.bmp (664.8 KB, 9 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
star4ker is offline Offline
3 posts
since Mar 2009
Mar 16th, 2009
0

Re: Ice Cream Program

Click to Expand / Collapse  Quote originally posted by star4ker ...
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)
  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:

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Mar 16th, 2009
0

Re: Ice Cream Program

maybe you need to rearrange the program so that it does this
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Mar 16th, 2009
0

Re: Ice Cream Program

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
star4ker is offline Offline
3 posts
since Mar 2009
Mar 16th, 2009
0

Re: Ice Cream Program

		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?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Mar 16th, 2009
0

Re: Ice Cream Program

Click to Expand / Collapse  Quote originally posted by star4ker ...
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)
  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;

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 13
Solved Threads: 6
Light Poster
seanhunt is offline Offline
40 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Deleting and Locking/Unlocking An Array
Next Thread in C++ Forum Timeline: reading from a file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC