Reputation: 141
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.
Upvotes: 3
Views: 193
Reputation: 3033
May be you need an C# expert to answer, but when I debug I got that, because when you ENTER, it will push to Console 13 and 10. So you need to Read
twice to through them.
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;
/**
*HINT: Try to use below double line:
*/
System.Console.Read();
System.Console.Read();
System.Console.Write("Please enter the second number: ");
input1 = System.Console.Read(); //Doesn't wasit for input and sets input1 = 13
System.Console.Read();
output = input * input1;
System.Console.WriteLine();
System.Console.Write("{0} times {1} equals {2}.", input, input1, output);
System.Console.ReadKey();
}
You can try to see more:
static void Main()
{
Console.Write("Please enter just one number and press enter:");
int input1 = Console.Read();
int input2 = Console.Read();
int input3 = Console.Read();
Console.WriteLine("You've inputted: {0},{1},{2}",input1, input2, input3);
Console.ReadKey();
}
Please enter just one number and press enter:1
You've inputted: 49,13,10
Upvotes: -1
Reputation: 991
Console.Read() only reads one character. So your first Console.Read() reads your number and the second reads the CR Ascii code (13) because of your "Enter". Use ReadLine() instead.
System.Console.Write("Please enter the first number: ");
string first = System.Console.ReadLine();
input = Convert.ToInt32(first);
System.Console.WriteLine();
System.Console.Write("Please enter the second number: ");
string second = System.Console.ReadLine();
input1 = Convert.ToInt32(second);
System.Console.WriteLine();
output = input * input1;
System.Console.WriteLine();
System.Console.Write("{0} times {1} equals {2}.", input, input1, output);
System.Console.ReadKey();
Upvotes: 4
Reputation: 11499
I believe it is because your program is using Console.Read()
instead of Console.ReadLine()
This means that it is reading 1 character at a time from the input. Your first input is 5 + a return. Try using ReadLine and you need to protect against entering something that isn't an integer. You can use the following program to achieve what you want.
class Program
{
static void Main(string[] args)
{
int input, input1;
string 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: ");
int.TryParse(System.Console.ReadLine(),out input); //Reads my input
System.Console.WriteLine();
System.Console.Write("Please enter the second number: ");
int.TryParse(System.Console.ReadLine(), out input1);
System.Console.WriteLine();
if (input != 0 && input1 != 0) {
input = input - 48;
output = (input * input1).ToString();
}else
{
output = "NaN";//not a number
}
System.Console.WriteLine();
System.Console.Write("{0} times {1} equals {2}.", input, input1, output);
System.Console.ReadKey();
}
}
Upvotes: 1
Reputation: 33151
The problem is that you are reading two characters one after the other. The best way to solve this is input
is set when you press the 1
key. input1
is 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.ReadLine
System.Console.Read
reads 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 use System.Console.ReadLine
which 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 using Int32.Parse
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 = Int32.Parse(System.Console.Read());
//input = input - 48;//No need to do this anymore as we have already converted the user's input to an integer.
System.Console.WriteLine();
System.Console.Write("Please enter the second number: ");
input1 = Int32.Parse(System.Console.ReadLine());
System.Console.WriteLine();
output = input * input1;
System.Console.WriteLine();
System.Console.Write("{0} times {1} equals {2}.", input, input1, output);
System.Console.ReadKey();
}
}
Upvotes: 1
Reputation: 17010
It has to do with how Read() and ReadLine() work. Try ReadLine() instead.
Upvotes: 1