need some help with code(noob)

Started by
11 comments, last by scrap 12 years ago
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#
Advertisement
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.”

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??
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.”


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.");
pricein = Console.ReadLine();

//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!");
pricein = Console.ReadLine();
}
//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");
again1 = Console.ReadLine();
//ends
if (again1 == "n" || again1 == "N")
{
again = false;
}
//restarts
if (again1 == "y" || again1 == "Y")
{
again = true;
}


}
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");
again1 = Console.ReadLine();
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.

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


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");
again1 = Console.ReadLine();
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 :))
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?
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.”

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 :)

This topic is closed to new replies.

Advertisement