im writing a programing C++ it gets numbers from the users ive forgotten how to validate the user entry so if a character is entered the user is asked to re enter the number i know a do loop can be used i just require the condition
im writing a programing C++ it gets numbers from the users ive forgotten how to validate the user entry so if a character is entered the user is asked to re enter the number i know a do loop can be used i just require the condition
You can use cin.goo(), this function returns a bool, so you can check it an if statement
He means cin.good( )
Couldn't think of anything interesting, cool or funny - sorry.
cin.good() has drawbacks. As long as the input begins with a digit, cin.good() will return "true", truncating the first invalid character and anything that follows. That is, '25g9j' will be held as '25'. Not too good. (Pardon the pun. )
This has been suggested before (by Prelude, if not others). Place your input into a string and validate the individual characters. On success, convert the string to a numeric value.
Not quite as bad as it seems.
I don't attest to the elegance of my approach here, but it works. Note that I've written the code using 'atof' vice 'atoi'. It can be easily modified to suit your particular needs.Code:#include <iostream> #include <cstdlib> // system call #include <ctype> // isdigit() #include <cmath> // atof (ASCII to float (double) ) #include <iomanip> // setprecision() inline void pause() { system("PAUSE"); } int main(void) { char* input = " "; double numVal; bool valid = true; std::cout << "Enter a numeric value: -> "; std::cin >> input; for ( unsigned int i = 0; i < strlen(input); i++ ){ if ( !isdigit(input[i]) && input[i] != '.'){ std::cerr << "\nError! " << "\'" << input[i] << "\'" << " is an invalid character.\n\n"; valid = false; } } if ( valid ){ numVal = atof(input); std::cout << setiosflags(std::ios::fixed)<< std::setprecision(2) << "\nnumVal = " << numVal << "\n\n"; } pause(); return 0; }
-Skipper
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow
You might want to allocate some space for the input, too.
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
>That is, '25g9j' will be held as '25'. Not too good.
Unless that was required. Using good/fail/etc in this way allows simple parsing of strings. Besides, if you want to alter it's behaviour you could do something like -
If you needed to include other delimeters (' ') the code would be straightforward.Code:#include <iostream> using std::cin; using std::cout; int main() { double numVal; cin >> numVal; while(cin.fail()) { cin.clear(); cin.ignore(); cin >> numVal; } //Ensure everthing on line is valid if(cin.get()!='\n') cout << "Input invalid\n"; else cout << numVal; return 0; }
Joe
Thanks, I was on a harry.He means cin.good( )
I like what skipper wrote.
by the way is cin.good() == !cin.fail()
I mean is there any differences between them
>I mean is there any differences between them
Yes, fail() tests only the failbit for the stream state, good() tests badbit, eofbit, and failbit and will return true only if none of them are set.
-Prelude
My best code is written with the delete key.
> Yes, fail() tests only the failbit for the stream state, good() tests badbit, eofbit, and failbit and will return true only if none of them are set.
Doesn't fail() also test badbit?
(Sorry, just happened to be revising my code to account for multiple decimal points when I noticed your post. )
-Skipper
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow
>Doesn't fail() also test badbit?
Yes, I should stop posting in a hurry, it hurts my reputation.
-Prelude
My best code is written with the delete key.
Nah, it's still intact.
-Skipper
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow