0

This code when compiled ...saids it contains infinite loop but i don't how to fix it? can someone tell me what im doing wrong?

~thank you

#include <iostream>
#include <string>
using namespace std;
// this program asks the user to order an ice cream
int main()
{
string flavor = "", sauce = "", sprinkles = "";
string order;

while (sprinkles == "")


{
	if (flavor == "")

		cout << "Do you want chocolate, vanilla or twist" << endl;
	 if (sauce == "")

		cout << "Hot fudge, chocolate or strawberrry sauce?" << endl;
	 if ( sprinkles== "")
		
		cout << "Do you want sprinkles <yes/no>?" << endl;
		
		getline(cin, order);
	
	if( sprinkles == "" && sauce != "" && flavor !="")
	if( order == "yes" || order =="no" )
	sprinkles = order;

	if(sprinkles == ""&& sauce == "" && flavor !="")
	if ((order == "fudge") || (order == "chocolate")
		|| (order == "strawberry"))
			sauce = order;

	if(sprinkles == ""&& sauce == "" && flavor =="")
	 if ((order == "chocolate") || (order == "vanilla")
			|| (order == "twist"))
				flavor = order;

}
	if (sprinkles == "yes")
		cout << "you ordered " << flavor << " ice cream with " << sauce << " sauce and sprinkles." << endl;

	else 
		cout << "You ordered " << flavor << " ice cream with " << sauce << " " << "sauce and without sprinkles." << endl;
return 0;
}

Edited by soccergad: n/a

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by AAAKsu
0

You initialize all your string containers to 'white space' and your while() loop condition tests for 'white space' which will always test true (because at no time are they ever changed)

0

This code when compiled ...saids it contains infinite loop but i don't how to fix it? can someone tell me what im doing wrong?

~thank you

#include <iostream>
#include <string>
using namespace std;
// this program asks the user to order an ice cream
int main()
{
string flavor = "", sauce = "", sprinkles = "";
string order;

while (sprinkles == "")


{
	if (flavor == "")

		cout << "Do you want chocolate, vanilla or twist" << endl;
	 if (sauce == "")

		cout << "Hot fudge, chocolate or strawberrry sauce?" << endl;
	 if ( sprinkles== "")
		
		cout << "Do you want sprinkles <yes/no>?" << endl;
		
		getline(cin, order);
	
	if( sprinkles == "" && sauce != "" && flavor !="")
	if( order == "yes" || order =="no" )
	sprinkles = order;

	if(sprinkles == ""&& sauce == "" && flavor !="")
	if ((order == "fudge") || (order == "chocolate")
		|| (order == "strawberry"))
			sauce = order;

	if(sprinkles == ""&& sauce == "" && flavor =="")
	 if ((order == "chocolate") || (order == "vanilla")
			|| (order == "twist"))
				flavor = order;

}
	if (sprinkles == "yes")
		cout << "you ordered " << flavor << " ice cream with " << sauce << " sauce and sprinkles." << endl;

	else 
		cout << "You ordered " << flavor << " ice cream with " << sauce << " " << "sauce and without sprinkles." << endl;
return 0;
}

The expression used as criteria in the while loop never gets changed causing an infinite loop.
Should look something like this:

while (sprinkles == "")
{	
   if (flavor == "")
   {
      cout << "Do you want chocolate, vanilla or twist" << endl;
      getline(cin, flavor);
   }
		
   if (sauce == "")
   {
      cout << "Hot fudge, chocolate or strawberrry sauce?" << endl;
      getline(cin, sauce);
   }
		
   if ( sprinkles== "")		
   {
      cout << "Do you want sprinkles <yes/no>?" << endl;
      getline(cin, sprinkles);
   }
}

Check if not empty is only made for sprinkles, do it for flavor and sauce as well?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.