Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# program does not evaluate operations and return wrong answers

Community.

I am learning how to program in C#. I wrote this small program that gets the name, age, favorite color, and two numbers from the user. I use Notepad ++ to write the code and run the C# compiler from the windows command prompt. 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 execute the program almost everything goes alright. However, when the user inputs the first number of the operations, the program skips the second prompt and print a completely different result from the one expected. For example, if I put 0 as the first number the program will jump to the operations and print the following:

//

48+13

48*13

48-13

48/13

Mindblown, right?

//

like image 957
James Avatar asked Aug 04 '26 13:08

James


2 Answers

Do not use Console.Read as it does not do what is expected:

Reads the next character from the standard input stream (and returns the integer value1 that represents it).

Here a good explanation from devshort on why the second call to Console.Read "skips":

If you enter in the value "1" for the first thing, it'll convert that to the ascii representation. Then the carriage return is STILL in the screen [input] buffer, so when you hit the next read (Console.Read) it reads the newline and converts it to a number.

Instead, one approach is to use Console.ReadLine instead (which returns a string) in conjunction with int.Parse or similar ..


1 Hint: the carriage return character has a value of 13.

The ascii that is visually 0 has a byte value of 48. or 0x30. Which explains the 48.

Basically, you're using the wrong function.

like image 38
Patashu Avatar answered Aug 07 '26 01:08

Patashu