Jump to content

  • Log In with Google      Sign In   
  • Create Account

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.

  • You cannot reply to this topic
12 replies to this topic

#1 scrap   Members   -  Reputation: 140

Like
0Likes
Like

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#

Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 8947

Like
0Likes
Like

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

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 scrap   Members   -  Reputation: 140

Like
0Likes
Like

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

#4 Bacterius   Crossbones+   -  Reputation: 8947

Like
0Likes
Like

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?

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#5 scrap   Members   -  Reputation: 140

Like
0Likes
Like

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.");

			    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;

			    }

			   

			   

		    }



#6 Bacterius   Crossbones+   -  Reputation: 8947

Like
1Likes
Like

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");
  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.

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

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#7 scrap   Members   -  Reputation: 140

Like
0Likes
Like

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

#8 scrap   Members   -  Reputation: 140

Like
0Likes
Like

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?

#9 Bacterius   Crossbones+   -  Reputation: 8947

Like
0Likes
Like

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?

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#10 scrap   Members   -  Reputation: 140

Like
1Likes
Like

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

#11 Bacterius   Crossbones+   -  Reputation: 8947

Like
0Likes
Like

Posted 12 April 2012 - 05:02 AM

nevermind i fixed it now Posted Image, 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 Posted Image

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.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#12 avelle   Members   -  Reputation: 108

Like
1Likes
Like

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

I hope that's helpful.

#13 scrap   Members   -  Reputation: 140

Like
0Likes
Like

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

I hope that's helpful.

yes! thank you. always learning more everyday.


thank you all for your replies :)




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.



PARTNERS