#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12

    endless loop for some reason


    The following basic example is creating an endless loop when I put in anything but 1 or 2, whats going on? I just want it to start the program over again if the inputted value isn't 1 or 2. Thanks in advance.

    #include <iostream.h>

    void main()

    {

    bool x;
    int cat = 0;

    do

    {

    cout << "Is cat true? 0 for yes, 1 for no.\n\n";
    cin >> x;

    if (x == 0)
    cout << "You are correct!!\n";
    else if (x == 1)
    cout << "You are not correct :(\n";
    else
    cout << "Type in a valid number please.\n";
    }

    while (x != 0 || x != 1);

    }
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Change
    while (x != 0 || x != 1);
    to
    while (x != 0 && x != 1);
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    C:\Documents and Settings\Administrator\Desktop\C++ files\options\asdf.cpp(15) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'bool' (or there is no acceptable conversion)
    Error executing cl.exe.

    Thats what it says for the cin statement. What should I do?
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Change your declaration of x from
    bool x;
    to
    int x;
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    It works, but now when I type something like "sdafsda" or any character, it says its correct. How do I get back to saying its not correct if anything but 0 and 1 are typed? Modified code:

    #include <iostream.h>

    void main()

    {

    int x;
    int cat = 0;

    do

    {

    cout << "Is cat true? 0 for yes, 1 for no.\n\n";
    cin >> x;

    if (x == 0)
    cout << "You are correct!!\n";
    else if (x == 1)
    cout << "You are not correct \n";
    else
    cout << "Type in a valid number please.\n";
    }

    while ((x != 0) && x (!= 1));

    }
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Maybe you can change x to type char and alter your if statements to compare characters instead of numbers:
    Code:
    #include <iostream.h>
    
    void main()
    {
    
    char x;
    int cat = 0;
    
    do
    
    {
    
    cout << "Is cat true? 0 for yes, 1 for no.\n\n";
    cin >> x;
    
    if (x == '0') 
    cout << "You are correct!!\n";
    else if (x == '1')
    cout << "You are not correct \n";
    else 
    cout << "Type in a valid number please.\n";
    }
    
    while ((x != '0') && (x != '1'));
    }
    Seriously though, if you're just starting to learn C++, you should try learning other concepts before coming to input validation stuff. Also, while you're at it, void main() violates ISO C++ standards (should return int, according to the standard). Check out this link and this link for more details as to why this is so.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    Thanks for your help so far man. But I can't seem to take your suggestions and make my code work. Everything works perfectly unless the user types in a letter. When they type in a letter the program loops endlessly. AHHHH Here is my new code:


    #include <iostream>

    int main()
    {
    using namespace std;


    int x = 1;
    int y;

    do
    {
    cout << "Is x 1 or 2? (If you type anything else then 1 or 2 the program will repeat until a valid value is entered.\n";
    cin >> y;
    cout << "\n";
    if (y == 1)
    cout << "Correct! X is one\n";
    else if (y==2)
    cout << "You are wrong!\n";
    }

    while ((y !=1 && y !=2));
    return 0;
    }
  14. #8
  15. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    ok, i understand why that is happening to u, but exactly how to fix it is tricky. i used to have same problem last year in my cs class. input validation is extremely hard to get correct, at least from my experience. what u need, is a loop inside the do loop that checks the input stream to make sure it is valid. when cin expects an integer, and recieves a character, it sets a flag:
    cin.rdstate() to 2, the value of cin.rdstate() is 0 normally. so perhaps u could create a loop using cin.rdstate()?? if ur not familiar with that, have u ever used cin.good(). it is ezier to use than the above mentioned, someting like this:

    cin >> y;
    while(!cin.good())
    {
    cin.clear();
    cin >> y;
    }

    hope that helps u out
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Hi,

    I'm not sure why your loop goes crazy when the user enters a letter instead of the expected integer, but you can validate your input very easily if you use a char type for the input from the user because a char literal can be anything:
    Code:
    int x=1;
    char answer;
    do
    {
    	cout<<"Is x equal to 1 or 2?"<<endl;
    	cin>>answer;
    	if(answer != '1' && answer != '2')
    	cout<<"Sorry, you must answer with a \"1\" or a \"2\"!"<<endl;
    
    }while(answer != '1' && answer != '2');
    
    cout<<"Thank you for the proper input.\n";
    
    //Now you can continue with the rest of the program
    In the rest of your program, your if statement conditions should test for char literals because the char variable "answer", in the case where the user inputs 1, stores the ASCII code that corresponds to 1 which is 49, so "answer" will not equal 1 but it will equal '1'.

    if(answer == '1') cout<<"You are right!";

    Alternatively, if for some reason you need an integer as the input, after validating the user's input, you can convert it to an integer like this:

    int int_answer=0;
    if(answer == '1') int_answer = 1;
    else int_answer = 2;
    Last edited by 7stud; February 9th, 2003 at 04:33 AM.
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    Thanks man. It works! I couldn't figure out the whole cin.good thing like that other guy said. But thanks for the suggestions.
    -andy

IMN logo majestic logo threadwatch logo seochat tools logo