Results 1 to 30 of 30

Thread: [Resolved] Input valid data?

  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

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    #include <iostream>
    #include <vector>
    #include <iterator>
    
    using namespace std;
    
    int main() {
    	cout << "Please enter some numbers, end with -1: " << flush;
    
    	vector<int> nums;
    	int num = 0;
    
    	while(!cin.eof()) {
    		cin >> num;
    
    		if(num == -1 || cin.fail()) {
    			// stop when we get either -1 or something
    			// that wasn't recognised as a number
    			break;
    		}
    
    		nums.push_back(num);
    	}
    
    	cout << endl << "You entered:" << endl;
    
    	copy(nums.begin(), nums.end(), ostream_iterator<int>(cout, "\n"));
    
    	cout << endl;
    
    	return 0;
    }
    This uses cin.fail() to check whether the input actually worked or not.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I'm not sure I follow that code...

    What I'm trying to do is have a menu in which the user chooses 1 through 5. It's easy enough to make sure it's between 1 and 5, but I need to prevent 5.5, a, +, etc because it makes the program spaz.

    That code just confuses me.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Try and read an integer. Then call cin.fail(). If it returns true, then it didn't work.

    Run the code, see what it does, look in a debugger. It's very simple
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by parksie
    Try and read an integer. Then call cin.fail(). If it returns true, then it didn't work.

    Run the code, see what it does, look in a debugger. It's very simple
    I tried calling cin.fail() after reading an integer:

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
        
        int num;
    
        cout << "Enter an Integer: ";
        cin >> num;
        
        while (num < 1 || num > 5 || cin.fail()) {
            system("CLS");
            cout << "Enter an INTEGER: ";
            cin >> num;
        }
    
        cout << endl << num << endl << endl;
    
        return 0;
    }
    Still goes crazy.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    while (num < 1 || num > 5 || cin.fail()) {
            cin.clear();
            system("CLS");
            cout << "Enter an INTEGER: ";
            cin >> num;
        }
    cin won't read again until you clear the status.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    [mike@relativity junk]$ ./hobo
    Enter an Integer: 10
    sh: CLS: command not found
    Enter an INTEGER: -4
    sh: CLS: command not found
    Enter an INTEGER: 4.3
    
    4
    
    [mike@relativity junk]$
    ...acts like I'd expect it to, I think. I made a fix to stop it going mental if you added a letter:
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main() {
        
        int num;
    
        cout << "Enter an Integer: ";
        cin >> num;
        
        while (num < 1 || num > 5 || cin.fail()) {
            cin.clear();
            cin.ignore();
    
            system("CLS");
            cout << "Enter an INTEGER: ";
            cin >> num;
        }
    
        cout << endl << num << endl << endl;
    
        return 0;
    }
    It's the .clear() and .ignore() -- the clear() resets the fail status, and the ignore() tells it to move past the offending character (the one you gave). So far this means that it'll give enough errors for each character you give it.

    I'll let you work that out though
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    parksies test shows another interesting thing...
    But I admit I know of no solution.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Solution is to use getline, and parse it yourself.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I actually mean this:
    sh: CLS: command not found



    Interesting, when just viewing posts mine is after yours. When replying it is before yours.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    That's because I think it needs "cls" not "CLS"...perhaps.


    Nope..."clear" on a terminal
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    system is bound to be platform-dependent. Except cd, but this is quite useless.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Damn...I was hoping for short and sweet, but this seems like too much.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Heh, there's never any "short and sweet"
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  15. #15
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Originally posted by parksie
    Heh, there's never any "short and sweet"
    Actually, Its called VB =P.

    Z.

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Sweet?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  17. #17
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Good, like candy =).

    Z.

  18. #18
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Yeah, but it's completely non-portable
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  19. #19
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    VB is like candy. Sweet and nice, but it'll ruin your teeth and you won't be able to chew on hard problems anymore
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  20. #20
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Originally posted by parksie
    Yeah, but it's completely non-portable
    [n00b]
    What r u talking about? I can put vb progs on Windows 95, and 98, and Xp, and ME, and 2000!!!
    [/n00b]

    Z.

  21. #21
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    =)
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  22. #22
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Nice smiley you got there =).

    Z.

  23. #23
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    VB is going to be quite useless when SQ comes out =)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  24. #24

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Hey, dude. Don't break my watermelon because I'm all punch, y'see?

    I don't know what I said either...
    My evil laugh has a squeak in it.

    kristopherwilson.com

  25. #25
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Originally posted by kedaman
    VB is going to be quite useless when SQ comes out =)
    What's SQ?

  26. #26
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    SQ stands short for Squirrel, which is a language Keda has developed, which follows none of the current programming paradigms (function-oriented, object-oriented, etc). Click at the link in his sig for a very confusing explanation of it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  27. #27
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Its not confusing to kedaman...
    But he isnt human =P

    Z.

  28. #28
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I have this impression too sometimes...

    I'm always terribly confused, but not as confused as when I see the syntax definition (same location).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  29. #29
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Hey man, have you tried this:

    PHP Code:

    int num
    ;
    char ch 0;

    do {
            
    system("cls");
            
    cout << "Enter an INTEGER: ";
            
    cin >> ch;
            
    cin.ignore ();
    } while (
    ch '1' ||  ch '5');

    num = (int) ch '0'
    How hard could that be
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  30. #30

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

    Code:
    cin >> option;
    
    while (option < '1' || option > '5') {
        system("CLS");
        cout << "Try again!" << endl << endl;
        cout << "Please choose a number (1) through (5): ";
        cin >> option;
    }
    
    choice = option - 48;
    is what I did.
    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