8 Replies - 14162 Views - Last Post: 28 February 2010 - 10:33 PM Rate Topic: -----

#1 czuker36  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 25-February 10

Cafe Menu Program (if-else and while loop issues)

Posted 28 February 2010 - 08:06 PM

Here is what I am supposed to do:
You are to write a looping menu program. The program will display a menu and allow the user to make a selection for purchase. Once a selection has been made, prompt the user for the quantity needed for the selection. The total price for the quantity needed is then added to the total amount that is to be paid upon check out. The program will continue to loop until the user enters a selection to indicate that the user is done shopping. When the user has finished shopping, display to the user the amount that is to be paid before and after tax. Add 8.3% sales tax to the final amount. Please display an appropriate message at the end of the program. Your program should also have a short welcome message that is not part of the menu.

The menu will consist of the following:
0. Check Out
1. Coffee $12.95
2. Tea $9.95
3. Sugar $1.50
4. Salt $2.25
5. Milk $3
6. Display the total amount of dollars owed

I am new to programming and I'm having trouble getting the if - else statements right along with the while loop. Here is what I have so far... I hope it isn't too jumbled and screwed up to be saved.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	int choice, quantity1, quantity2, quantity3, quantity4, quantity5;
	double a = 12.95, b = 9.95, c = 1.50, d = 2.25, e = 3.00;
	char letter;
	
	cout << "Welcome to our store!" << endl << endl;
	cout << "Please make a selection" << endl << endl;
	cout << "Enter 1 for Coffee - $" << a << endl;
	cout << "Enter 2 for Tea - $" << b << endl;
	cout << "Enter 3 for Sugar - $" << c << endl;
	cout << "Enter 4 for Salt - $" << d << endl;
	cout << "Enter 5 for Milk - $" << e << endl;
	cout << "Enter 6 to see what you owe" << endl;
	cout << "Enter 0 when you are done" << endl;

	cout << "Choice: ";
	cin >> choice;
	cout << endl;

	if (choice == 1)
	{
		cout << "What is the quantity you would like?" << endl;
		cout << "Quantity: ";	
		cin >> quantity1;
		cout << endl;
	
		if (choice == 2)
		{
			cout << "What is the quantity you would like?" << endl;
			cout << "Quantity: ";	
			cin >> quantity2;
			cout << endl;

			if (choice == 3)
			{
				cout << "What is the quantity you would like?" << endl;
				cout << "Quantity: ";	
				cin >> quantity3;
				cout << endl;

				if (choice == 4)
				{
					cout << "What is the quantity you would like?" << endl;
					cout << "Quantity: ";	
					cin >> quantity4;
					cout << endl;

					if (choice == 5)
					{	
						cout << "What is the quantity you would like?" << endl;
						cout << "Quantity: ";	
						cin >> quantity5;
						cout << endl;

						if (choice == 6)
						{
							cout << "Your total amount is: " << endl;

							if (choice == 0)
							{
								cout << "Proceeding to check-out." << endl << endl;
								cout << "Your total is: " << endl;
								cout << "Your total after tax is: " << endl;
								cout << "Thank you. Have a nice day." << endl;
							}
						}
					}
				}
			}
		}	
	}

	else if (0 > choice > 6)
			{
					cout << "You have entered a choice that is not on the menu." << endl;
					cout << "Please try again." << endl << endl;

					cin >> choice;
			}

		while (0 > choice > 6)
			{
				cout << "Please make a selection" << endl << endl;
				cout << "Enter 1 for Coffee - $" << a << endl;
				cout << "Enter 2 for Tea - $" << b << endl;
				cout << "Enter 3 for Sugar - $" << c << endl;
				cout << "Enter 4 for Salt - $" << d << endl;
				cout << "Enter 5 for Milk - $" << e << endl;
				cout << "Enter 6 to see what you owe" << endl;
				cout << "Enter 0 when you are done" << endl;
				break;

			}

}


Even if you can just help me with one little thing, I would appreciate it very much.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Cafe Menu Program (if-else and while loop issues)

#2 Astraeus  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 205
  • Joined: 17-November 09

Re: Cafe Menu Program (if-else and while loop issues)

Posted 28 February 2010 - 08:11 PM

Pardon my asking but what do you mean by "You can't get the if statements working with the while loops"? >.>

First off, instead of using:
else if (0 > choice > 6)


use:

else if ((choice < 0) && (choice > 6))


Also, perhaps, if you can (or know them), you want to consider a switch statement. A switch will make this more organized, and is easier to keep track of.
switch (choice) {
case 1:
    cout << "What is the quantity you would like?" << endl;
                cout << "Quantity: ";   
                cin >> quantity1;
                cout << endl;
                break;
case 2:
    cout << "What is the quantity you would like?" << endl;
                        cout << "Quantity: ";   
                        cin >> quantity2;
                        cout << endl;
                        break;
//et cetera for each number..

default:
   cout << "Your choice is invalid\n";
}


Or something along those lines, not that per se :P

This post has been edited by Astraeus: 28 February 2010 - 08:20 PM

Was This Post Helpful? 1
  • +
  • -

#3 czuker36  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 25-February 10

Re: Cafe Menu Program (if-else and while loop issues)

Posted 28 February 2010 - 08:17 PM

View PostAstraeus, on 28 February 2010 - 07:11 PM, said:

Pardon my asking but what do you mean by "You can't get the if statements working with the while loops"? >.>


oops I'm sorry about that. The only if statement that works is the first one (choice == 1). When you enter 1 for choice it then proceeds to ask for quantity like I want it too. But, if you enter any of the other choices 2-6 or 0, it doesn't say what I want it to say, it just immediately shows the menu again (while loop). I'm just really lost with this program. It's pretty rough, and I began rewriting it. I just want to know what I did wrong in this one.
Was This Post Helpful? 0
  • +
  • -

#4 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Cafe Menu Program (if-else and while loop issues)

Posted 28 February 2010 - 08:20 PM

This is not the way to do multiple conditions
if (0 > choice > 6) 


You need to use an &&(AND) or ||(OR) such as
if (0 > choice && choice > 6) 


Do the same for your while loop
 while (0 > choice > 6)
 {
      cout << "Please make a selection" << endl << endl;
      cout << "Enter 1 for Coffee - $" << a << endl;
      cout << "Enter 2 for Tea - $" << b << endl;
      cout << "Enter 3 for Sugar - $" << c << endl;
      cout << "Enter 4 for Salt - $" << d << endl;
      cout << "Enter 5 for Milk - $" << e << endl;
      cout << "Enter 6 to see what you owe" << endl;
      cout << "Enter 0 when you are done" << endl;
      break;
}

There you should add the cin for the users input instead of the break;. Like
        cout << "Choice: ";
        cin >> choice;


This post has been edited by seeP+: 28 February 2010 - 08:23 PM

Was This Post Helpful? 1
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: Cafe Menu Program (if-else and while loop issues)

Posted 28 February 2010 - 08:26 PM

HOLY IF STATEMENT lol you are way over thinking this.

this should get you started, no in each case use a total+=item to sum your total and your should be pretty much good
#include <iostream>


using namespace std;

int main()
{
	double coffee = 12.95, tea = 9.95, sugar = 1.50, salt = 2.25, milk = 3.00;
	int choice;
	double total = 0.0;

	while(true)
	{
		cout << "Welcome to our store!" << endl << endl;
		cout << "Please make a selection" << endl << endl;
		cout << "Enter 1 for Coffee - $" << coffee << endl;
		cout << "Enter 2 for Tea - $" << tea << endl;
		cout << "Enter 3 for Sugar - $" << sugar << endl;
		cout << "Enter 4 for Salt - $" << salt << endl;
		cout << "Enter 5 for Milk - $" << milk << endl;
		cout << "Enter 6 to see what you owe" << endl;
		cout << "Enter 0 when you are done" << endl;

		cout << "Choice: ";
		cin >> choice;


		switch(choice)
		{
		case 0:
			cout<<"GOOD BYE"<<endl;
			exit(0);
		case 1:
			cout<<"YOUR SELECTED COFFEE"<<endl;
			break;
		case 2:
			cout<<"YOU SELECTED TEA"<<endl;
			break;
		case 3:
			cout<<"YOU SELECTED SUGAR"<<endl;
			break;
		case 4:
			cout<<"YOUR ENTERED SALT"<<endl;
			break;
		case 5:
			cout<<"YOU ENTERED MILK"<<endl;
			break;
		case 6:
			cout<<"YOU OWE THIS MUCH "<<endl;
			break;
		}
	}


}


This post has been edited by ImaSexy: 28 February 2010 - 08:28 PM

Was This Post Helpful? 1
  • +
  • -

#6 czuker36  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 25-February 10

Re: Cafe Menu Program (if-else and while loop issues)

Posted 28 February 2010 - 08:29 PM

OK yeah I figured out that I was doing the multiple conditions wrong, and I tried the OR thing earlier and it gave me a bunch of errors... Probably just a syntax issue I made. I'll try it again.

Also, that switch statement does seem much more simplified. I will definitely use it instead.

Thanks a bunch guys! You guys rock!! Hopefully I can be un-n00bed enough to pass this class :P
Was This Post Helpful? 0
  • +
  • -

#7 czuker36  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 25-February 10

Re: Cafe Menu Program (if-else and while loop issues)

Posted 28 February 2010 - 09:56 PM

Alright guys, this program is officially reborn! It's a hell of a lot better thanks to all of your help. There are only 2 more things I need to do to make it a grade A project (besides comment it :P)

1. I need it to make the user re-input their choice in case they input a number that's not on the menu (anything less than 0 and anything greater than 6) by saying "You have entered a choice not on the menu, please try again" Would I be able to add another case in the switch statement that can do this? Or do I need to add a while loop somewhere that will do this? Like if the user keeps inputing choices that aren't on the menu, it needs to keep prompting them for a correct input.

2. I need it to say the same thing, but for the case that the user gets witty and decides to enter a letter. Would it be best to add a declare a 'char' and make a while loop with the 'cin.fail' and 'cin.clear' functions in case they enter a letter?

Here is the newborn code :)

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
        double coffee = 12.95, tea = 9.95, sugar = 1.50, salt = 2.25, milk = 3.00;
        int choice, q1 = 0, q2 = 0, q3 = 0, q4 = 0, q5 = 0;
        double total, tax_total;

		cout << "Welcome to our store!" << endl << endl;

		cout.setf(ios::fixed);
		cout.setf(ios::showpoint);

        while(true)
        {
                cout << "Please make a selection" << endl << endl;
                cout << "Enter 1 for Coffee - $" << setprecision(2) << coffee << endl;
                cout << "Enter 2 for Tea - $" << tea << endl;
                cout << "Enter 3 for Sugar - $" << sugar << endl;
                cout << "Enter 4 for Salt - $" << salt << endl;
                cout << "Enter 5 for Milk - $" << milk << endl;
                cout << "Enter 6 to see what you owe" << endl;
                cout << "Enter 0 when you are done" << endl << endl;

                cout << "Choice: ";
                cin >> choice;
				cout << endl;


                switch(choice)
                {
                case 0:
total = (q1*coffee) + (q2*tea) + (q3*sugar) + (q4*salt) + (q5*milk);
						tax_total = total * 1.083;

                        cout << "Proceeding to check out..." << endl << endl;
						cout << "Your total is: $" << total << endl;
						cout << "Your total after tax is: $" << tax_total << endl;
						cout << "Thank you and have a nice day" << endl;
                        exit(0);
                case 1:
                        cout << "What is the quantity you would like?" << endl;
						cout << "Quantity: ";
						cin >> q1;
						cout << endl;
                        break;
                case 2:
                        cout << "What is the quantity you would like?" << endl;
						cout << "Quantity: ";
						cin >> q2;
						cout << endl;
                        break;
                case 3:
                        cout << "What is the quantity you would like?" << endl;
						cout << "Quantity: ";
						cin >> q3;
						cout << endl;
                        break;
                case 4:
                        cout << "What is the quantity you would like?" << endl;
						cout << "Quantity: ";
						cin >> q4;
						cout << endl;
                        break;
                case 5:
                        cout << "What is the quantity you would like?" << endl;
						cout << "Quantity: ";
						cin >> q5;
						cout << endl;
                        break;
                case 6:
						total = (q1*coffee) + (q2*tea) + (q3*sugar) + (q4*salt) + (q5*milk);
						
						cout << "Your total amount is: $" << total << endl;
                        break;
						
                }
        }

}


Thanks again for being such a great community!! You guys are my saviors, haha!

This post has been edited by czuker36: 28 February 2010 - 10:14 PM

Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: Cafe Menu Program (if-else and while loop issues)

Posted 28 February 2010 - 10:10 PM

Put a case called

default:
//put error message here


in your switch statment to handle the error meassage

This post has been edited by ImaSexy: 28 February 2010 - 10:11 PM

Was This Post Helpful? 1
  • +
  • -

#9 czuker36  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 25-February 10

Re: Cafe Menu Program (if-else and while loop issues)

Posted 28 February 2010 - 10:33 PM

Cool thanks, I've learned more from this thread than I normally do in class haha. Many thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1