string accepting only numbers
How I can check if credit card numbers contains only of numbers?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
# include <iostream>
# include <string>
# include <iomanip>
using namespace std;
int main (){
string creditCard;
do{
cout << "Enter credit card number:\t";
cin >> creditCard;
for (int i = 0; i < 16; i++)
{
if (creditCard.length < 16 && creditCard.length > 16)
cout << "\nInvalid credit card number.Please try again:";
}
}while(true);
return 0;
}
|
Examine each char in the string. If the char you are examining is not a number, then the string is not just numbers.
if ( creditCard.find_first_not_of( "0123456789" ) == string::npos ) cout <<"Valid";
Topic archived. No new replies allowed.