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
Novice needs help with C#
Started by jendomatic, Oct 10 2009 03:21 AM
5 replies to this topic
#1
Posted 10 October 2009 - 03:21 AM
|
|
|
#2
Posted 10 October 2009 - 05:54 AM
What code do you have?
#3
Posted 10 October 2009 - 03:43 PM
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
Posted 10 October 2009 - 11:28 PM
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
Posted 11 October 2009 - 02:50 AM
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
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
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.
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.
But, it better work this time.
#6
Posted 11 October 2009 - 08:11 AM
Hignar said:
I don't get the error posted by matio by the way.


Sign In
Create Account

Back to top









