Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The error i'm getting is 'input string was not in the correct format' this is the code
static void Main(string[] args)
       {
           int numberOne;
           int numberTwo;
           int numberThree;
           int numberFour;
           int numberFive;

           Console.WriteLine("Enter the first integer");
           numberOne = int.Parse(Console.ReadLine());

           Console.WriteLine("Enter the second integer");
           numberTwo = int.Parse(Console.ReadLine());

           Console.WriteLine("Enter the third integer");
           numberThree = int.Parse(Console.ReadLine());

           Console.WriteLine("Enter the fourth integer");
           numberFour = int.Parse(Console.ReadLine());

           Console.WriteLine("Enter the fifth integer");
           numberFive = int.Parse(Console.ReadLine());


           Arithmetic arithmetic = new Arithmetic();
           arithmetic.ArithmeticMethod();
       }
   }


public void ArithmeticMethod()
      {
          int numberOne;
          int numberTwo;
          int numberThree;
          int numberFour;
          int numberFive;

          numberOne = int.Parse(Console.ReadLine());

          numberTwo = int.Parse(Console.ReadLine());

          numberThree = int.Parse(Console.ReadLine());

          numberFour = int.Parse(Console.ReadLine());

          numberFive = int.Parse(Console.ReadLine());

          Sum(numberOne, numberTwo, numberThree, numberFour, numberFive);
          Average(numberOne, numberTwo, numberThree, numberFour, numberFive);
          Product(numberOne, numberTwo, numberThree, numberFour, numberFive);
          SmallestNumber(numberOne, numberTwo, numberThree, numberFour, numberFive);
          LargestNumber(numberOne, numberTwo, numberThree, numberFour, numberFive);
      }
      public static int Sum(int numberOne, int numberTwo, int numberThree, int numberFour, int numberFive)
      {
          int sum;
          sum = numberOne + numberTwo + numberThree + numberFour + numberFive;
          Console.WriteLine($"the sum of the integers is {sum}");
          return sum;
      }


I'm really not sure what's going on.

What I have tried:

i've tried to cast it differently but nothing is really working.
Posted
Updated 22-Feb-20 16:08pm
Comments
Patrice T 22-Feb-20 20:45pm    
Where is the error ?
What is the guilty input ?
Member 14637431 22-Feb-20 21:02pm    
the Ints in the second part of the code

1 solution

If you just hit enter on any of those ReadLines without entering a value, i.e: leaving it blank, that conversion is going to fail. An empty string cannot be converted to a number.

Unroll those int.Parse lines. That means do NOT put the Console.ReadLine called in them. Replace int.Prase with int.TryParse instead. Google for "int.TryParse" for the documentation on how to use it.

Also, repeating code just screams "make me my own method!". Create a method that just accepts input and parses that input to an integer, then return the integer.
 
Share this answer
 
Comments
Maciej Los 23-Feb-20 8:55am    
5ed!
Member 14637431 23-Feb-20 14:27pm    
I think I get what you're saying, i'm just confused as to how to implement a TryParse with a user input/ReadLine
Dave Kreskowiak 23-Feb-20 18:31pm    
Break out the Console.ReadLine into its own line of code:
    string inputValue = Console.ReadLine();

Now you've got the input in its own string and can do other things with it, like makle your code FAR more debuggable, and parse the string with int.TryParse.
Member 13566383 23-Feb-20 15:53pm    
Do it step by step.
First step: Read the documentation ( https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=netframework-4.8 )
Second step: Copy the example given in the documentation I cited and try to understand what's going on. Use the debugger to step through the code.
Third step: Apply your new acquired knowledge to your programme.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900