CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: help with loops

Threaded View

  1. #1
    Join Date
    Nov 2010
    Posts
    3

    [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]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured