I am trying to create a simple calculator in the console for single digit numbers.(Well I actually only care about multiplication)
So, here is my code and if someone could help me.
class multiplythisnumber
{
static void Main()
{
int input, input1, output; //variable decleration
System.Console.WriteLine("This application is meant to multiply two single digit numbers.");
System.Console.WriteLine("You can choose both numbers, however this is only a test.\r I am not sure the read command even does what I think it does.");
System.Console.WriteLine();
System.Console.Write("Please enter the first number: ");
input = System.Console.Read(); //Reads my input + 48(assuming 48 is the value of enter)
input = input - 48;
System.Console.WriteLine();
System.Console.Write("Please enter the second number: ");
input1 = System.Console.Read(); //Doesn't wasit for input and sets input1 = 13
System.Console.WriteLine();
output = input * input1;
System.Console.WriteLine();
System.Console.Write("{0} times {1} equals {2}.", input, input1, output);
System.Console.ReadKey();
}
}
I added comments to explain what I was doing to everyone else, and just as personal notes.
The last line always ends up as “(0) times 13 equals 0.” -Assuming I used zero for input 1.
Edit:
Just to clarify I know 0*13=0 (yes it said 12 earlier what it has really been saying is 13). The problem is that it is not allowing me to set input1 and is just setting it at 13 and continuing executing.
Edit2: I would like to say thanks to Matt, because the changes he made allowed the code to work correctly. So, looks like I have my first code that actually does something other than tell you your own name.
The problem is that you are reading two characters one after the other.The best way to solve this isinputis set when you press the1key.input1is read when you press the Enter key. Just don’t press the Enter key; instead enter both numbers immediately one after the other.System.Console.ReadLineSystem.Console.Readreads a single character from the console. When you type the 0 key, your program actually reads the character ‘0’ (‘0’ == 48 in ASCII) not the number 0.Here’s how I would fix your program:
Instead of using
System.Console.Read, which reads a character, I would useSystem.Console.ReadLinewhich reads an entire line rather than a single key/character. This would allow your users to enter numbers longer than one digit. You can convert the user’s input from a string to an integer by usingInt32.Parse