Thread: I am stuck in a loop!!!

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1

    I am stuck in a loop!!!

    Here is the problem. I am using a switch and I want the user to pick a number (1-6), and if he enters a letter or any other character then it will go to the default and tell them they entered the wrong choice and to chose again which will run the function that allows them to pick again. I have all of that working, but my problem is when I call the function in the default it gets stuck in a loop because the value in the varaiable that caused the loop is still in there. Can someone help and get me out this loop.

    Thanks

    Here is the code I have:



    #include<iostream.h>
    class finalswitch
    {
    private:
    int value;
    public:
    void set();
    void newcode();
    };

    void finalswitch::set ()
    {
    cout<<"Enter a value"<<endl;
    cin>>value;

    switch(value)
    {
    case 1:
    cout<<"This is 1";
    break;
    case 2:
    cout<<"This is 2";
    break;
    case 3:
    cout<<"This is 3";
    break;
    case 4:
    cout<<"This is 4";
    break;
    case 5:
    cout<<"This is 5";
    break;
    case 6:
    cout<<"This is 6";
    break;

    default:
    set();

    }
    }

    void main()
    {
    finalswitch aswitch;
    aswitch.set ();

    }

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Try putting this in the code before any input is taken.

    cin.clear();//clears the input bits that tell if input was good
    cin.ignore(80,'\n')//ignores first 80 char or newline, whichever comes first



    The reason this was happening was that the input was still in the buffer. Therefore, when the program went to retrieve input, it thought it already had it. Then, it proceded to continue calling the default case because the input in the buffer still wasn't 1 through 6.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    try running this program multiple times, inputting diferent values for value. Make some values 1-6, and some values > 6, and some < 1, and some values should be characters like the letter a or the symbol *. See what happens with the different inputs. When you discern a pattern I think you will be able to answer your question.

    #include <iostream.h>
    int main()
    {
    int value;
    char dummy;
    cout << "enter a value 1 - 6" << endl;
    cin >> value;
    cout << value;
    cin >> dummy;
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. Help! Stuck in a loop!
    By raell in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2003, 10:47 AM
  5. Stuck in a loop!.....Get me out of here!!
    By rabmaz in forum C Programming
    Replies: 3
    Last Post: 09-01-2002, 09:16 AM