+ 1

c# problem please help me

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace methods { class Program { static void Main(string[] args) { Console.WriteLine("Enter your ferst num"); int x = Convert.ToInt32(Console.ReadLine()); bool Cansul; char A = '*' ; char B = '/' ; char C = '+' ; char D = '-' ; Console.WriteLine("but * or / or + or -"); char a =Convert.ToChar( Console.Read()); do { if (a == A) { Cansul = false; } else if (a == B) { Cansul = false; } else if (a == C) { Cansul = false; } else if (a == D) { Cansul = false; } else { Cansul = true ; Console.WriteLine("you must but * or / or + or - "); a =Convert.ToChar( Console.Read()); } } while (Cansul); Console.WriteLine("Enter your second num"); // here the error :( i can't fix it . int y = Convert.ToInt32(Console.ReadLine()); /* char A = '*' ; char B = '/' ; char C = '+' ; char D = '-' ; */ int Z; if (a == A) { Z = x * y ; } else if (a == B) { Z = x / y; } else if (a == C) { Z = x + y; } else if (a == D) { Z = x - y; } else { Z = 0; Console.WriteLine("error"); } Console.WriteLine(value: Z); Console.WriteLine("any key to End :) "); Console.Rea

31st Jan 2018, 8:24 AM
Ahmad Akil
Ahmad Akil - avatar
2 Answers
+ 4
The code seems to be fine. The only problem I found was. Console.ReadKey The ReadKey statement itself works fine but it gives problems in the next line. Which is "Enter your second number". It looks like Console.ReadKey leaves a carriage-return character for the next input and this next input only gets this carriage-return character instead of the actual input. int y = Convert.ToInt32(Console.ReadLine()); is fine. I changed readkey to readline and now it works fine. The input I used was 1 + 2 https://code.sololearn.com/cVHOr0F2dx0A
31st Jan 2018, 8:36 AM
sneeze
sneeze - avatar
+ 1
Console.WriteLine("but * or / or + or -"); // here is problem with ReadKey ==> Read char a =Convert.ToChar( Console.ReadKey()); Console.WriteLine("Enter your second num"); //but here i dont know what to do int y = Convert.ToInt32(Console.ReadLine());
31st Jan 2018, 6:00 AM
Ahmad Akil
Ahmad Akil - avatar