CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Nov 2003
    Posts
    34

    Cin, Enter with No Data

    Hi,

    I'm building an application to teach myself C++. I'm currently in the "fix areas where users could screw up the program" phase.

    What I've noticed is that with something like:

    int x = 0;
    cout << "Enter an integer: "; cin >> x;

    It's easy enough to have to fix the input stream if the user enters something like 'w' or another non-integer.

    What I can't figure out, though, is how I can get around what occurs if the user mistakenly presses enter while not having entered any data. Usually the cursor just moves down the screen until something other than enter is input.

    So the question is: if the user hits enter in a scenario like the above, but nothing else, how can I have another prompt that says something like: "Invalid entry. Try again: " type thing?

    Thx

  2. #2
    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    762
    I think you're going to have to use std::getline() for that, and I believe it only reads in strings so you'll have to convert anything you get from it to whatever data type you need it to be.

  3. #3
    Join Date
    Nov 2003
    Posts
    34
    Yeah, I thought about that, but it seemed like there must be a better way ...

    Thanks, though.

  4. #4
    Join Date
    Dec 2003
    Posts
    99
    Hi,

    I dont think there is any better way than std::getline().

    On similar lines, Is there any way we can move the cursor back to orignal position?
    If this is possible portably I think it is better to do this instead of showing up an error message.

  5. #5
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    Outputting "\h" should move the cursor one step back. I don't know if it will move it up though.
    All the buzzt
    CornedBee

  6. #6
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    Hi,

    Somebody asked a similar question not too long ago.

    http://www.codeguru.com/forum/showth...hreadid=280442


    TDM

  7. #7
    Join Date
    Dec 2003
    Posts
    99
    Originally posted by CornedBee
    Outputting "\h" should move the cursor one step back. I don't know if it will move it up though.
    sorry i tried it out (MS .NET 2003)
    doesnt work though

    I could not find the \h in the C standard either

  8. #8
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    Sorry, I meant \b. Don't ask me why I wrote \h, it wasn't a typo.
    All the buzzt
    CornedBee

  9. #9
    Join Date
    Dec 2003
    Posts
    99
    '\b' was the closest, but I had tried that before posting. Doesnot put the cursor a line up.

    Cornedbee, I would like to know why you wrote \h unless even this mail had a typo.

  10. #10
    Join Date
    Nov 2003
    Posts
    1,902
    In CornedBee's subconscience, he realized that the ascii code for backspace is 8, and therefore typed the 8th letter of the alphabet, "h", instead of "b"

    There is no C/C++ standard way of doing what you want - but there are solutions:
    For *nix type systems there are libraries like curses and ncurses which allow you to manipulate the cursor posistion amoung other things.
    For Windows systems there are Win32 API functions for maninpulating the console.
    Here is a good tutorial for the Windows side of things.

    gg

  11. #11
    Join Date
    Dec 2003
    Posts
    99
    Cornedbees subconcience + Your conscious = great brain !!!

    Anyway thank you for the links

    Regards

  12. #12
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    There was a question sometime in the past couple of days asking for the C++ equialent to _getch(). I don't know if it was this forum or the Win API forum but I doubt it was in the other forum. An answer was provided that uses a platform-specific functions to set up the console for unbuffered input and probably all platforms have a way to do that, in case that helps.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  13. #13
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721
    For the original question, maybe don't skip whitespaces.
    Something like this :

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        int x;
    
        bool bOK = false;
    
        cout << "enter an integer > ";
        while(!bOK)
        {
            cin.unsetf(std::ios::skipws);
            cin >> x;
    
            if (!cin) 
            {
                cout << "Invalid entry. Try again: ";
                cin.clear();
                cin.ignore(80,'\n');
            }
            else
            {
                bOK = true;
                cin.setf(std::ios::skipws);
            }
        }
    
        cout << "x = " << x << endl;
    
        return 0;
    }
    Last edited by Philip Nicoletti; February 5th, 2004 at 07:13 AM.

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

Featured