C # program does not evaluate operations and returns incorrect answers
Community.
I am learning how to program in C #. I wrote this little program that gets the name, age, favorite color, and two numbers from the user. I am using Notepad ++ to write code and run the C # compiler from the Windows command line. Here is the source code of the program
using System;
class ShowSomething
{
static void Main(string[] args)
{
string name, age, favColor;
int num1,num2, sum, mult, subs;
float div;
Console.Write("What is your name? ");
name = Console.ReadLine();
Console.WriteLine("Hello, " + name);
Console.WriteLine();
Console.Write("How old are you? ");
age = Console.ReadLine();
Console.WriteLine("So you are " + age, "I thought that you were older!");
Console.WriteLine();
Console.Write("What is your favorite Color? ");
favColor = Console.ReadLine();
Console.WriteLine(favColor + " is a cool color!");
Console.WriteLine();
Console.WriteLine("Nice meeting you, " + name, "Have a good day!");
Console.WriteLine();
Console.WriteLine("Let us do some operations, " + name);
Console.WriteLine();
Console.Write("Please enter a number: ");
num1 = Console.Read();
Console.Write("Please enter another number: ");
num2 = Console.Read();
sum = num1 + num2;
mult = num1 * num2;
subs = num1 - num2;
div = num1 / num2;
Console.WriteLine();
Console.WriteLine("Alright, " + name, "Let us blow up your mind!");
Console.WriteLine();
Console.WriteLine(num1 + "+" + num2, "=" + sum);
Console.WriteLine(num1 + "*" + num2, "=" + mult);
Console.WriteLine(num1 + "-" + num2, "=" + subs);
Console.WriteLine(num1 + "/" + num2, "=" + div);
Console.WriteLine();
Console.WriteLine("Mindblown, Right?");
}
}
When I run the program, everything goes well. However, when the user enters the first operation number, the program skips the second prompt and prints a completely different result from what was expected. For example, if I put 0 as the first number, the program goes to operations and prints the following:
//
48 + 13
48 * 13
48-13
48/13
Mindblown, right?
//
source to share
Don't use Console.Read
as it doesn't do what you expected:
Reads the next character from standard input (and returns an integer value of 1 that represents it).
Here's a good explanation from devshort about why the second call Console.Read
"skips":
If you enter the value "1" first, it will convert it to ascii representation. Then the carriage return will be STILL in the [input] screen buffer, so when you hit the next read (Console.Read) it reads a new line and converts it to a number.
Instead, one approach is to instead Console.ReadLine
(which returns a string) combined with int.Parse
or the like.
1 Hint: The carriage return character has a value of 13.
Ok, I changed your code a bit and added some explanation because I changed something.
string name, age, favColor;
int num1, num2, sum, mult, subs;
float div;
Console.WriteLine("What is your name? ");
//Start a new line and write ..
name = Console.ReadLine();
//Read the whole line
Console.WriteLine("\nHello, {0}", name);
//{0} stands for the first variable you refer to after the, etc
Console.WriteLine("How old are you? ");
age = Console.ReadLine();
Console.WriteLine("\nSo you are {0}, I thought that you were older!", age);
// something new.. \n refers to a "new line", so instead of writing Console.Writeline(); you can use this
Console.WriteLine("What is your favorite Color? ");
favColor = Console.ReadLine();
Console.WriteLine("{0} is a cool color!\n", favColor);
Console.WriteLine("Nice meeting you, {0}", name);
Console.WriteLine("Have a good day!\n");
Console.WriteLine("Let us do some operations, {0}", name);
Console.WriteLine("Please enter a number: ");
num1 = Convert.ToInt16(Console.ReadLine());
//int.TryParse(Console.ReadLine(), out num1);
//Another way is to "try parse", try converting a string to an integer
Console.WriteLine("\nPlease enter another number: ");
num2 = Convert.ToInt16(Console.ReadLine());
//int.TryParse(Console.ReadLine(), out num2);
//Another way is to "try parse", try converting a string to an integer where out is the returning variable
sum = num1 + num2;
mult = num1 * num2;
subs = num1 - num2;
div = num1 / num2;
Console.WriteLine("\nAlright, {0}", name);
Console.WriteLine("Let us blow up your mind!\n");
//Again use {0} ... which writes easier than having to use + every time,
//its not wrong but its easier this way
Console.WriteLine("{0} + {1} = {2}", num1, num2, sum);
Console.WriteLine("{0} * {1} = {2}", num1, num2, mult);
Console.WriteLine("{0} - {1} = {2}", num1, num2, subs);
Console.WriteLine("{0} / {1} = {2}", num1, num2, div);
Console.WriteLine("\nMindblown, Right?");
Console.ReadLine();
//Console.ReadLine(); at the end to prevent the program from closing
source to share