+ 3

Where is my fault in this code? I want to write my age without using string

static void Main(string[] args) { string yourName; int yourAge = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("What is your name and age?"); //Type your name, press enter and type your age yourName = Console.ReadLine(); yourAge = Console.ReadLine (); Console.WriteLine("Hello {0}; You are {1} years old", yourName, yourAge); }

18th Feb 2017, 7:03 PM
Abdülcelil Arslan
Abdülcelil Arslan - avatar
5 Réponses
+ 3
if thats the case, there's still a way, change ya code to be like string yourname; int yourage; yourname = Console.ReadLine(); yourage = int.Parse(Console.ReadLine()); Console.WriteLine("Name: {0}, Your age is: {1}", yourname, yourage); note that int.Parse is the same as Convert.ToInt32. the problem with your code above is your declared age as an int and almost at the end of the code you enteted a string yourage = Console.ReadLine(); by doing this you were assigning it a string value yet you had declared it as an int
18th Feb 2017, 8:32 PM
Tuchy
Tuchy - avatar
+ 1
the fault with your code is that you declared the age as int and the asked for a first input of age which you converted to an int. the problem comes at the end of the code where you asked for another age which you didn't change to integer. to correct your code just declare age as a string like string yourname; string yourage; yourname = Console.ReadLine(); yourage = Console.ReadLine(); Console.WriteLine("Hello {0}, Your age is: {1}", yourname, yourage);
18th Feb 2017, 7:32 PM
Tuchy
Tuchy - avatar
0
string="Hello" string="You are " int =a//your age string="years old" I don not know very well C# but this i will do in c++. XD
18th Feb 2017, 7:34 PM
Galu Matei Gabriel
Galu Matei Gabriel - avatar
0
@Tuchy is there any way to write it with int? i just want to learn conversion. I have seen an example on SoloLearn and it works and i wanted to insert it to here but i couldn't achieve
18th Feb 2017, 8:05 PM
Abdülcelil Arslan
Abdülcelil Arslan - avatar
0
@Tuchy so Console.ReadLine() is a string. Thank you so much :)
18th Feb 2017, 8:35 PM
Abdülcelil Arslan
Abdülcelil Arslan - avatar