Results 1 to 30 of 30

Thread: [Resolved] Input valid data?

Threaded View

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    [Resolved] Input valid data?

    How can you prevent errors from occuring when a user inputs wrong information? Lets say I ask for an integer value (like below) and they put 'a'? It then causes a major spaz in the program.

    Example Code:

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
        
        int num;
    
        cout << "Enter an Integer: ";
        cin >> num;
        
        while (num < 1 || num > 5) {
            system("CLS");
            cout << "Enter an INTEGER: ";
            cin >> num;
        }
    
        cout << endl << num << endl << endl;
    
        return 0;
    }
    How can this be prevented?
    Last edited by The Hobo; Sep 28th, 2002 at 05:29 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width