•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,500 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,753 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Code will check whether the number entered is integer or not.
In the latter case, the delim character itself is also extracted.
ignore extracts characters from the input sequence and discards them. The extraction ends when max characters have been extracted and discarded or when the character delim(\n) is found, whichever comes first.In the latter case, the delim character itself is also extracted.
numeric_limits<int>::max() - Returns the maximum finite value for integer.gcount - returns the number of characters extracted by the last unformatted input operation performed on the object. Last edited : Aug 18th, 2007.
#include <iostream> #include <limits> using namespace std; int main() { int number = 0; cout << "Enter an integer: "; cin >> number; cin.ignore(numeric_limits<int>::max(), '\n'); if (!cin || cin.gcount() != 1) cout << "Not a numeric value."; else cout << "Your entered number: " << number; return 0; }
Comments (Newest First)
JLChafardet | Newbie Poster | Jul 21st, 2008
•
•
•
•
I managed to solve my problem by adding
cin.clear(); on the numerical validation, that managed to stop the infinite loop.
thanks anyway for this, it was really really useful.
cin.clear(); on the numerical validation, that managed to stop the infinite loop.
thanks anyway for this, it was really really useful.
cplusplus Syntax (Toggle Plain Text)
// Caso profesor. case 'p': case 'P': Profesor: if(profesor[0] != "") { gotoxy(5,27); verde printf("%c", 175); rojo cout << " Ya ha definido un profesor."; blanco } else { gotoxy(5,23); blanco cout << "Ingrese la cedula del profesor: "; rojo cin.ignore(1,'\n'); cin >> ci; cin.ignore(numeric_limits<streamsize>::max(), '\n'); if (!cin || cin.gcount() != 1) { gotoxy(5,27); verde printf("%c", 175); rojo cout << " No es un valor numerico." << flush; cin.clear(); break; blanco } else { blanco gotoxy(5,25); cout << "Ingresar el nombre y apellido del profesor: "; rojo getline(cin, b); blanco if(b == "") { gotoxy(5,27); verde printf("%c", 175); rojo cout << " No introdujo Nombre."; blanco } else { stringstream out; out << ci; a = out.str(); profesor[0]= a; profesor[1]= b; } } } break;
JLChafardet | Newbie Poster | Jul 19th, 2008
•
•
•
•
may be a nice trick, but doesn't works, i have been trying to validate input for numbers for a long time, and this isn't working on my program.
we are a 2nd semester IT Computer Degree class, we haven't seen pointers nor regex so that's not an option as we cannot use what we haven't been taught in classes.
I have the following switch/case on my code.
i need to validate, if a is not numeric, should print an error (which it doesn't do.)
the rest of the program runs pretty well, and is amazingly well formed, its about 600 lines of code atm, and it will grow.
the next part, which i will have to do between today and tomorrow, also contains the need for this numeric validation, as it will be the grades array, where I'm supposed to store 3 grades per student, and the grades needs to be between 0 and 20, only numerical values should be accepted.
the parts of the 0 to 20 are easy enough, but i cant get the numeric validation thing to work, all help is appreciated.
we are a 2nd semester IT Computer Degree class, we haven't seen pointers nor regex so that's not an option as we cannot use what we haven't been taught in classes.
I have the following switch/case on my code.
cpp Syntax (Toggle Plain Text)
case 'p': case 'P': Profesor: gotoxy(5,23); blanco cout << "Ingrese la cedula del profesor: "; rojo //cin.ignore(1,'\n'); cin >> a; cin.ignore(numeric_limits<int>::max(), '\n'); if (!cin || cin.gcount() != 1) { cout << "No es un valor numerico."; } blanco gotoxy(5,25); cout << "Ingresar el nombre y apellido del profesor: "; rojo getline(cin, b); blanco if(profesor[0] != "") { gotoxy(5,27); verde printf("%c", 175); rojo cout << " Ya ha definido un profesor."; blanco } else if(a == "" || b == "") { gotoxy(5,27); verde printf("%c", 175); rojo cout << " No introdujo Nombre o Cedula."; blanco } else { profesor[0]=a; profesor[1]=b; } break;
i need to validate, if a is not numeric, should print an error (which it doesn't do.)
the rest of the program runs pretty well, and is amazingly well formed, its about 600 lines of code atm, and it will grow.
the next part, which i will have to do between today and tomorrow, also contains the need for this numeric validation, as it will be the grades array, where I'm supposed to store 3 grades per student, and the grades needs to be between 0 and 20, only numerical values should be accepted.
the parts of the 0 to 20 are easy enough, but i cant get the numeric validation thing to work, all help is appreciated.
munyu | Newbie Poster | Dec 19th, 2007
•
•
•
•
nice trick
Post Comment
•
•
•
•
DaniWeb Marketplace (Sponsored Links)