Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[C++] If else infinite loop?

loop

  • Please log in to reply
20 replies to this topic

#13 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 06 April 2009 - 08:02 AM

Try this:
//Menu Chooser
//demostrates switch statement

#include <iostream>
using namespace std;

int main()
{
  bool valid=false;
  cout << " Is CodeCall A Great Site\n\n";
  cout << "1 -- Yeah Totally!\n";
  cout << "2 -- Not Sure..\n";
  cout << "3 -- No, it's an awful site!\n";
  
  //TryAgain: //this is a kind of pin where you can come back to later in the code
  while (!valid) 
  {         
    valid=true;       
    char choice;
    cout << "Choice: ";
    choice = cin.get();   


    switch (choice)
    {
      case '1':
        cout << "You're Totally Right!!\n\n";
        break;
      
      case '2':
        cout << "I'm Sorry But If You Knew The Forum You Wouldn't Have Doubts b\n\n";
        valid=false;     //Here the loop will be transported back to the ping 'TryAgain'
        break;
      
      case '3':
        cout << "Are You Serious?! I'm Sorry That Can't Be Correct...\n\n";
        valid=false; 
        break;
      
      default:
        cout << "Sorry you must answer a number from 1 to 3\n";
            //cin.get(choice, 1);
        valid=false;    
    }
  }                  
  
  system("pause"); //Pause the program otherwise the program will shut down
  return 0;

}

  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#14 Spider7

Spider7

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 06 April 2009 - 08:36 AM

Yaa! It works now!

but I find it weird I declare a char while I want an integer answer.

And also another downside with declaring a char if I enter a String(or more than 1 character it gives the notification of "Sorry you must answer a number from 1 to 3"
but it gives the notification as many times, as there are character.

so when I would enter 'hello' It will give the notification 5 times.
first it'll say 'Sorry you must answer a number from 1 to 3'
and after that it says the following for times this:
Choice : Sorry you must answer a number from 1 to 3

How to solve that problem :P


You can use int but need to handle the error conditions.

Basically, when cin gets an illegal character, it got into error state and won't move forward in the input stream. Your program needs to clear the error (function is clear()) and move the position forward (function is ignore()). Post back if you have trouble get it working.
  • 0

#15 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 06 April 2009 - 08:57 AM

You can use int but need to handle the error conditions.

Basically, when cin gets an illegal character, it got into error state and won't move forward in the input stream. Your program needs to clear the error (function is clear()) and move the position forward (function is ignore()). Post back if you have trouble get it working.


I Hope I understood what you ment and I did this.

//Menu Chooser
//demostrates switch statement

#include <iostream>
using namespace std;

int main()
{
  bool valid=false;
  cout << " Is CodeCall A Great Site\n\n";
  cout << "1 -- Yeah Totally!\n";
  cout << "2 -- Not Sure..\n";
  cout << "3 -- No, it's an awful site!\n";
  
  //TryAgain: //this is a kind of pin where you can come back to later in the code
  while (!valid) 
  {         
    valid=true;       
    char choice;
    cout << "Choice: ";
    cin >> choice;  //get here the input from the user
    cin.ignore(256, '\n'); //This is used to discard 255 values(numbers, letters) or the \n whatever comes first
    


    switch (choice)
    {
      case '1':
        cout << "You're Totally Right!!\n\n";

        break;
      
      case '2':
        cout << "I'm Sorry But If You Knew The Forum You Wouldn't Have Doubts b\n\n";
        valid=false;     //Here the loop will be transported back to the ping 'TryAgain'
        break;
      
      case '3':
        cout << "Are You Serious?! I'm Sorry That Can't Be Correct...\n\n";
        valid=false; 
        break;
      
      default:
        cout << "Sorry you must answer a number from 1 to 3\n";
           [B][COLOR="Red"]cin.clear();[/COLOR][/B]
        valid=false;    
    }
  }                  
  
  system("pause"); //Pause the program otherwise the program will shut down
  return 0;

}

I think you ment this?

By the way this also clears the problem with reading the whole string! when I enter number 2 in the fourth place it will still say 'Sorry you must answer a number from 1 to 3' So I think my problem is fixed now....?

Thanks everyone for all the help!! couldn't find the answer without the help of you guys!!
  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#16 Spider7

Spider7

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 06 April 2009 - 11:05 AM

Close. I'm thinking the code like this:
int 	input;
while (!(std::cin >> input)) {
    std::cin.clear();		// clear the error state
    std::cin.ignore(256, '\n');  // clear out the existing input
    cout >> "please enter a number....\n";
}

  • 0

#17 Korsicall

Korsicall

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 06 April 2009 - 07:11 PM

I'm just curious, why do you have the boolean set up like that?

If you do this, you don't change it back and forth.

int main()
{
  bool valid=true;
  cout << " Is CodeCall A Great Site\n\n";
  cout << "1 -- Yeah Totally!\n";
  cout << "2 -- Not Sure..\n";
  cout << "3 -- No, it's an awful site!\n";
  
  //TryAgain: //this is a kind of pin where you can come back to later in the code
  while (valid) 
  {         
    char choice;
    cout << "Choice: ";
    cin >> choice;  //get here the input from the user
    cin.ignore(256, '\n'); //This is used to discard 255 values(numbers, letters) or the \n whatever comes first
    


    switch (choice)
    {
      case '1':
        cout << "You're Totally Right!!\n\n";
        valid = false;
        break;

      case '2':
        cout << "I'm Sorry But If You Knew The Forum You Wouldn't Have Doubts b\n\n";
          break;
      
      case '3':
        cout << "Are You Serious?! I'm Sorry That Can't Be Correct...\n\n";
          break;
      
      default:
        cout << "Sorry you must answer a number from 1 to 3\n";
           cin.clear();
      }
  }                  
  
  system("pause"); //Pause the program otherwise the program will shut down
  return 0;

}

Not trying to nitpick, I'm just curious if there is something that I missed.
  • 0

#18 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 06 April 2009 - 10:34 PM

I'm just curious, why do you have the boolean set up like that?

If you do this, you don't change it back and forth.

int main()
{
  bool valid=true;
  cout << " Is CodeCall A Great Site\n\n";
  cout << "1 -- Yeah Totally!\n";
  cout << "2 -- Not Sure..\n";
  cout << "3 -- No, it's an awful site!\n";
  
  //TryAgain: //this is a kind of pin where you can come back to later in the code
  while (valid) 
  {         
    char choice;
    cout << "Choice: ";
    cin >> choice;  //get here the input from the user
    cin.ignore(256, '\n'); //This is used to discard 255 values(numbers, letters) or the \n whatever comes first
    


    switch (choice)
    {
      case '1':
        cout << "You're Totally Right!!\n\n";
        valid = false;
        break;

      case '2':
        cout << "I'm Sorry But If You Knew The Forum You Wouldn't Have Doubts b\n\n";
          break;
      
      case '3':
        cout << "Are You Serious?! I'm Sorry That Can't Be Correct...\n\n";
          break;
      
      default:
        cout << "Sorry you must answer a number from 1 to 3\n";
           cin.clear();
      }
  }                  
  
  system("pause"); //Pause the program otherwise the program will shut down
  return 0;

}

Not trying to nitpick, I'm just curious if there is something that I missed.


Well you did miss somenthing....
First of all you missed the #include <iostream>
and adfter that you forgot the using namespace std; ....

Naah joking I'm just messing with you :P

Well I tried your code and I didn't see anything wrong with it!
But you just made the boolean true.. and it has to be proven false?(am I right or do I totally miss your point?)

I think there is nothing wrong with it just a little weird that the right answer is a false boolean. because I want to say that answer 1 is right and you make valid = false than.. I think when you do it the other way arounds so make the boolean at the start false and than prove it true is a better way to say the same as you only more logical..
  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#19 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 06 April 2009 - 10:35 PM

Close. I'm thinking the code like this:

int 	input;
while (!(std::cin >> input)) {
    std::cin.clear();		// clear the error state
    std::cin.ignore(256, '\n');  // clear out the existing input
    cout >> "please enter a number....\n";
}


Oke I didn't understand what you ment :P.

but I don't understand the error state.. why do you clear it at the start?
Could you maybe explain that because I don't understand why you would do that?

Edited by SterAllures, 07 April 2009 - 12:37 AM.

  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#20 Korsicall

Korsicall

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 07 April 2009 - 10:40 AM

Ah, I see, I don't really read the names of the variables. I just read into what they do, so I didn't read the name "valid", and yes, it does make more sense with what was there before.
  • 0

#21 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 09 April 2009 - 01:06 PM

Ah, I see, I don't really read the names of the variables. I just read into what they do, so I didn't read the name "valid", and yes, it does make more sense with what was there before.



Oke ;).
Well It's really good way tho! like the way you do it ;)
  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download