newb loop question

This is a discussion on newb loop question within the C++ Programming forums, part of the General Programming Boards category; Hi I'm fairly new to c++ programming and have a simple question regarding a piece of code I have written. ...

  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 03: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.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    Quote Originally Posted by StainedBlue View Post
    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.
    No, changing the datatype is masking the real issue here.

    Code:
    #include <iostream>
    #include <limits>
    
    using namespace std;
    
    int main()
    {
    	int switchn = 0;
    
        do
    	{
             cout<<"Please choose conversion type"<<endl;
             cout<<"1.) Ra to RMS\n";
             cout<<"2.) RMS to Ra\n";
             cin>>switchn;
    		 
             system("cls");
    			
    		 //If the read failed.
    		 if(!cin)
    		 {
    			 //Clear the error flags.
    			 cin.clear();
    		 }
    			
    		 //Remove the erroneous input until a new line.
    		 cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    	while (!(switchn == 1) && !(switchn == 2));//end of do-while
    
    
    	cout << switchn;
    	system("pause");
    }

  9. #9
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    >>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.

    Yes, as I admitted above, I skimmed too briefly.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    Quote Originally Posted by StainedBlue View Post
    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.
    This method works pretty well for resolving this particular loop however the next problem I run into is that I use the switchn varaiable as the argument for a switch case later on in the program so I simply run into problems there.

    I found another solution however from a different source which I thought might be interesting to post.

    Code:
    do{             
                 cout<<"Please choose conversion type"<<endl;
                 cout<<"1.) Ra to RMS\n";
                 cout<<"2.) RMS to Ra\n";
                 cin>>switchn;
                 cin.ignore();
                 
                 if(cin.fail()){
                      cin.clear();
                      cin.ignore(numeric_limits<streamsize>::max(), '\n');
                              }//end of if
        
                 system("cls");
              }while (!((switchn == 1)||(switchn == 2)));//end of do-while
    you can see where I added the if statement. Still trying to figure out exactly what it does and how it works but it seems effective.

    Thanks for all your replies!
    Last edited by filio623; 09-18-2009 at 03:56 PM. Reason: wrong code

  11. #11
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    1) Maybe it's just me, but when I expect input from a user at a command prompt, I expect a char or a string, but hey that's just me.

    2)I get the exact same behavior by omitting the cin.fail check, granted I assume a char is being used.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21