-
February 1st, 2004, 10:24 PM
#1
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
-
February 1st, 2004, 10:26 PM
#2
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.
-
February 1st, 2004, 10:59 PM
#3
Yeah, I thought about that, but it seemed like there must be a better way ...
Thanks, though.
-
February 2nd, 2004, 01:24 AM
#4
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.
-
February 2nd, 2004, 04:21 AM
#5
Outputting "\h" should move the cursor one step back. I don't know if it will move it up though.
All the buzzt
CornedBee
-
February 2nd, 2004, 04:40 AM
#6
Hi,
Somebody asked a similar question not too long ago.
http://www.codeguru.com/forum/showth...hreadid=280442
TDM
-
February 2nd, 2004, 06:08 AM
#7
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
-
February 2nd, 2004, 08:53 AM
#8
Sorry, I meant \b. Don't ask me why I wrote \h, it wasn't a typo.
All the buzzt
CornedBee
-
February 2nd, 2004, 03:23 PM
#9
'\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.
-
February 2nd, 2004, 04:18 PM
#10
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
-
February 4th, 2004, 02:52 AM
#11
Cornedbees subconcience + Your conscious = great brain !!!
Anyway thank you for the links
Regards
-
February 4th, 2004, 03:14 PM
#12
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.
-
February 4th, 2004, 05:47 PM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|