0

I'm new to c# and am trying to make a simple calculator. It works fine until it goes back to the start to take a new number. When taking the new number it says it cant convert the user input to a integer.

using System;

namespace simpleCalculator
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            start:
            Console.Clear();
            Console.WriteLine("Enter first number");
            int x = Convert.ToConsole.ReadLine();
            Console.WriteLine("Would you like to\n1. Add\n2. Multiply\n3. Devide");
            string o = Console.ReadLine();
            if (o == "1")
            {
                Console.WriteLine("Enter second number\n");
                int y = Convert.ToInt32(Console.ReadLine());
                add(temp, y);
                goto start;



                Console.Clear();
                goto start;
            }
        }
        public static void add(int num01, int num02)
            {
                Console.Clear();
                Console.WriteLine((num01 + num02) + "\nPress enter to contiue.");
                Console.Read();
            }
        }
    }
4
  • Can you say exactly which line gives you the error? Commented Mar 18, 2017 at 14:48
  • Have you looked at int.TryParse? Commented Mar 18, 2017 at 14:50
  • try using while(true) instead of goto and use tryparse. msdn.microsoft.com/en-us/library/… Commented Mar 18, 2017 at 14:51
  • 1
    What's Convert.ToConsole.ReadLine()? Seems like that's where your problem is to me. Commented Mar 18, 2017 at 14:55

3 Answers 3

2

Use TryParse so if the parsing fails, you will not get an exception.

var enteredValue = Console.ReadLine();
var parsedValue;
if (!int.TryParse(enteredValue, out parsedValue))
{
    // parse failed do whatever you want
}

Do that for both entries and if both of them pass, then call your add method.

Sign up to request clarification or add additional context in comments.

Comments

0

You're looking for int.Parse(). Be careful to validate your input. You should probably create an escape condition.

Edited to show an alternative solution
Edited to be more explicit on how to handle some input

class Program
{
    public static void Main(string[] args)
    {
        string input = String.Empty;
        int x = 0, y = 0;

        while (true)
        {
            try
            {
                Console.WriteLine("Enter first number");
                 x = int.Parse(Console.ReadLine());

                Console.WriteLine("Would you like to\n1. Add\n2. Multiply\n3. Divide");
                input = Console.ReadLine();

                Console.WriteLine("Please enter a second number");
                y = int.Parse(Console.ReadLine());
            }
            catch
            {
                Console.WriteLine("Invalid input");
                continue;
            }
            switch (input)
            {
                case "1":
                    Console.WriteLine($"{x} + {y} = " + add(x, y));
                    break;
                case "2":
                    //TODO implement multiply case
                    break;
                case "3":
                    //TODO implement divide case
                    break;
                default:
                    Console.WriteLine("Invalid input");
                        break;
            }
        }
    }
    public static int add(int x, int y) => x + y;
}

6 Comments

Which will still error with: Input string was not in a correct format.
That line as it stands does not have an error. It is meant as an example that OP can use to fix his issue.
Unfortunately as freedom states, your solution will still crash as the OP's does. So it really isn't a solution.
"Be careful to validate your input."
I've been more explicit now.
|
0

Try this: int numbers = Convert.ToInt32("1234");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.