Thread: Need help using switch

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    33

    Need help using switch

    I have to write a program asking the user what type of sandwich they would like. I know how to do that part using switch but how do I get it to show the price only for that sandwich.

    This is what I have so far.

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    
    int main()
    {
    
    	int salami;
    	int cheese;
    	int turkey;
    	int order;
    	double food;
    
    	cout << "What would you like to order" << endl;
    	cout << "s - Salami" << endl;
    	cout << "c - Cheese" << endl;
    	cout << "t - Turkey" << endl;
    	cout << "n - No Order" << endl;
    	cin >> order;
    
    	switch (order)
    	{
    	case 's':
    	food = 5.59;
    	break;
    	case 'c':
    	food = 4.49;
    	break;
    	case 't':
    	food = 2.39;
    	break;
    	case 'n':
    	food = 5.59;
    	break;
    
    	
    	return 0;
    	}
    
    }
    Now how do I show the user their total? Say they pick turkey, how do I write a cout statement so it only shows the turkey total. I know I have to use the if statement but now sure how to.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You have order as an int but are comparing it against characters. Switch to char.

    You aren't using and don't need the variables salami, cheese and turkey.

    After the switch, if you output the value of food, then it will show the correct number because you set the correct value inside the switch.

    Your return statement should be before the last brace, not inside the switch's brace.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    How do I ask the user how much of the food they picked they want and make it display that?

    How would I multiply only the sandwich they order by the price?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> How do I ask the user how much of the food they picked they want and make it display that?
    You already have code that asks the user which type of sandwich they want, so just you should know how to ask how many sandwiches they want. The question of how many is separate from what type, so you don't have to relate the two together. Just make a count variable that you can store the "how many" answer in.

    >> How would I multiply only the sandwich they order by the price?
    Use the variable that holds the "how many" and multiply it by the variable that holds the price (in this case that variable is food).

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    Alright thanks.

    I just need your guys advice on what I should to do complete this program.

    I have to ask the user what type of sandwich they would like (3 choices, each has different price). Then I have to ask them how they would like it shipped and finally find the total with taxes. I need to use switch and if. How should I do this?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Just keep going. You're doing fine so far. Start with the next thing on the to-do list, and if you get stuck, ask a specific question about your attempt.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    I'm a little confused because on our paper it said something about using 3 different switch structures. Where would I need this?

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    I would guess they are for the type of sandwich, the shipping, and couting the type of sandwich... but if you say you need to used 'if' also, then I'm not sure where you'd put that.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    Oh I figured it out. I need to have 3 different switches because they may want more than one type of sandwch.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    Getting a really stupid problem.

    Code:
    	fst = food * o;
    
    	cout << "Your subtotal is $" << fst << endl;

    Answer is coming out to some negative huge number. What could be the problem?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Do food and o have values set? You'll get negative huge numbers when one of them is not initialized.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	cout << "n - No Order" << endl;
    	cin >> order;
    ....
    	case 't':
    	food = 2.39;
    	break;
    	case 'n':
    	food = 5.59;
    ....
    So if I'm not hungry, I'm 3 and a bit better off by buying a Turkey sandwich than by saying "I don't want anything".

    Supermarkets may pick up on this one... ;-)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sembhi View Post
    Getting a really stupid problem.

    Code:
    	fst = food * o;
    
    	cout << "Your subtotal is $" << fst << endl;

    Answer is coming out to some negative huge number. What could be the problem?
    There are two names of variables that are "dubious", and that is "o" and "l" - because they can look very similar to zero and one. Of course, upper case O is worse than lower-case o, but it should be avoided as much as possible. Since C allows long variable names, it shouldn't be difficult to avoid using such names - in old versions of Basic that only allowed single letters for variables it may be a different story.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    Here is my code/

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    
    int main()
    {
    
    	char o;
    	double food;
    	double fst;     // first sub total
    	double sst;		// second sub total
    	double st;		// subtotal
    	double tst;		// third sub total
    	double tbt;    // total before tax
    	int zone;
    	double shipping_cost;
    	double tws;		// total with shipping
    	double gt;		//
    	
    	cout << "What would you like to order?" << endl;
    	cout <<	"1 - Salami - $5.59 per pound" << endl;
    	cout << "2 - Cheese - $4.49 per pound" << endl;
    	cout << "3 - Turkey - $2.39 per pound" << endl;
    	cout << "4 - No Order" << endl;
    	cin >> o;
    	cout <<	endl;
    
    	switch (o)
    	{
    	case 1:
    	food = 5.59;
    	break;
    	case 2:
    	food = 4.49;
    	break;
    	case 3:
    	food = 2.39;
    	break;
    	case '4':
    	food = 0;
    	break; 
    	}
    
    	if (food == 0)
    	{
    		cout << "Good bye!" << endl;
    		return 0;
    	}
    	else
    	{
    	{
    	cout << "How much would you like?" << endl;
    	cin >> o;
    
    	cout <<	endl;
    
    	fst = food * o;
    
    	cout << "Your subtotal is $" << fst << endl;
    	}
    	}
    How do I initialize food to each of those values?

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In your original code, order was an int but your cases were characters.

    Now, o is a char and your cases use integers.

    Pick one or the other, characters or integers.

    BTW, I like the variable name order much better than o. Also, you should be using a different variable for the sandwich type and the number of sandwiches. Here you used o for both.
    Last edited by Daved; 11-14-2007 at 07:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM