i need help for a simple thing (beginner qst) | Sololearn: Learn to code for FREE!

+1

i need help for a simple thing (beginner qst)

hey you, yes you... if you go see in my only code on my profile, i ant to reproduce a vending machine. i want that when we choose a thing, we press the number associated to it and then it outputs a text. (using the if statement) the problem is that even if i put a random number, the text will still be display... how can i fix it? thx for your help

9/18/2021 4:28:20 PM

eloufou

7 Answers

New Answer

+1

eloufou I saw a couple issues on your console read statement. First use Console.ReadLine() and second wrap it in Convert.ToInt32() like this: int choice = Convert.ToInt32(Console.ReadLine()); The input comes in from the playground as a string and must be converted to an Integer for the if to work as it is in the code at the time I looked at it. Hope this I helpful 🙂

+2

As I know, we can't use a float variable to compare. Cause programs can't compare the number after the dot. So, you should use integer numbers, then it'll work you see. I hope it helps.

+2

You can use a string to get user choice string choice = Console.ReadLine(); if( choice.StartsWith( "1.1" ) ) // more code follows ...

+2

Thx guys!

+1

eloufou We can't see your code. Please, firstly show your attempt, after that ask what your problem is. Thanks for understanding. Happy coding!

+1

https://code.sololearn.com/c0TNPE071zRQ/#cs i think its good with the link just try to put 1.2

+1

And most importantly: remove the semi-colon after the if condition. Otherwise, the block after it will not count as the block belonging to the if statement 🤓 That is why the text will always be printed. Edit: I just noticed that you have changed the code. I am referring to its original version where you wrote if(choice == 1.1); { }