Jump to content

Novice needs help with C#

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
jendomatic

jendomatic

    Newbie

  • Members
  • Pip
  • 5 posts
I need help with my C# program. I'm trying to design a program that mimics the control system of a drink vending machine. It consist of 4 different drinks and its to operate continually alowing the users to obtain items until they have ordered all they want. If they type in anything other than the required characters an error comes up and on termination d prog display the number and detalis purchased. Do i have to use the IF statement here at all??

This is what my code looks like so far
Console.WriteLine("Name of drink goes in here" price then requested character"); goes that way untile the 4th drink.

then... I used a String menu = Console.ReadLine(); at the end of it. But when I run the program, the items all comes up at once and when i type in the required characters and select E to exist I do not see any purchases made. It just goes blank

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What code do you have?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
jendomatic

jendomatic

    Newbie

  • Members
  • Pip
  • 5 posts
This is code I've tried to run.
{
    class Program
    {
        static void Main(string[] args)
        {
            //write to console and get input
            Console.WriteLine("Hot Chocolate 99p - type in H:");
            Console.WriteLine("Pepsi 108p - type in P:");
            Console.WriteLine("Orange Juice 85p - type in O:");
            Console.WriteLine("Tea 50p - type in T:");
            Console.WriteLine("Total items:");
            String items = Console.ReadLine();
            Console.Write("Items selected, {0}", items);
            Console.ReadLine();
        }
    }
}
But when I run it and press E, the screen disappears but its suppose to show all the menu ordered on the screen. Do i need the IF statement at this point and do I have to change the Console.WriteLine to Console.Write

Edited by WingedPanther, 10 October 2009 - 05:40 PM.
add code tags (the # button)


#4
matio

matio

    Learning Programmer

  • Members
  • PipPipPip
  • 51 posts
When run I get:
Unhandled Exception: System.IndexOutOfRangeException: Array index is out of range.
  at xxx.MainClass.Main (System.String[] args) [0x00000] 
(this may not be related, sorry if it isn't)

#5
Hignar

Hignar

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 420 posts
A few points

1. You need to contain some of your code in a while loop so that the user can make additional selections. If you only want the user to be able to purchase 4 drinks then you need something like


while (items < 4)

{

    //code for purchasing drinks

}


2. In order for the loop to work you'd need set the variable items as an int and converting the user input using int.Parse


int items = 0;

items +=int.Parse(Console.ReadLine());


3. You need a switch statement to allow the user to pick a drink

I don't get the error posted by matio by the way.
If there's a new way, I'll be the first in line.

But, it better work this time.

#6
matio

matio

    Learning Programmer

  • Members
  • PipPipPip
  • 51 posts

Hignar said:

I don't get the error posted by matio by the way.
Probably my set up.