Thread: another do while question

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    30

    another do while question

    i am working on this program for my class, and i almost have this figured out. the problem is that since chocolate can be referred to as 2 different answers, that is where my program is going wacky. once i type in chocolate the first time, the first question is supposed to go away and ask me the second one. once i answer that, the second question is supposed to go away and ask me the final question. the program is working for every thing else (strawberry, hot fudge) except chocolate. i can type in chocolate the first time, the 1st question goes away, but once i type it in again, the second and third question are asked again and again and again (until the counter reaches 100).
    if you need me to explain this better let me know.
    here is the assignment description:
    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. Use the screen shots
    below as a guide. 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.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string icecream = "", answer, sauce = "", sprinkles = "";
    
    	int kwb = 0;
    	do
    	{
    		if (kwb++ > 100)
    			break;
    		if (icecream == "")
    			cout << "Do you want chocolate, vanilla or twist? " << endl;
    		if (sauce == "")
    			cout << "Hot fudge, chocolate or strawberry sauce? " << endl;
    		if (sprinkles == "")
    			cout << "Do you want sprinkles (yes/no)? " << endl;
    		getline (cin, answer);
    		if ((answer == "chocolate")&&(sauce == ""))
    		{
    			icecream = "chocolate";
    			
    				
    		}
    
    		else
    		{
    			if ((answer == "vanilla")||(answer == "twist"))
    				icecream = answer;
    			if ((answer == "hot fudge")||(answer == "strawberry"))
    				sauce = answer;
    			if ((answer == "yes")||(answer == "no"))
    				sprinkles = answer;
    		}
    		
    		
    
    
    	} while ((icecream == "")||(sauce == "")||(sprinkles == ""));
    
    	if (sprinkles == "yes")
    		cout << "You ordered " << icecream << " ice cream with " << sauce << " sauce and sprinkles." << endl;
    	if (sprinkles == "no")
    		cout << "You ordered " << icecream << " ice cream with " << sauce << " sauce without sprinkles." << endl;
    }
    thanks for the help guys.
    Last edited by kbpsu; 03-23-2009 at 11:50 AM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    for starters, use else if instead of just using if (icecream == ""), if (sauce == "") and if (sprinkles == "").

    edit
    When I look closer to your source I see that there are more places where you should be using "else if" rather then just "if". You should readup about the difference between "else if" and "if", and also what an if-statement really is.

    dirty example of using "else if"
    Code:
    if (x > n) /* will always be evaluated */
      std::cout << "x > n" std::endl;
    
    if (x==y) /* will always be evaluated */
      std::cout << "x==y" << std::endl;
    
    else if (x>y) /* will be evaluated if x != y */
      std::cout << "x > y" << std::endl; 
    
    else /* will be evaluated if x != y and x < y */
      std::cout << "x < y" << std::endl;
    Last edited by edoceo; 03-23-2009 at 12:00 PM.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    thanks edoceo for the help. i actually just figured it out right now. in case anyone was wondering, here is what it should look like (or at least what works for me):
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string icecream = "", answer, sauce = "", sprinkles = "";
    
    	int kwb = 0;
    	do
    	{
    		if (kwb++ > 100)
    			break;
    		if (icecream == "")
    			cout << "Do you want chocolate, vanilla or twist? " << endl;
    		if (sauce == "")
    			cout << "Hot fudge, chocolate or strawberry sauce? " << endl;
    		if (sprinkles == "")
    			cout << "Do you want sprinkles (yes/no)? " << endl;
    		getline (cin, answer);
    		if (answer == "chocolate")
    		{
    			if (icecream == "")
    				icecream = "chocolate";
    			else if (sauce == "")
    				sauce = "chocolate";
    		}
    
    		else
    		{
    			if ((answer == "vanilla")||(answer == "twist"))
    				icecream = answer;
    			if ((answer == "hot fudge")||(answer == "strawberry"))
    				sauce = answer;
    			if ((answer == "yes")||(answer == "no"))
    				sprinkles = answer;
    		}
    		
    		
    
    
    	} while ((icecream == "")||(sauce == "")||(sprinkles == ""));
    
    	if (sprinkles == "yes")
    		cout << "You ordered " << icecream << " ice cream with " << sauce << " sauce and sprinkles." << endl;
    	if (sprinkles == "no")
    		cout << "You ordered " << icecream << " ice cream with " << sauce << " sauce without sprinkles." << endl;
    }

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    I'm guessing you having been copy+pasting a bit wrong since the if-statements on line #14, #16 and #18 are still "wrong"*

    You should also go through the logic of your program to find some other way of doing what you want since the path you have chosen now is somewhat messy. Maybe there is a better way to accomplish what you want?

    * from the sense that the program is printing more then one question at once

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM