Thread: newb loop question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    newb loop question

    Hi

    I'm fairly new to c++ programming and have a simple question regarding a piece of code I have written.

    I set up a do while loop as part of a simple menu that will repeat the menu if the user doesn't either enter a one or a two (the only choices available).

    Below is the syntax for this code.

    Code:
     do{
                 cout<<"Please choose conversion type"<<endl;
                 cout<<"1.) Ra to RMS\n";
                 cout<<"2.) RMS to Ra\n";
                 cin>>switchn;
                 cin.ignore();
        
                 system("cls");
              }while (!((switchn == 1)||(switchn == 2)));//end of do-while
    It works great if the user enters in a wrong number, however if he enters any sort of other character like a letter then it just goes into a neverending loop that I have to force quit.

    Is there a way to set it up so that if someone enters a letter by accident it will just go throught the loop once and not continuously uninteruupted?

    Thanks

  2. #2
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    See De Morgan's laws

    Your condition is actually saying, "Anything but the two I've specified."

    Which results in an infinite loop.

    Also, just let your program scroll and get rid of
    Code:
    system("cls");
    Last edited by StainedBlue; 09-18-2009 at 02:01 PM.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    Quote Originally Posted by StainedBlue View Post
    See De Morgan's laws

    Your condition is actually saying, "Anything but the two I've specified."

    Which results in an infinite loop.
    OK I guess my question is this.
    When the user enters in a wrong number such as 5 the program will loop through but stop at the cin command until it gets another input.

    however if the user enters a letter like "y" the program will loop again but will bypass the cin command and just continue the loop forever by itself.

    Why is this and how do I fix it?

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    Quote Originally Posted by filio623 View Post
    OK I guess my question is this.
    When the user enters in a wrong number such as 5 the program will loop through but stop at the cin command until it gets another input.

    however if the user enters a letter like "y" the program will loop again but will bypass the cin command and just continue the loop forever by itself.

    Why is this and how do I fix it?
    You attempt to read a character into an integer which sends cin into a fail state:

    std::cin.fail() is equal to true.

    You must first call cin.clear() to remove the error flag before you can read in any more data.

    Consider using a switch control statement within your loop.

  5. #5
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    >>Consider using a switch control statement within your loop.

    or learn about De Morgan's laws and fix this the way it should be.

    and yes, if you're expecting a response from the user, make sure you're looking for a "char".

  6. #6
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Don't i feel silly.

    1) use a char for "switchn"

    2) look at specifically:

    Code:
    switchn == 1
    should be:

    Code:
    switchn == '1'
    You're logical condition is actually correct.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    Quote Originally Posted by StainedBlue View Post
    >>Consider using a switch control statement within your loop.

    or learn about De Morgan's laws and fix this the way it should be.

    and yes, if you're expecting a response from the user, make sure you're looking for a "char".
    De Morgan's laws have nothing to do with the infinite loop, the loop condition is correct. As I stated above, the reason for the infinite loop is the cin statement is being skipped while it remains in the error state. Therefore the value in switchn remains as a value other than 1 or 2.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Please, Beginner Question, rewrite loop
    By office888 in forum C Programming
    Replies: 4
    Last Post: 12-11-2006, 10:07 AM
  2. for loop question
    By cdalten in forum C Programming
    Replies: 4
    Last Post: 03-20-2006, 09:42 AM
  3. Complete Newb, have a loop that won't exit
    By jason414 in forum C++ Programming
    Replies: 5
    Last Post: 02-18-2006, 09:38 AM
  4. for ..loop newb question
    By mike71484 in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2006, 11:30 PM
  5. newb question on ARRAYS
    By viciousv322 in forum C++ Programming
    Replies: 18
    Last Post: 11-16-2005, 07:34 PM