0

I am currently a complete beginner to C# however I am having this issue and as a python user the error doesn't make any sense to me. I have tried to find a fix but I don't know what to search for or how to describe the error, so please help!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace testin
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintState();
            CalculateAge();
            EzMaths();
            //Here I call the procedure which is declared later on
            Console.ReadLine();
        }
        static void PrintState()
        {
            /*Here I write the procedure of 'PrintState',
             * or print statement. The program will print "hey world"
             * to the console as a result. */
            Console.WriteLine("Hey world");
        }

        static void EzMaths()
        {
            Console.WriteLine("Enter the first number ");
            Console.Write("> ");
            int num1 = Console.Read();
            Console.WriteLine("Enter the second number ");
            Console.Write("> ");
            int num2 = Console.Read();
            int total = (num1 + num2);
            Console.WriteLine(total);
        }


        static void CalculateAge()
        {
            Console.WriteLine("Enter your year of birth ");
            Console.Write("> ");
            int date = Console.Read();
            int age = (2018 - date);
            Console.Write("You are " + (age));
            Console.ReadLine();


        }


    }
}
  • `Console.Read` returns an integer, but does not *read* an integer, as the [fine manual](https://learn.microsoft.com/dotnet/api/system.console.read) explains. [See also](https://stackoverflow.com/q/24443827/4137916). – Jeroen Mostert Nov 05 '18 at 12:41
  • `Console.Read()` returns an integer but it is the character code of the next character read. For instance, if you type a zero, `0`, it has a character code of 48. That's what you'll get in return. – Lasse V. Karlsen Nov 05 '18 at 12:41
  • You need to use `Console.ReadLine()` and then parse the value using a method such as `int.TryParseValue()` to convert the string read from user input into a number. – Martin Costello Nov 05 '18 at 12:42
  • Console.Read will return a character then you need to tryparse before accessing it – Ranjit Singh Nov 05 '18 at 12:43
  • you need to some convertion to integer on you `Console.Read()`, ex `Convert.ToInt32(Console.ReadLine());` – styx Nov 05 '18 at 12:43

2 Answers2

3

Console.Read(); does read the next Character, but as its int representation. You'll need to use Console.ReadLine();, but now you're faced with a different problem, Console.ReadLine(); returns a string and not a int, so now you need to Convert it. In the .NET World you'd do it like this:

string userInput = Console.ReadLine();
int userInputNumber = Convert.ToInt32(userInput);

This is not safe, as the user could enter something that's not a number and that would make the program crash. Of course if you're just getting started and making a "Hello World" application this is probably not your biggest concern.
I'll post the better version anyways if yo're interested:

string userInput = Console.ReadLine();
int userInputNumber;
if (int.TryParse(userInput, out userInputNumber))
{
    // Do code here with userInputNumber as your number
}

And in C# (like every modern Language) we have some Syntatic Sugar to make this shorter but also harder to read:

if (int.TryParse(Console.ReadLine(), out int userInputNumber))
{
    //Do code here
}
MindSwipe
  • 7,193
  • 24
  • 47
  • 1
    It's worth pointing out that initializing the variable to 0 does nothing: `out` will always set the value, even if parsing fails (and the compiler knows this, and will not give "use of uninitialized variable" errors). – Jeroen Mostert Nov 05 '18 at 13:04
1

you need to use console.ReadLine() instead of console.Read() in your CalculateAge function. refer to this answer to understand the difference between Console.Read and console.ReadLine: Difference between Console.Read() and Console.ReadLine()?

Yet I would suggest that you also add a tryParse in order to validate if the entered value is an integer before you convert it to int

something like this:

    static void CalculateAge()
    {
        Console.WriteLine("Enter your year of birth ");
        Console.Write("> ");
        string input = Console.ReadLine();
        int date = 2018;
        Int32.TryParse(input, out date);
        int age = (2018 - date);
        Console.Write("You are " + (age));
        Console.ReadLine();
    }

you can have another implementation if you dont want it to have a default value of 2018. like add a while loop, while tryParse != false then keep asking for input.

themehio
  • 38
  • 7
  • 3
    `Convert.ToInt32` is a poor choice here. Never trust user input. It may not be a number at all. And then it would be good to explain to OP why you are using it. – Fildor Nov 05 '18 at 12:50