Thread: Is there any way to prevent a char/int infinite loop?

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    9

    Is there any way to prevent a char/int infinite loop?

    The one that always occurs when you enter a character where the program is expecting an integer?

    This is in a command line application using Standard C++ (not visual or anything like that).

  2. #2
    Registered User
    Join Date
    Nov 2004
    Posts
    6
    why dont you change the int to a char?

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    9
    Because I need to perform mathematical operations using user inputs...

  4. #4
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    you can store the user input into a char first, and use isdigit() function to check if it is an integer, if the isdigit() returns true,then convert the char into an integer.it is simple

    blow me ... ...

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    You need to always check the status of the stream and loop on .good(), never .eof()
    Code:
    while(cin) {
        int x;
        while(!(cin >> x)) {
            cin.clear();
            std::string token;
            if(cin >> token && token != "quit") {
                cout << "expected integer, read \"" << token << '"' << endl;
            } else {
                cerr << "unrecoverable stream error while expecting integer" << endl;
               /****throw abort return or whatever, bail out here****/
            }    
        }
    /*** x is valid here ***/
    }
    another example reading a number of ints into a vector in {2, 3,5} format
    Code:
    bool readvec(std::vector<int> &v, std::istream &is=std::cin) {
        char ch;
        if(is >> ch && ch == '{') {
            int n;
            while(is >> n >> ch && ch == ',') v.push_back(n);
            if(ch == '}') {
                v.push_back(n);
                return true;
            }
        }
        return false;
    }

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Data validation can be done reading everything into a string first, then validating the input, then converting to appropriated data type (int, float have built in data conversion functions). Alternatively, you can monitor stream methods (for example good(), fail(), clear(), ignore()) for appropriate input. Both approaches have their advantages and disadvantages. I'd suggest a board search as this topic comes up reasonably often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. infinite loop
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-22-2001, 11:04 AM