Roman Gusan
Roman Gusan

Reputation: 41

How to add user input together

My program has to add the previous number entered to the next one. This is what I have so far, and I got stuck. It adds up the number that I have entered, but I don't understand how I have to validate one number from previous.

static void Main(string[] args)
{
    const int QUIT = -1;
    string inputStr;
    int inputInt = 0;
    do
    {
        Console.Write("Type a number (type -1 to quit): ");
        inputStr = Console.ReadLine();
        bool inputBool = int.TryParse(inputStr, out inputInt);

        if (inputBool == true)
            inputInt += inputInt;

        Console.WriteLine("Sum of the past three numbers is: {0}", inputInt);
    } while (inputInt != QUIT);
}

Since there may be an indefinite number of entries, I don't know how to properly use an array.

Upvotes: 1

Views: 4927

Answers (5)

Charles Lambert
Charles Lambert

Reputation: 5132

you need to create a local variable to hold the output from int.TryParse. Also you do not need a do while loop, you can exit immediately when the input is -1. With these changes, keeping up with the last 3 numbers becomes much easier:

static void Main(string[] args)
{
    const int QUIT = -1;
    int[] last3 = new int[3];
    // infinite loop, exit is done when input is -1
    for(;;) {
        Console.Write("Type a number (type -1 to quit): ");
        var input = Console.ReadLine();
        int tmp; // holds the int value of the input
        if (int.TryParse(input, out tmp))
        {
            if (tmp == QUIT)
                break; // input is -1

            // input was an int, so lets move the last two towards
            // the front of the array, last3[0] contained the oldest value
            // which is gone now
            last3[0] = last3[1];
            last3[1] = last3[2];

            // now overwrite the last item in the array with the newest value 
            last3[2] = tmp;
        }

        // add up the values, note if input was not a valid int, this will sum
        // the last 3 valid values because the above if statement will only execute
        // and change the values in the array if the user input was a number
        var sum = last3[0] + last3[1] + last3[2];

        Console.WriteLine("Sum of the past three numbers is: {0}", sum);
    }
}

Upvotes: 0

Steve's a D
Steve's a D

Reputation: 3821

Use a list to store all of the numbers as they come in. Then at the end count how many items are in the list, and Sum() the entire list

static void Main(string[] args)
{
    const int QUIT = -1;
    string inputStr;
    List<int> allNumbers = new List<int>();
    do
    {
        Console.Write("Type a number (type -1 to quit): ");
        inputStr = Console.ReadLine();
        bool inputBool = int.TryParse(inputStr, out inputInt);

        if (inputBool == true)
            allNumbers.Add(inputInt); // Add a new element to the list
        Console.WriteLine("Sum of the past " + allNumbers.Count + " numbers is: {0}", allNumbers.Sum());
    } while (inputInt != QUIT);
}

Upvotes: 0

user195488
user195488

Reputation:

This answer uses LINQ and Queues to do what you want.

static void Main(string[] args)
{
   const int QUIT = -1;
   string inputStr;
   int inputInt = 0;

   Queue myQ = new Queue();

   do
   {
      Console.Write("Type a number (type -1 to quit): ");
      inputStr = Console.ReadLine();
      bool inputBool = int.TryParse(inputStr, out inputInt);

      if (inputBool == true)
      {
         if (myQ.Count() == 3)
         {
            myQ.Dequeue();
            myQ.Enqueue(inputInt);
         }
         else
         {
            myQ.Enqueue(inputInt);
         } 
      }

      if (myQ.Count() == 3)
      {
         Console.WriteLine("Sum of the past three numbers is: {0}", myQ.Sum());
      }

   } while (inputInt != QUIT);

}

Upvotes: 0

ram2013
ram2013

Reputation: 505

If you are trying to find sum of numbers take another variable.

static void Main(string[] args)
{
    const int QUIT = -1;
    string inputStr;
    int inputInt = 0,tempint=0;
    do
    {
        Console.Write("Type a number (type -1 to quit): ");
        inputStr = Console.ReadLine();
        bool inputBool = int.TryParse(inputStr, out tempint);

        if (inputBool == true)
        {
            inputInt += tempint;
        }

        Console.WriteLine("Sum of the past three numbers is: {0}", inputInt);

    } while (tempint!= QUIT);


}

Upvotes: 2

If you want the simplest solution, you can just keep track of the past 3 numbers and sum them up when you print

static void Main(string[] args)
{
    const int QUIT = -1;
    string inputStr;
    int i1 = 0;
    int i2 = 0;
    int i3 = 0;
    int inputInt = 0;
    do
    {
        Console.Write("Type a number (type -1 to quit): ");
        inputStr = Console.ReadLine();
        bool inputBool = int.TryParse(inputStr, out inputInt);

        if (inputBool == true)
        {
            i3 = i2;
            i2 = i1;
            i1 = inputInt;
        }

        Console.WriteLine("Sum of the past three numbers is: {0}", i1+i2+i3);

    } while (inputInt != QUIT);


}

Upvotes: 0

Related Questions