I don't understand why my program is not working properly. If I enter 10, then the program will give me not 11 but 101, why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I don't understand why my program is not working properly. If I enter 10, then the program will give me not 11 but 101, why?

https://code.sololearn.com/c2novHVl1m1i/?ref=app

22nd May 2022, 9:57 AM
Иван Литовченко
8 Answers
+ 4
Иван Литовченко , the variable x is string, so the operation is string concatenation => "10" + 1 => "101". You can declare x as int => int x =int.Parse(Console.ReadLine ()).
22nd May 2022, 10:07 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 2
Иван Литовченко , you didn't assign it to a variable => int y = Convert.ToInt32 (x);
22nd May 2022, 10:14 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
But I wrote Convert.Toint32, he had to convert x to int ?
22nd May 2022, 10:12 AM
Иван Литовченко
+ 1
int x = Convert.ToInt32(Console.ReadLine());
22nd May 2022, 9:46 PM
Maryam
Maryam - avatar
+ 1
static void Main(string[] args) { int x = int.Parse(Console.ReadLine()); x+=1; Console.WriteLine (x); }
23rd May 2022, 7:27 PM
joshua
joshua - avatar
+ 1
Instead of making x a string make it a int Get the input from user , since its a string type cast it to a int with the int.Parse() method .
23rd May 2022, 7:29 PM
joshua
joshua - avatar
0
String concatenation is happening when u are printing the results with the + 1 which is why when u enter 10 printts out the 1 appended to it instead add the one before print statement
23rd May 2022, 7:20 PM
joshua
joshua - avatar
- 2
SoloProg ,And separately the convert.tu command without inserting it into any method cannot be obtained?
22nd May 2022, 2:13 PM
Иван Литовченко