C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-03-2009, 03:17 PM   #1
Registered User
 
Join Date: Nov 2009
Location: Can
Posts: 1
C# McDonalds Program Help!

I can't figure out how to assign the value of the item to a variable and store it to be used later, So i can add it to the other items to give the total of the order.

Please Help!
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ex6_2
{
    class Program
    {
        static void Main(string[] args)
        {
            string userin;
            char sandwichch;
            char friesch;
            char drinkch;
            int sandwich = 0, fries = 0, drink = 0;
            double total = 0.0;

            Console.WriteLine("(a) Big Mac - $3.99         (b) McChicken - $3.99");
            Console.WriteLine("(c) Filet-O-Fish - $3.00    (d) No Sandwich");
            Console.WriteLine("Please select sandwich: ");
            userin = Console.ReadLine();
            sandwichch = userin[0];
            switch (sandwichch)
            {
                case 'a': //bigmac
                    sandwich = 3.49;
                    sandwich = Convert.ToInt32(Console.ReadLine());
                    break;
                case 'b': //mcchicken
                    sandwich = 3.49;
                    sandwich = Convert.ToInt32(Console.ReadLine());
                    break;
                case 'c': //filetofish
                    sandwich = 3.00;
                    sandwich = Convert.ToInt32(Console.ReadLine());
                    break;
                case 'd': //nosandwich
                    sandwich = 0.00;
                    sandwich = Convert.ToInt32(Console.ReadLine());
                    break;
                default:
                    total = 0.0;
                    Console.WriteLine("Invalid Input");
                    break;
            }
            Console.WriteLine("(a) Large - $2.25 (b) Medium - $1.75 (c) Small - $1.75 (d) No Fries");
            Console.WriteLine("Please select fries size: ");
            userin = Console.ReadLine();
            fries = userin[0];
            switch (friesch)
            {
                case 'a': //large
                    fries = 2.25;
                    fries = Convert.ToInt32(Console.ReadLine());
                    break;
                case 'b': //medium
                    fries = 1.75;
                    fries = Convert.ToInt32(Console.ReadLine());
                    break;
                case 'c': //small
                    fries = 1.25;
                    fries = Convert.ToInt32(Console.ReadLine());
                    break;
                case 'd': //no
                    fries = 0.00;
                    fries = Convert.ToInt32(Console.ReadLine());
                    break;
                default:
                    total = 0.0;
                    Console.WriteLine("Invalid Input");
                    break;
            }
            Console.WriteLine("(a) Large - $1.75 (b) Medium - $1.60 (c) Small - $1.30 (d) No Drink");
            Console.WriteLine("Please select drink size: ");
            userin = Console.ReadLine();
            drink = userin[0];
            switch (drinkch)
            {
                case 'a': //large
                    drink = 1.75;
                    drink = Convert.ToInt32(Console.ReadLine());
                    break;
                case 'b': //medium
                    drink = 1.60;
                    drink = Convert.ToInt32(Console.ReadLine());
                    break;
                case 'c': //small
                    drink = 1.30;
                    drink = Convert.ToInt32(Console.ReadLine());
                    break;
                case 'd': //no
                    drink = 0.00;
                    drink = Convert.ToInt32(Console.ReadLine());
                    break;
                default:
                    total = 0.0;
                    Console.WriteLine("Invalid Input");
                    break;
            }
            if (sandwich >= 3.00 && fries >= 1.25 && drink >=1.30)
            {
                total = sandwich + fries + drink - 0.75;
            }
            else
            {

            }
            Console.WriteLine("Your total is: {0:0.0}", total);
        }
    }
}
initialfresh is offline   Reply With Quote
Old 11-03-2009, 04:23 PM   #2
Jack of many languages
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 1,929
I used to love to eat Filet O Fish sandwiches, until I read the fat content. Sigh.
__________________
Mac and Windows cross platform programmer. Ruby lover.

Memorable Quotes From Recent Posts:

I can't remember.
Dino is offline   Reply With Quote
Old 11-03-2009, 04:42 PM   #3
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,470
Code:
case 'a': //bigmac
                    sandwich = 3.49;
                    sandwich = Convert.ToInt32(Console.ReadLine());
                    break;
                case 'b': //mcchicken
                    sandwich = 3.49;
                    sandwich = Convert.ToInt32(Console.ReadLine());
                    break;
                case 'c': //filetofish
                    sandwich = 3.00;
                    sandwich = Convert.ToInt32(Console.ReadLine());
                    break;
                case 'd': //nosandwich
                    sandwich = 0.00;
                    sandwich = Convert.ToInt32(Console.ReadLine());
You assign sandwich to a value and then proceed to blow it away in the next line. You need to setup a dictionary or a 2D array and assign the values in that way. What you are attempting to do, or at least what it looks like, is extract the value from what is printed on screen. While you can probably find a way to do that it is not ideal. Your output should be a way to display what is in memory to the user, not the other way around. Don't use video to compute what it is memory.

Here is a sample of a dictionary that maps a string to a float or in .NET terms a System.String to a System.Single.
key..........value
"Big mac",3.49

All you do from here is lookup sandwich in the dictionary and it will return 3.49 or the value associated with Big mac. In order to make a dynamic menu system you can use the same type of approach.
Here is a dictionary that maps from a String to a String which might seem odd and it is but it does work. Since you are noticing that this is an odd construct it should clue you in that this is not the only way way to arrive at Big mac from the letter a and that that there might be a better approach.

key..value
"a","Big mac"

Now when the user presses 'a' you get the value Big mac. Use the value Big mac to lookup it's price in your other dictionary. As long as your dictionary matches what is displayed on screen it will work.
__________________
If you aim at everything you will hit something but you won't know what it is.

Last edited by Bubba; 11-03-2009 at 04:46 PM.
Bubba is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Issue with program that's calling a function and has a loop tigerfansince84 C++ Programming 9 11-12-2008 01:38 PM
Need help with a program, theres something in it for you engstudent363 C Programming 1 02-29-2008 01:41 PM
This is a simple program.. Help me please I think it has an error.. lesrhac03 C Programming 4 02-21-2008 10:39 AM
My program, anyhelp @licomb C Programming 14 08-14-2001 10:04 PM


All times are GMT -6. The time now is 12:10 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22