User Name Password Register
DaniWeb IT Discussion Community
All
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
Aug 18th, 2007
Views: 3,893
SpS
Code will check whether the number entered is integer or not.

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.
cplusplus Syntax | 5 stars
  1. #include <iostream>
  2. #include <limits>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. int number = 0;
  8. cout << "Enter an integer: ";
  9. cin >> number;
  10.  
  11. cin.ignore(numeric_limits<int>::max(), '\n');
  12.  
  13. if (!cin || cin.gcount() != 1)
  14. cout << "Not a numeric value.";
  15. else
  16. cout << "Your entered number: " << number;
  17. return 0;
  18. }
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.

  1. // Caso profesor.
  2. case 'p':
  3. case 'P': Profesor:
  4. if(profesor[0] != "")
  5. {
  6. gotoxy(5,27);
  7. verde
  8. printf("%c", 175);
  9. rojo
  10. cout << " Ya ha definido un profesor.";
  11. blanco
  12. }
  13. else
  14. {
  15. gotoxy(5,23);
  16. blanco
  17. cout << "Ingrese la cedula del profesor: ";
  18. rojo
  19. cin.ignore(1,'\n');
  20. cin >> ci;
  21. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  22.  
  23. if (!cin || cin.gcount() != 1)
  24. {
  25. gotoxy(5,27);
  26. verde
  27. printf("%c", 175);
  28. rojo
  29. cout << " No es un valor numerico." << flush;
  30. cin.clear();
  31. break;
  32. blanco
  33. }
  34. else
  35. {
  36. blanco
  37. gotoxy(5,25);
  38. cout << "Ingresar el nombre y apellido del profesor: ";
  39. rojo
  40. getline(cin, b);
  41. blanco
  42. if(b == "")
  43. {
  44. gotoxy(5,27);
  45. verde
  46. printf("%c", 175);
  47. rojo
  48. cout << " No introdujo Nombre.";
  49. blanco
  50. }
  51. else
  52. {
  53. stringstream out;
  54. out << ci;
  55. a = out.str();
  56. profesor[0]= a;
  57. profesor[1]= b;
  58. }
  59. }
  60. }
  61. 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.

  1. case 'p':
  2. case 'P': Profesor:
  3. gotoxy(5,23);
  4. blanco
  5. cout << "Ingrese la cedula del profesor: ";
  6. rojo
  7. //cin.ignore(1,'\n');
  8. cin >> a;
  9. cin.ignore(numeric_limits<int>::max(), '\n');
  10.  
  11. if (!cin || cin.gcount() != 1)
  12. {
  13. cout << "No es un valor numerico.";
  14. }
  15. blanco
  16. gotoxy(5,25);
  17. cout << "Ingresar el nombre y apellido del profesor: ";
  18. rojo
  19. getline(cin, b);
  20. blanco
  21. if(profesor[0] != "")
  22. {
  23. gotoxy(5,27);
  24. verde
  25. printf("%c", 175);
  26. rojo
  27. cout << " Ya ha definido un profesor.";
  28. blanco
  29. }
  30. else if(a == "" || b == "")
  31. {
  32. gotoxy(5,27);
  33. verde
  34. printf("%c", 175);
  35. rojo
  36. cout << " No introdujo Nombre o Cedula.";
  37. blanco
  38. }
  39. else
  40. {
  41. profesor[0]=a;
  42. profesor[1]=b;
  43. }
  44. 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

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 5:36 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC