Sign in to follow this  
Followers 0
scrap

need some help with code(noob)

12 posts in this topic

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#
0

Share this post


Link to post
Share on other sites
Did you try a Console.ReadKey() and checking whether the resulting character is a "y" or a "n" (and possibly their uppercase counterparts)?
0

Share this post


Link to post
Share on other sites
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??
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
[CODE]
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;
}


}
[/CODE]
0

Share this post


Link to post
Share on other sites
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:

[CODE]
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;
}
}
[/CODE]

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
1

Share this post


Link to post
Share on other sites
[quote name='Bacterius' timestamp='1334141811' post='4930201']
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:

[CODE]
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;
}
}
[/CODE]

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.
[/quote] thank you very much :))
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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 :)
1

Share this post


Link to post
Share on other sites
[quote name='scrap' timestamp='1334226713' post='4930536']
nevermind i fixed it now [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img], 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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/quote]
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.
0

Share this post


Link to post
Share on other sites
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:
[code]if (again1 == "n" || again1 == "N")[/code]

into:
[code]if (again.ToUpper() == "N")[/code]

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 [code]again = again.ToUpper()[/code]

I hope that's helpful.
1

Share this post


Link to post
Share on other sites
[quote name='avelle' timestamp='1334240800' post='4930577']
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:
[code]if (again1 == "n" || again1 == "N")[/code]

into:
[code]if (again.ToUpper() == "N")[/code]

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 [code]again = again.ToUpper()[/code]

I hope that's helpful.
[/quote]yes! thank you. always learning more everyday.


thank you all for your replies :)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0