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 7 Years Ago by soccergad: n/a

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)

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 article has been dead for over six months. Start a new discussion instead.