0

I created a console application that will take user input of any number and + or - operator for now, and returns the result. for example user can input 1+2+3+4, and result should be 10.

My problem is that I cannot figure out how to get the last number to be summed into the total result.

that mean in my code only 1+2+3 will be calculated while 4 will be ignored.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace SimpleCalculator
{
    public class Program
    {
        public static void Main()
        {
            int result = 0;
            int number;
            string numberString = "";

            Console.WriteLine("Enter numbers followed by operation eg. x+y-z");
            while (true)
            {
                string userInput = UserInput();
                //Loop into each element of the input string
                for (int i = 0; i < userInput.Length; i++)
                {
                    if((IsNumber(userInput[i])))
                    {
                        numberString += userInput[i];
                    }
                    else if (!IsNumber(userInput[i]))
                    {
                        number = Int32.Parse(numberString);
                        numberString = "";
                        result = PerformCalculation(result, number, userInput[i]);
                    }

                }
                number = Int32.Parse(numberString);
                result += number;
                Console.WriteLine($"{userInput}={result}");
            }
        }

        // check if input is number or operator
        static bool IsNumber(char input)
        {
            if (char.IsDigit(input)){return true;}
            else{return false;}
        }

        static string UserInput()
        {
            string User_input = Console.ReadLine();
            return User_input;
        }

        static int PerformCalculation(int sum, int num, char op)
        {
            switch (op)
            {
                case '+': return sum + num;
                case '-': return sum - num;
                default: throw new ArgumentException("Uknown operator");
            }
        }
    }
}
AMJ
  • 11
  • 2
  • It looks like you only perform addition when a `+` is read. Thus, the last digit entered ; 3, in your `1+2+3` example, is left in `numberString` but never summed. Consider having a special case when i = userInput.Length-1, which is when the very last number is being read. – user43395 Apr 29 '19 at 20:41
  • Seems like a homework question that can be solved via debugging. Did you debug the code? – ilkerkaran Apr 29 '19 at 20:44
  • My suggestion; Take each number and the operator before it as a single number. If there is no operator, use "+". So "1+2+3-4" breaks down into "+1", "+2", "+3", "-4". Add them together and you get the correct answer. Of course, that will only work for addition and subtraction. – Heretic Monkey Apr 29 '19 at 20:47
  • Thanks for replying, I figured adding that condition input.length - 1 but got stuck at the point where to performcalculation. – AMJ Apr 29 '19 at 22:57

1 Answers1

1

You have already figured out that you are not adding the last number and lets do some thinking to figure out why.

The number will only be added when you call PerformCalculation and whats the condition that this will be called? else if (!IsNumber(userInput[i]))

If you look at your input do you see a non number at the end of the string? if not then PerformCalculation wont get called.

How do you fix that? Before you return the result check if numberString is empty, if not then do the final calculation before returning the result.

Since this is a homework problem i will leave the rest as an exercise.

Steve
  • 11,696
  • 7
  • 43
  • 81
  • Thank you, I will be looking into that once I get to code. This is not homework I’m self taught c# - my aim is to make the application more complicated by adding later multiplication and division ability. – AMJ Apr 29 '19 at 22:58