-5

I am trying to make an example formula console calculator and can't find out how to convert the output of the Console.ReadLine() to a integer. Here is a sample of code that I tried.

    int A;
    Console.WriteLine("What is number A?");
    String numA = Console.ReadLine();
    Convert.ToInt32(numA) == A;
Dorad
  • 3,413
  • 2
  • 44
  • 71
  • 2
    Well you're calling a reasonable method (although I'd use `int.Parse`) but you need to *assign* the value to `A`. I would suggest at this point reading an introductory book on C#.... Stack Overflow isn't a good place to learn the basics of a language. – Jon Skeet Nov 27 '16 at 21:52
  • `Convert.ToInt32(numA) == A;` converts the value entered (`string`) into `int`, then `==` operator compares it with *garbage* (`A` is not initialize) so you have `bool` value which you *throw away* - `;` – Dmitry Bychenko Nov 27 '16 at 22:10

3 Answers3

1

You can implement it like this:

int A;

do {
  Console.WriteLine("What is number A?");
}
while (!int.TryParse(Console.ReadLine(), out A));

keep asking user until he/she enters the integer value

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0
int.Parse(numA)

or...

int i;
if (int.TryParse(numA, out i) {
    // parsed correctly
}
jleach
  • 7,410
  • 3
  • 33
  • 60
-1

Try this

int A;
 Console.WriteLine("What is number A?");
 String numA = Console.ReadLine();
A = Int32.Parse(numA);`
Yanga
  • 2,885
  • 1
  • 29
  • 32