0

I am teaching myself c# with the help of a few books and I got curious so I tried building a small console app to get two numbers from user add them together and using an if else statement depending on the result print one of the messages but my code asks for the second number and prints the else statement and the press any key to continue all at the same time without getting my second number or doing the math

    using System;

namespace helloworldwcond
{
    class Program
    {
        public static void Main(string[] args)
        {
            int a;
            int b;
            int c;
            Console.Write("Enter a Number: ");
            a = Console.Read ();
            Console.Write ("Enter another Number: ");
            b = Console.Read ();
            c = a + b;

            if (c == 7) {
                Console.WriteLine("Hello World!");
            } else {
                Console.Write("We Are DOOMED");
            }



            // TODO: Implement Functionality Here

            Console.Write ("Press any key to continue . . . ");
            Console.ReadKey (true);
        }
    }
}
Matieus27
  • 3
  • 3

4 Answers4

1

Console.Read returns the next character as an integer. So a 0 is ASCII 48, so entering a 0 will return 48. Use Convert.ToInt32() to convert a character to the integer value.

a = Convert.ToInt32(Console.Read());

Of course you may want to also add checking that the character typed is a number. And this will only work for 1 digit numbers.

Either way, probably want to do a Console.ReadLine() instead which will return a string of everything typed until they hit enter. Then convert the string to a number as int.Parse(string value)

Console.Write("Enter a Number: ");
string temp = Console.ReadLine();
a = Int32.Parse(temp);  // Or could use
                        bool isThisANumber = Int32.TryParse(temp, a);
Mike
  • 335
  • 4
  • 20
  • Whao. Still adding more info. 30 seconds to -1 me. Woot. – Mike Oct 03 '15 at 13:53
  • `Convert.ToInt32` is same as `int.Parse` it throws exception if the input string is not valied – sujith karivelil Oct 03 '15 at 14:00
  • The read method really reads just a character, so you would need to type "34". Not 3 then enter then 4, as the enter key would be processed as the second character. – Mike Oct 03 '15 at 14:03
  • `int.Parse` is a thousand times more readable when doing actual parsing though. `Convert.ToInt32` has a huge amount of overloads, each behaving differently and converting objects of different types.. `int.Parse` has one purpose, and one purpose only: to parse strings and return the result as an integer. – sara Oct 03 '15 at 14:07
  • However your first paragraph is incorrect. Convert.ToInt32() does not convert a character to its integer value. – Maor Veitsman Oct 03 '15 at 14:19
1

The problem is the way you get your input. You should use Console.ReadLine() instead of Console.Read(). The correct code should be:

    int a;
    int b;
    int c;
    Console.Write("Enter a Number: ");
    a = Int32.Parse(Console.ReadLine());
    Console.Write("Enter another Number: ");
    b = Int32.Parse(Console.ReadLine());
    // The rest are the same

Why use Console.ReadLine() instead of Console.Read()?

  1. Console.Read() return the ASCII code of the input character. For example, you enter 4 for a, then what you get is a = 52, not a = 4, because 52 is the ASCII code of character 4.
  2. Console.Read() only read 1 character from the input stream. So, when you enter value for a, you type: 3 enter. At that time, your input stream has 2 characters: 3 and enter. Then, when you call Console.Read() for b, it will read the next character, which is enter, and go on. It's the reason for the behavior of your program.

In conclusion, you should use Console.ReadLine() to read a line, then parse it as you want.

To know more about the difference between that 2 functions, please read this post.

Community
  • 1
  • 1
Triet Doan
  • 11,455
  • 8
  • 36
  • 69
  • This one worked thank you very much. I imagine if I would have made it through my books a little farther before experimenting I would have been able to do this much easier. thank you very much for the help. – Matieus27 Oct 03 '15 at 14:55
0

As answered by Mike above, Console.Read returns the ASCII value of the first character in the input. You could use Console.ReadLine instead and parse the string as an integer. Mike has suggested as well while I was posting this.

using System;

namespace helloworldwcond
{
    class Program
    {
        public static void Main(string[] args)
        {
            int a;
            int b;
            int c;
            Console.Write("Enter a Number: ");
            a = int.Parse(Console.ReadLine());
            Console.Write("Enter another Number: ");
            b = int.Parse(Console.ReadLine());
            c = a + b;

            if (c == 7)
            {
                Console.WriteLine("Hello World!");
            }
            else
            {
                Console.Write("We Are DOOMED");
            }

            // TODO: Implement Functionality Here

            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}
Alex
  • 21,273
  • 10
  • 61
  • 73
-2

Console.Read returns the ascii value of the input given. For a quick and dirty solution try this:

        int a;
        int b;
        int c;
        Console.Write("Enter a Number: ");
        a = Console.Read() - '0';
        Console.Write("Enter another Number: ");
        b = Console.Read() - '0';
        c = a + b;

        if (c == 7)
        {
            Console.WriteLine("Hello World!");
        }
        else
        {
            Console.Write("We Are DOOMED");
        }
Maor Veitsman
  • 1,544
  • 9
  • 21