Advertisement
cotolonco

Validar Ingreso tipo int

Nov 22nd, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. /*Validar entrada unicamente de tipo int*/
  8.  
  9. int pedirInt(string);
  10.  
  11. int main()
  12. {
  13.  
  14.     int numero;
  15.     numero = pedirInt("Ingrese Numero: ");
  16.     cout << endl << "Su Numero: " << numero << endl;
  17.     return 0;
  18. }
  19.  
  20. int pedirInt(string texto){
  21.     int valorInt;
  22.     bool terminado = false;
  23.     do{
  24.         cout << texto;
  25.         string linea;
  26.         getline(cin, linea);
  27.         stringstream ss(linea);
  28.         if (ss >> valorInt)
  29.             terminado = true;
  30.     }while(terminado == false);
  31.     return valorInt;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement