+ 4

Calculator help

I'm stuck x.x not really sure how to get this to work. I'm sure it's syntax error but I'm still pretty new to programming. Suggestions? https://code.sololearn.com/cT63oEGdjke0/?ref=app

6th Feb 2018, 2:21 PM
Dutch
Dutch - avatar
7 Answers
+ 11
Let's see, your program going to perform computation on 3 inputs: āœ… value of x āœ… value of y āœ… operator The code missed out the 3rd input, which is the operator going to use and the value used for comparison in switch. šŸ“¢ UPDATE The numeric input need to be converted into integers/decimals as well for arithmetic calculation. Besides, take note that user input is always in string so you'll need to use string literals for each of the switch case as well. Hopefully it helps! šŸ˜‰
6th Feb 2018, 2:53 PM
Zephyr Koo
Zephyr Koo - avatar
+ 4
@Schindlabua you're so much help! Thank you! Now about the Convert.To stuff...I keep getting an exception error saying something about parsing? I've done this before but I'm a bit out of practice and also this isn't JS or CPP lol x.x what can I do yo fix this new one?
6th Feb 2018, 5:52 PM
Dutch
Dutch - avatar
+ 4
Got it! The problem was in how it was to be entered. Return must be pressed after each entry! ex: 1 + 1 Seems kinda odd...any way to be able to enter it in a single line? Thank you all again for the help!
7th Feb 2018, 12:28 AM
Dutch
Dutch - avatar
+ 3
Lots of things! I guess you want x and y to be the numbers you enter, right? Here a problem is that Console.ReadLine gives you text, not a number, so you'd need to convert it like double x = Convert.ToDouble(Console.ReadLine()); Next, you check for what you want to calculate (+-*/) but you never asked the user for that. You let the user input x and y but not the operator! So you have an empty `switch` and this gives you a syntax error. Neeext, your print statements want to look like case "+": Console.WriteLine("Sum is {0}", x+y); You have a stray "+" there and the compiler doesn't know what to do with it, so you get more syntax errors. You can also remove the empty `x+y` statement right after the case. You want to print "{0}" in all the cases, it's not like a counter that counts up. Hope that helps.
6th Feb 2018, 3:00 PM
Schindlabua
Schindlabua - avatar
+ 3
hmmm made changes but it still isnt working quite right x.x
6th Feb 2018, 3:22 PM
Dutch
Dutch - avatar
+ 3
Convert.ToOperator, nice :D Small detour: Say you have two numbers, and print their sum. double a = 123, b = 321; Console.WriteLine("a + b = {0}", a+b); It will print 444, as expected. Of course you can also add text. string a = "summer", b = "fun"; Console.WriteLine("a + b = {0}", a+b); It will print summerfun, as expected. What happens if the text happens to contain numbers? string a = "123", b = "321"; Console.WriteLine("a + b = {0}", a+b); It will print 123331, probably not as expected. That's why programmers are so stingy about types and type conversion. Of course the only thing you do with your operator is compare it to "+", "-", etc, so text is the way to go. string n = Console.ReadLine(); is totally enough. Convert.ToOperator doesn' even exist :P Also remove the "x+y;"-like statements immediately after the case, like I wrote in my example above. "default" needs a colon (:) like case, not a semicolon (;).
6th Feb 2018, 3:34 PM
Schindlabua
Schindlabua - avatar
+ 3
You've kind of figured out the problem, ReadLine will read a whole line, and you do that 3 times. If you're ok with having a space in between each part of your input, like "1 + 2" and not "1+2" you can do it in one line pretty easily. First you read the line, and split it into an array wherever you encounter a space, like var parts = Console.ReadLine().Split(' '); Then you parse the parts instead of seperate lines. var x = Convert.ToDouble(parts[0]); var n = parts[1]; var y = Convert.ToDouble(parts[2]);
7th Feb 2018, 8:37 AM
Schindlabua
Schindlabua - avatar