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.
Odds are you just need to run a simple string.Trim