I am trying to create a C# calculator for a class, but when I try to use it it always returns blatantly wrong answers, like 1+3 = 100, or rounding down the decimal 2.33 to 50. It also crashes after the first input every time, which makes me think that its reading when i press enter for some reason, but I've looked and can't find documentation on any issue like that. Thanks in advance.
'
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace calculator
{
class Program
{
static void Main(string[] args)
{
//set label for finished functions to return to
start:
System.Console.WriteLine("choose from one of the following calculator choices");
System.Console.WriteLine("1: whole number");
System.Console.WriteLine("2: addition");
System.Console.WriteLine("3: sine");
System.Console.WriteLine("4: cosine");
System.Console.WriteLine("5: absolue value");
System.Console.WriteLine("6: quit");
int optionSelect = Convert.ToInt32(Console.ReadLine());
switch (optionSelect)
{
case 1:
System.Console.WriteLine("whole number");
System.Console.WriteLine("give a decimal");
decimal temp = System.Console.Read();
System.Console.WriteLine(Convert.ToInt32(temp));
goto start;
case 2:
System.Console.WriteLine("addition");
System.Console.WriteLine("first int");
int add1 = Convert.ToInt32(System.Console.ReadKey());
System.Console.WriteLine("second int");
Console.ReadLine();
int add2 = Convert.ToInt32(System.Console.Read());
Console.ReadLine();
int answer = add1+add2;
System.Console.WriteLine("the answer is " + answer);
goto start;
case 3:
System.Console.WriteLine("sine");
System.Console.WriteLine("enter your angle in radians");
double numSin = Console.Read();
System.Console.WriteLine(Math.Sin(numSin));
goto start;
case 4:
System.Console.WriteLine("cosine");
System.Console.WriteLine("enter your angle in radians");
double numCos = Console.Read();
System.Console.WriteLine(Math.Cos(numCos));
goto start;
case 5:
System.Console.WriteLine("absolute value");
System.Console.WriteLine("enter a number");
double num = Convert.ToDouble(Console.Read());
System.Console.WriteLine(Math.Abs(num));
goto start;
case 6:
System.Console.WriteLine("shutting down...");
goto start;
default:
System.Console.WriteLine("invalid input");
goto start;
}
}
}
}
`
So there are multiple problems in your code.
Console.Read
will return the corresponding ASCII code for the first character that you enter. Here is a link with the ASCII table: http://www.asciitable.com/
So if you put in the console 0 is not actually 0 but the corespondent value for 0 in the ASCII table. 0=48, 1=49, 2=50 ... 9 = 50.
If you put multiple characters only the first one will be processed by Console.Read
.
To fix your problem you could use as you did in the begging :
string line = Console.ReadLine();
and after that, you can convert that line to whatever you need like decimal x = Convert.ToDecimal(line)
. or int x = Convert.ToInt32(line)
Also please try to change that goto
logic into a while
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.