# Cómo usar isdigit para números de más de un dígito [cerrado]

My program runs fine except when I enter a number for one of the digits then it just terminates instead of declaring invalid card number. How do I use isdigit to handle the problem?

``````#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;

int main()
{

//Input Variables

double cardnumber;
string name;
double TypeofBook;
string Title;
float RegularFine;
float BestSellersFine;
float MagazinesFine;
float HardcoverBooksFine;
double DaysLate;
char Choice;
char Y;
char N;
char y;
char n;

do

{ //Get Patron's info

cout << "Please Enter Patron's Library Card Number: \n";
cin >> cardnumber;
//Validate card number
while ((cardnumber <= 0) ||(cardnumber > 9999))
{
cout << "You have entered an invalid card number \n";
cout << "Enter a valid card number \n";
cin >> cardnumber;
}
``````

preguntado el 22 de septiembre de 13 a las 09:09

no encuentro ninguna llamada `isdigit()` in your code. Also I don't get what your critical input should be: "except when I enter a number for one of the digits then it just terminates". -

Are you taking your first programming class and the language is C++? -

Unrelated: your curly braces don't match up. You'll need another closing brace (for `main()`) y el `do {` no es necesario. -

## 5 Respuestas

``````bool isnum(const std::string& arg){
for(std::string::iterator it=arg.begin();it!=arg.end();it++)
if(!isdigit(*it))
return false;
return true;
}
``````

Respondido el 22 de Septiembre de 13 a las 11:09

Utilice el valor de retorno de `std::cin >> cardnumber` expresión:

``````int cardnumber;
if (std::cin >> cardnumber && (cardnumber > 0 && cardnumber  < 9999)) {
// success
}
``````

where "success" means that `int` has been successfully retrieved from `std::cin`and it is within the interval <0; 9999)

Respondido el 22 de Septiembre de 13 a las 09:09

``````#include <iostream>
#include<string>
using namespace std;

int toNumber(string s)
{
int num=0;
for (int i=0;i<s.size();i++)
if (s[i]>='0'&&s[i]<='9')
num = num*10 + s[i]-'0';
else
return -1;
return num;
}

int main(int argc, const char * argv[])
{
int number;
string cardnumber;
cout << "Please Enter Patron's Library Card Number: \n";
cin >> cardnumber;

while ( (number = toNumber(cardnumber)) == -1 || number > 9999) // no need to check if less than zero. cause it will return -1.
{

cout << "You have entered an invalid card number \n";
cout << "Enter a valid card number \n";
cin >> cardnumber;
}

// use card number here as string (cardnumber) and int (number).
cout << cardnumber << " " << number << endl;
return 0;
}
``````

Respondido el 22 de Septiembre de 13 a las 09:09

i guess you are looking for a validation in which no `char` should be entered,if i am correct then there is a function made `is_int` leer este ,

contestado el 23 de mayo de 17 a las 11:05

isdigit is for char. if you are expecting non-digits characters as input. you must use string to get those then to validate. as follow:

``````string cardnumber;
cout << "Please Enter Patron's Library Card Number: \n";
cin >> cardnumber;
//Validate card number
while (cardnumber.find_first_not_of("0123456789") != -1)
{
cout << "You have entered an invalid card number \n";
cout << "Enter a valid card number \n";
cin >> cardnumber;
}
``````

Buena suerte,

Respondido el 22 de Septiembre de 13 a las 09:09

dont forget to #include<string> using namespace std; - hasan

`std::string::npos` may also be preferred over `-1`. - Jamal

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.