0

Calculator for Addition

Hey guys, I have just recently started coding in C# and now decided to try a small Programm in form of a calculator for addition with my limited knowledge. I have now put something together and it doesn’t work right. I would appreciate any tips on how I might get it to work or if that to advanced for the start and I should learn more first. Best regards, Luis https://code.sololearn.com/cbcPyCRo9kRH/?ref=app

23rd Jul 2019, 9:21 PM
LuisUP
15 Answers
+ 1
No problem 👍
23rd Jul 2019, 10:22 PM
Faisal
Faisal - avatar
+ 3
faisal all the languages display ints directly why not c#?
23rd Jul 2019, 10:31 PM
Lyna
Lyna - avatar
+ 2
Additionally, because C# can't output ints directly, you would need to convert them to a string before outputting them by using the Convert.ToString() function (similar to the variables). Lastly, to avoid raising any syntax errors, you would need to end off lines 13, 14, and 16 with a semicolon (;) as not having one would raise an error.
23rd Jul 2019, 9:50 PM
Faisal
Faisal - avatar
+ 2
Lyna Dang you're right! Sorry about that haha Seems that I was getting the error from somewhere else 😅 LuisUP The Convert.ToString() function seems to be unnecessary in this case, sorry about that!
23rd Jul 2019, 10:33 PM
Faisal
Faisal - avatar
+ 2
by the way, I wanted to learn c# to create a game. should I learn it like the other languages or just some of it?
23rd Jul 2019, 10:39 PM
Lyna
Lyna - avatar
+ 2
faisal thanks. is it real that I have to learn about photoshop or is it just recommended (to create games)?
24th Jul 2019, 12:07 AM
Lyna
Lyna - avatar
+ 1
I found a couple things wrong: Firstly, on line 12, you seem to have declared a string which doesn't get used within the program. I would recommend deleting that line (for now, at least) to avoid raising any errors. Secondly, a variable name can't be a number, so you would need to change the name of both variables to something that would work a little better (perhaps just one and two would work okay). Along with the variable names, because you're declaring the variables as int and input is always taken in as a string, you would need to convert the variables to an integer to allow them to be stored properly. You can do this by just surrounding Console.ReadLine() within the Convert.ToInt32() function. For your print statement, you have the formatted string set up correctly, but you would need to encase whatever you're outputting within quotation marks (" ") to indicate that it's a string (you would also need to change the variable name to whatever your new names are). (Continued)
23rd Jul 2019, 9:48 PM
Faisal
Faisal - avatar
+ 1
hey, firstly thanks a lot for taking the time. now i think i changed most of the stuff you told me but i didnt quite understand where i should then convert it to a string best regards, Luis
23rd Jul 2019, 10:01 PM
LuisUP
+ 1
You almost got it! Rather than converting the Console.WriteLine() function to an integer, you should instead convert the Console.ReadLine() functions to such when declaring the variables. It should look like this once you've got it: int one = Convert.ToInt32(Console.ReadLine()); (And then you'd just do that for the other variable) As for converting it to a string, you would do that on line 15 where you have both your strings being formatted. So, if you were to just print out the first variable, it would look like this: Console.WriteLine("{0}", Convert.ToString(one));
23rd Jul 2019, 10:06 PM
Faisal
Faisal - avatar
+ 1
i think it works now for the most part but it doesnt calculate it it just says for example 10+10
23rd Jul 2019, 10:16 PM
LuisUP
+ 1
You could fix that by only having one set of curly brackets within your string and add up the variables within one Convert.ToString() function: Console.WriteLine("{0}", Convert.ToString(one+two));
23rd Jul 2019, 10:19 PM
Faisal
Faisal - avatar
+ 1
it works now thanks a lot again, Luis
23rd Jul 2019, 10:22 PM
LuisUP
+ 1
no problem just updated it and works fine
23rd Jul 2019, 10:35 PM
LuisUP
+ 1
Lyna If you're looking to go into creating games with Unity, I would definitely recommend learning as much as you can about C# as a large portion of the scripting aspect of it is with the language.
23rd Jul 2019, 10:55 PM
Faisal
Faisal - avatar
0
You can use directly Console.WriteLine(one + two); This call the overloaded method that take an int as parameter. For convert an int to string you can use the ToString() method (example: (one + two).ToString()) instead of Convert... or, from C# 6, the string interpolation: string answer =
quot;The answer is: {42}";
23rd Jul 2019, 10:55 PM
Eymerich