4 Replies - 948 Views - Last Post: 14 July 2009 - 07:34 PM Rate Topic: -----

#1 mvb5142  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 29-June 09

Keep asking the user until some condition is met

Posted 14 July 2009 - 03:27 PM

HELP PLEASE!!!!
I want to write a program that asks the user to order an ice cream treat. The user selects the type
of ice cream (vanilla, chocolate or twist) the type of sauce (hot fudge, chocolate or strawberry) and
whether or not to add sprinkles. NOTE: "chocolate" may refer to either the ice cream or the sauce;
so I would like the program to assume it refers to the ice cream if an ice cream flavor has not yet been selected.

This is where I'm having my problem. Also I can't get a yes or no input to cout for whether or not to add sprinkles.

Here is what I have so far

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string flavor = "", sauce = "", sprinkles = "";
	string order;
	do
	{
		if (flavor == "")
			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, order);
		if ((order == "chocolate") || (order == "vanilla")
				|| (order == "twist"))
			flavor = order;
		if ((order == "hot fudge") || (order == "chocolate")
				|| (order == "strawberry"))
			sauce = order;
		if (order == "")
			sprinkles = order;
	} 

	while ((flavor == "") || (sauce == "") || (sprinkles == "yes"));
		cout << "You ordered " << flavor << " ice cream with " << sauce
			<< " sauce and sprinkles." << endl;

		((flavor == "") || (sauce == "") || (sprinkles == "no"));
			cout << "You ordered " << flavor << " ice cream with " << sauce
			<< " sauce without sprinkles." << endl;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Keep asking the user until some condition is met

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1073
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Keep asking the user until some condition is met

Posted 14 July 2009 - 04:16 PM

this could of been done simpler but i though id use a class since your learning

 
#include <iostream>
#include <string>
#include <limits>
using namespace std;

class iceCream
{
private:
	char s;
public:
	int type;
	int sauce;
	bool sprinkles;
	string chooseIceCream();
	string chooseSauce();
	bool chooseSprink()
	{
		cout<<"Sprinkles? (Y/N)";
		cin>>s;
		if(toupper(s)=='Y') return true;
		else return false;
	}
	void pause()
	{
		cout<<endl;
		cout<<"Press Enter To Quit...";
		cin.sync();
		cin.ignore(numeric_limits<streamsize> ::max(), '\n');
	}
}; iceCream x;
string iceCream::chooseIceCream()
{
	cout<<"Enter type of Ice Cream:"<<endl
		<<"[1] Vanilla"<<endl
		<<"[2] Chocolate"<<endl
		<<"[3] Strawberry"<<endl
		<<"Pick : ";
	cin>>x.type;
	switch(x.type)
	{
	case 1:  return "Vanilla";	break;
	case 2:  return "Chocolate";  break;
	case 3:  return "StrawBerry"; break;
	default:
		cout<<"Bad Choice"<<endl;
		x.chooseIceCream();
		break;
	}
	cout<<endl;
}
string iceCream::chooseSauce()
{
	cout<<"Enter type of Sauce:"<<endl
		<<"[1] Hot Fudge"<<endl
		<<"[2] Chocolate"<<endl
		<<"[3] Strawberry"<<endl
		<<"Pick : ";
	cin>>x.sauce;
	switch(x.sauce)
	{
	case 1: return "Hot Fudge";  break;
	case 2: return "Chocolate";  break;
	case 3: return "Strawberry"; break;
	default: cout<<"Bad Choice"<<endl; x.chooseSauce(); break;
	}
	cout<<endl;
}
int main()
{
	string icecream = x.chooseIceCream();
	string sauce = x.chooseSauce();
	bool choice = x.chooseSprink();
	cout<<"ICECREAM : "<<icecream<<endl<<"SAUCE : "<<sauce<<endl;
	if(choice) cout<<"TOPPING : "<<"Sprinkles"<<endl;
	else  cout<<"TOPPING : "<<"No Sprinkles"<<endl;

	x.pause();
	return 0;
}



Was This Post Helpful? 0
  • +
  • -

#3 Guest_c.user*


Reputation:

Re: Keep asking the user until some condition is met

Posted 14 July 2009 - 04:27 PM

mvb5142 said:

The user selects the type
of ice cream (vanilla, chocolate or twist) the type of sauce (hot fudge, chocolate or strawberry) and
whether or not to add sprinkles.

you should separate input, one for ice cream and second for sauce
these problems caused by mix
then you can do sauce condition inside an ice cream condition
now you have sauce like you can select only sauce without ice cream
Was This Post Helpful? 0

#4 mvb5142  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 29-June 09

Re: Keep asking the user until some condition is met

Posted 14 July 2009 - 05:08 PM

View PostImaSexy, on 14 Jul, 2009 - 03:16 PM, said:

this could of been done simpler but i though id use a class since your learning

 
#include <iostream>
#include <string>
#include <limits>
using namespace std;

class iceCream
{
private:
	char s;
public:
	int type;
	int sauce;
	bool sprinkles;
	string chooseIceCream();
	string chooseSauce();
	bool chooseSprink()
	{
		cout<<"Sprinkles? (Y/N)";
		cin>>s;
		if(toupper(s)=='Y') return true;
		else return false;
	}
	void pause()
	{
		cout<<endl;
		cout<<"Press Enter To Quit...";
		cin.sync();
		cin.ignore(numeric_limits<streamsize> ::max(), '\n');
	}
}; iceCream x;
string iceCream::chooseIceCream()
{
	cout<<"Enter type of Ice Cream:"<<endl
		<<"[1] Vanilla"<<endl
		<<"[2] Chocolate"<<endl
		<<"[3] Strawberry"<<endl
		<<"Pick : ";
	cin>>x.type;
	switch(x.type)
	{
	case 1:  return "Vanilla";	break;
	case 2:  return "Chocolate";  break;
	case 3:  return "StrawBerry"; break;
	default:
		cout<<"Bad Choice"<<endl;
		x.chooseIceCream();
		break;
	}
	cout<<endl;
}
string iceCream::chooseSauce()
{
	cout<<"Enter type of Sauce:"<<endl
		<<"[1] Hot Fudge"<<endl
		<<"[2] Chocolate"<<endl
		<<"[3] Strawberry"<<endl
		<<"Pick : ";
	cin>>x.sauce;
	switch(x.sauce)
	{
	case 1: return "Hot Fudge";  break;
	case 2: return "Chocolate";  break;
	case 3: return "Strawberry"; break;
	default: cout<<"Bad Choice"<<endl; x.chooseSauce(); break;
	}
	cout<<endl;
}
int main()
{
	string icecream = x.chooseIceCream();
	string sauce = x.chooseSauce();
	bool choice = x.chooseSprink();
	cout<<"ICECREAM : "<<icecream<<endl<<"SAUCE : "<<sauce<<endl;
	if(choice) cout<<"TOPPING : "<<"Sprinkles"<<endl;
	else  cout<<"TOPPING : "<<"No Sprinkles"<<endl;

	x.pause();
	return 0;
}






This was a little helpful but I need for my output to look like this

"Do you want chocolate, vanilla or twist?"
"Hot fudge, chocolate or strawberry sauce?"
"Do you want sprinkles (yes/no)?"

i.e. vanilla

"Hot fudge, chocolate or strawberry sauce?"
"Do you want sprinkles (yes/no)?"

i.e. chocolate

"Do you want sprinkles (yes/no)?"

i.e. no

"You ordered "vanilla ice cream with chocolate sauce without sprinkles."
"Press any key to continue..."
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1073
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Keep asking the user until some condition is met

Posted 14 July 2009 - 07:34 PM

you have all the information you need, you can simply just edit that with in the main now
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1