$5 ### Image of the Day Submit IOTD | Top Screenshots ### The latest, straight to your Inbox. Subscribe to GameDev.net's newsletters to receive the latest updates and exclusive content. Sign up now ## need some help with code(noob) Old topic! Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic. 12 replies to this topic ### #1scrap Members Posted 10 April 2012 - 09:13 PM i have a program where if the user types "y" it restarts and if the user types "n" it ends. but for some reason the program also accepts any other uni code character(other then n) to restart the program. how do i make the program end, or ask again for y\n if he inputs something like"3tsd" im using C# ### #2Bacterius Members Posted 10 April 2012 - 09:31 PM Did you try a Console.ReadKey() and checking whether the resulting character is a "y" or a "n" (and possibly their uppercase counterparts)? “If I understand the standard right it is legal and safe to do this but the resulting value could be anything.” ### #3scrap Members Posted 10 April 2012 - 09:34 PM im using a if ReadLine() is n or N or y or Y. and if its Y it restarts if N it ends, but it accepts all characters for restart... what does Console.ReadKey() do?? ### #4Bacterius Members Posted 10 April 2012 - 10:18 PM Console.ReadKey() reads the next character (and only the next character) input into the console and optionally displays it. Then you can check if it's either Y or N, or something else, and respond accordingly. But you should have been able to do exactly the same thing with ReadLine(), so I suspect this is not the problem. Can you show your code? “If I understand the standard right it is legal and safe to do this but the resulting value could be anything.” ### #5scrap Members Posted 11 April 2012 - 04:43 AM  static void Main(string[] args) { //setting variables bool again = true; double price; double priceincrese; string pricein; string again1; //while its true the program runs while (again == true) { //asks for user$ input

Console.Write("please enter the price excluding taxes.");

//if users input is 0 or less, restarts program

while (!double.TryParse(pricein, out price) || price <= 0)

{

Console.WriteLine("ERROR!!! Please enter a valid price!");

}

//calculates the tax and new price

priceincrese = price * .2;

price = price + priceincrese;

Console.Write("the new price is \$");

Console.WriteLine(price);

// asks user for y\n to restart or end program

Console.Write("Do you want to calculate another item? y or n");

//ends

if (again1 == "n" || again1 == "N")

{

again = false;

}

//restarts

if (again1 == "y" || again1 == "Y")

{

again = true;

}

}



### #6Bacterius  Members

Posted 11 April 2012 - 04:56 AM

Ah, I see. Essentially, until the user enters "n" or "N" the variable "again" will always be equal to true. Note that if the user doesn't enter either "n", "N", "y" or "Y", then both if/then conditions are skipped, and "again" is still equal to true (which causes the loop to restart).

What you want is something like:

while (true) do
{
Console.Write("Do you want to calculate another item? y or n");
if (again1 == "n" || again1 == "N")
{
again = false;
break;
}
if (again1 == "y" || again1 == "Y")
{
again = true;
break;
}
}


This way, until the user explicitly chooses Y or N, the program will keep asking for input. The infinite while loop repeats as long as the user doesn't enter Y or N (so if he enters garbage, the program will just ask again). You can customize this depending on your needs, but the basic idea is that you need to keep looping the question until the user enters a valid input before being able to keep going.

If you want the program to directly end if the user doesn't enter Y or N but rather "3djs", you can modify the infinite loop above so that it only checks the Y condition. If the user enters anything other than "y" or "Y", then you know you need to exit, so you can set "again" to false and break out.

Edited by Bacterius, 11 April 2012 - 05:00 AM.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #7scrap  Members

Posted 11 April 2012 - 05:36 AM

Ah, I see. Essentially, until the user enters "n" or "N" the variable "again" will always be equal to true. Note that if the user doesn't enter either "n", "N", "y" or "Y", then both if/then conditions are skipped, and "again" is still equal to true (which causes the loop to restart).

What you want is something like:

while (true) do
{
Console.Write("Do you want to calculate another item? y or n");
if (again1 == "n" || again1 == "N")
{
again = false;
break;
}
if (again1 == "y" || again1 == "Y")
{
again = true;
break;
}
}


This way, until the user explicitly chooses Y or N, the program will keep asking for input. The infinite while loop repeats as long as the user doesn't enter Y or N (so if he enters garbage, the program will just ask again). You can customize this depending on your needs, but the basic idea is that you need to keep looping the question until the user enters a valid input before being able to keep going.

If you want the program to directly end if the user doesn't enter Y or N but rather "3djs", you can modify the infinite loop above so that it only checks the Y condition. If the user enters anything other than "y" or "Y", then you know you need to exit, so you can set "again" to false and break out.

thank you very much )

### #8scrap  Members

Posted 11 April 2012 - 03:03 PM

it still doesnt work with the "breaks"

is there a way so that ReadLine() only accepts certain characters? and if user doesnt put in the right one it asks again? and THEN exacutes the if\then statment after the varialbe is set?

### #9Bacterius  Members

Posted 11 April 2012 - 10:14 PM

What do you mean it still doesn't work? Normally the code I posted:
- restarts if "y" or "Y" is entered
- exits if "n" or "N" is entered
- asks again if anything else is entered

What is it doing for you?

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #10scrap  Members

Posted 12 April 2012 - 04:31 AM

nevermind i fixed it now , with break it ends the loop and closes my program but i made a new line that says, if you dont try Y or n or N or y then it askes again...bu thanks, i learned a new keyword break

### #11Bacterius  Members

Posted 12 April 2012 - 05:02 AM

nevermind i fixed it now , with break it ends the loop and closes my program but i made a new line that says, if you dont try Y or n or N or y then it askes again...bu thanks, i learned a new keyword break

Excellent
Yes break makes you go out of the current loop. It's useful because it often allows to simplify stuff like exclusive conditions. There are other useful keywords like "continue" which directly goes back to the first instruction of the loop and increments the counter if it's a for loop.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #12avelle  Members

Posted 12 April 2012 - 08:26 AM

Hi Scrap, I just wanted to mention something that would hopefully make your life easier in the future. When comparing characters such as y, Y, n, and N, you can force the comparison to be either all lower case or all upper case which would be useful in this instance.

You could turn the following:
if (again1 == "n" || again1 == "N")

into:
if (again.ToUpper() == "N")

and it would take whatever was in the variable 'again' and capitalize it for the comparison. It's important to note that it wouldn't save it as upper case beyond this statement. For that you would need to do
again = again.ToUpper()

### #13scrap  Members

Posted 12 April 2012 - 02:00 PM

Hi Scrap, I just wanted to mention something that would hopefully make your life easier in the future. When comparing characters such as y, Y, n, and N, you can force the comparison to be either all lower case or all upper case which would be useful in this instance.

You could turn the following:

if (again1 == "n" || again1 == "N")

into:
if (again.ToUpper() == "N")

and it would take whatever was in the variable 'again' and capitalize it for the comparison. It's important to note that it wouldn't save it as upper case beyond this statement. For that you would need to do
again = again.ToUpper()