-
November 19th, 2010, 02:53 AM
#1
[resolved]help with loops
i need help with a program i am writing for college.
i dont want people to do it for me , im just after pointers & hints please.
the question is this
The user wants an application that allows her to enter a series of numbers until she decides to exit the application by typing 4 for quit. On starting the program the user should be asked to enter the first number. A menu should be displayed inviting the user to select an option from the menu.
Menu options include:
1. Add currently entered number to stored total and display new total
2. Subtract currently entered number from stored total and display new total
3. Average currently entered number with stored total, add to stored total and display new total
4. Quit the application and display the stored total
If the user types 4, the program should display “Goodbye”. Otherwise typing 1, 2 or 3 should ask the user to enter another number and display the result of the selected operation.
the code i have written so far is this
class Program
{
static void Main(string[] args)
{
double num1 = 0; // value of num1
double num2 = 0; // value of num2
double result = 0;
double temp; //value of temp
string enterFirst; // read enterFirst
string enterNum; // read enterNum
string myInput = ""; // read myInput
int count = 0;
Console.WriteLine("Enter First Number:"); // enter value for enterNum1
enterFirst = Console.ReadLine(); // read enterNum1
num1 = double.Parse(enterFirst);
while (myInput != "4")
{
Console.WriteLine("\n1. Add Numbers\n"); //display output
Console.WriteLine("\n2. Subtract Numbers\n"); //display output
Console.WriteLine("\n3.Average Numbers\n"); //display output
Console.WriteLine("\n4. Quit\n"); // display output
Console.WriteLine("\nPick option"); // display output
myInput = Console.ReadLine(); // read myInput
if (myInput == "4")
{
Console.WriteLine("Goodbye");
Console.WriteLine("The result is " + result);
}
else
{
Console.WriteLine("Enter a number");
enterNum = Console.ReadLine();
num2 = double.Parse(enterNum);
}
switch (myInput)
{
case "1": temp = num1 + num2;
count++;
if (count > 1)
{
temp = temp + num2;
result = temp;
}
break;
case "2": temp = num1 - num2;
count++;
if (count > 1)
{
temp = temp - num2;
result = temp;
}
break;
case "3":
break;
default:
break;
}
}
}
}
my problem is this ,
after the program loops round a second time im not sure how to get it to ignore the first entered number and just continue with the running total. Is this problem caused by using the switch statement, if not how can i over come this ??
any help is gratefully recieved , and rememebr only hint and tips please .
many thanks
Last edited by cats_rule; November 24th, 2010 at 03:51 AM.
Reason: [resolved]
-
November 19th, 2010, 04:04 PM
#2
Re: help with loops
use
(code) (/code) to post code, replace ( ) with [ ]
one problem is see
you need to put this
Code:
if (myInput == "4")
{
Console.WriteLine("Goodbye");
Console.WriteLine("The result is " + result);
}
outside of
Code:
while (myInput != "4")
there is that for starters...
Last edited by bixel; November 19th, 2010 at 04:10 PM.
-
November 21st, 2010, 02:08 AM
#3
Re: help with loops
hi Bixel,
thank you for your reply & help.
i realised after id posted i should have used the code tags.i found i wasnt able to edit my post afterwards.
im away from my pc with visual studio on it but will try your advice when i get the chance.
just so i understand correctly , by moving
Code:
if (myInput == "4")
{
Console.WriteLine("Goodbye");
Console.WriteLine("The result is " + result);
}
outside the while statement . i assume this allows me to use the running total without calling on the first entered number???
if not could you explain why im doing this.
ive only been programming in c# since september :-(
-
November 24th, 2010, 03:51 AM
#4
Re: help with loops
i have managed to sort this problem now.
thanks for the help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|