'The console does not accept the number written by the console as an int. How to fix it?
I have been studying C# for about a week (I have no other programming experience, so please be gentle) and am trying to write a calculator as part of a typical exam. My calculator works adequately with two numbers and I'm trying to get it to work with more. My idea was to restart MyMet after the console had written the result of the first calculation. Problem: The console refuses to accept the number written to it as int a and throws an error. How to fix it?
In this example code I provided, only the multiplication operation shows my idea. Other operations have remained unchanged since the version of the calculator that only worked with two numbers. I also apologize if my question or my language seems silly. I've only been studying C# for a week and English is not my native language.
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
void MyMet()
{
string[] calc = Console.ReadLine().Split(' ');
int a = int.Parse(calc[0]);
char b = char.Parse(calc[1]);
int c = int.Parse(calc[2]);
switch (b)
{
case '*':
Console.Write(a * c);
MyMet();
break;
case '+':
Console.WriteLine(a + c);
Console.ReadKey();
break;
case '-':
Console.WriteLine(a - c);
Console.ReadKey();
break;
case '/':
Console.WriteLine(a / c);
Console.ReadKey();
break;
}
}
MyMet();
}
}
}
This example produces an error:
System.FormatException: "The input string was not in the correct format."
I want the output from the previous calculation to always be the first input number of the next calculation. How easy is it to achieve this?
On advice, I checked the input value through debugging. Debugging shows that the input value is missing. Conclusion: The console absolutely does not see the number entered to it. I'm attaching a screenshot and a link to a video demonstrating the problem.
Video link: https://drive.google.com/file/d/1Yzk3mMP6zweZ29y9Jq92fJTg2mypFgfn/view?usp=sharing
If anyone is interested, I found an alternative solution and just overwrote the variables. I am attaching the final code of the program, but I am still looking for an answer to the question why the console does not accept the number written by the console and how to fix it.
class Program
{
static void Main(string[] args)
{
string[] calc = Console.ReadLine().Split(' ');
double a = double.Parse(calc[0]);
char b = char.Parse(calc[1]);
double c = double.Parse(calc[2]);
void MyMet()
{
switch (b)
{
case '*':
Console.Write(a * c);
calc = Console.ReadLine().Split(' ');
a = a * c;
b = char.Parse(calc[1]);
c = double.Parse(calc[2]);
MyMet();
break;
case '+':
Console.Write(a + c);
calc = Console.ReadLine().Split(' ');
a = a + c;
b = char.Parse(calc[1]);
c = double.Parse(calc[2]);
MyMet();
break;
case '-':
Console.Write(a - c);
calc = Console.ReadLine().Split(' ');
a = a - c;
b = char.Parse(calc[1]);
c = double.Parse(calc[2]);
MyMet();
break;
case '/':
Console.Write(a / c);
calc = Console.ReadLine().Split(' ');
a = a / c;
b = char.Parse(calc[1]);
c = double.Parse(calc[2]);
MyMet();
break;
}
}
MyMet();
}
}
Thanks to one of the answers, I realized how easy it is to change the value of the variable a. I've marked this answer as correct and posting the final version of the calculator if anyone needs it. Thanks to all who answered and to the entire stackoverflow community.
static void Main(string[] args)
{
void MyMet(double? Result = null)
{
string[] calc = Console.ReadLine().Split(' ');
double a = Result ?? double.Parse(calc[0]);
char b = char.Parse(calc[1]);
double c = double.Parse(calc[2]);
switch (b)
{
case '*':
Result = a * c;
break;
case '+':
Result = a + c;
break;
case '-':
Result = a - c;
break;
case '/':
Result = a / c;
break;
}
Console.Write(Result);
MyMet(Result);
}
MyMet();
}
}
Solution 1:[1]
After looking at your video, I see the issue. You entered
3 * 2
and then on the next line it output
6
then you press space and add
- 3
So on the screen it looks like:
6 * 3
The confusion here is that the output does not serve dual purpose as input. The 6 was only output, not input. So the only input you provided to the computer the second time around was * 3, so a failure should have been expected.
If you want the calculator to save the last computed value to reuse, like a normal calculator does, then you can't pass it back and forth as output/input without retyping in it. What you probably want, since you have this as a recursive function, is to update your MyMet function to take an optional parameter with the computed result:
void MyMet(int? computedResult = null)
then for a you just set it to computedResult and coalesce it to a for the first case when its. not set yet.
a = computedResult ?? char.Parse(calc[0]);
and when you make the recursive calls, just pass the computation. I'd also move the print statement before the recursive call:
computedResult = a * c;
Console.Write(computedResult);
MyMet(computedResult);
Solution 2:[2]
Do this
string input = Console.ReadLine();
string[] calc = input.Split(' ');
and look to see what input contains
Put a breakpoint on the Split line so you can see the input string.
Solution 3:[3]
The problem is that one or more of your input strings is not parseable to an integer. Maybe the string value is "1.1" or maybe it's "!????????????????Qx?| ?23?????Y??????????? z??". Who knows. We can't tell what your input string is if you don't tell us.
But I can tell you the steps you need to take to solve the problem.
First off, you need to debug your code. Set a breakpoint before the first int.Parse. Step over (F10) until the error gets thrown.
Now you know which index of calc has the problem. Run the code again and see what the value is at calc[the index with the problem]:

Odds are you just need to run a simple string.Trim
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dan Csharpster |
| Solution 2 | pm100 |
| Solution 3 |

