|
-
Sep 27th, 2002, 01:19 PM
#1
Thread Starter
Stuck in the 80s
[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.
-
Sep 27th, 2002, 01:37 PM
#2
Monday Morning Lunatic
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
-
Sep 27th, 2002, 03:25 PM
#3
Thread Starter
Stuck in the 80s
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.
-
Sep 27th, 2002, 03:29 PM
#4
Monday Morning Lunatic
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
-
Sep 27th, 2002, 03:41 PM
#5
Thread Starter
Stuck in the 80s
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.
-
Sep 27th, 2002, 04:26 PM
#6
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.
-
Sep 27th, 2002, 04:26 PM
#7
Monday Morning Lunatic
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
-
Sep 27th, 2002, 04:28 PM
#8
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.
-
Sep 27th, 2002, 04:30 PM
#9
Monday Morning Lunatic
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
-
Sep 27th, 2002, 04:36 PM
#10
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.
-
Sep 27th, 2002, 04:38 PM
#11
Monday Morning Lunatic
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
-
Sep 27th, 2002, 04:41 PM
#12
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.
-
Sep 27th, 2002, 09:09 PM
#13
Thread Starter
Stuck in the 80s
Damn...I was hoping for short and sweet, but this seems like too much.
-
Sep 28th, 2002, 05:16 AM
#14
Monday Morning Lunatic
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
-
Sep 28th, 2002, 06:47 AM
#15
Frenzied Member
Originally posted by parksie
Heh, there's never any "short and sweet"
Actually, Its called VB =P.
Z.
-
Sep 28th, 2002, 06:56 AM
#16
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.
-
Sep 28th, 2002, 07:08 AM
#17
Frenzied Member
-
Sep 28th, 2002, 07:36 AM
#18
Monday Morning Lunatic
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
-
Sep 28th, 2002, 07:37 AM
#19
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.
-
Sep 28th, 2002, 07:46 AM
#20
Frenzied Member
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.
-
Sep 28th, 2002, 07:52 AM
#21
Monday Morning Lunatic
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
-
Sep 28th, 2002, 08:22 AM
#22
Frenzied Member
Nice smiley you got there =).
Z.
-
Sep 28th, 2002, 08:31 AM
#23
transcendental analytic
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.
-
Sep 28th, 2002, 09:18 AM
#24
Thread Starter
Stuck in the 80s
Hey, dude. Don't break my watermelon because I'm all punch, y'see?
I don't know what I said either...
-
Sep 28th, 2002, 06:28 PM
#25
Hyperactive Member
Originally posted by kedaman
VB is going to be quite useless when SQ comes out =)
What's SQ?
-
Sep 28th, 2002, 06:48 PM
#26
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.
-
Sep 29th, 2002, 12:25 AM
#27
Frenzied Member
Its not confusing to kedaman...
But he isnt human =P
Z.
-
Sep 29th, 2002, 12:31 PM
#28
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.
-
Sep 29th, 2002, 01:31 PM
#29
Fanatic Member
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
-
Sep 30th, 2002, 05:50 AM
#30
Thread Starter
Stuck in the 80s
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.
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
|