-2

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;
string address;
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;
}
  • 2
    I cannot find any call to `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"*. – Zeta Sep 22 '13 at 08:21
  • 2
    Are you taking your first programming class and the language is C++? – 6502 Sep 22 '13 at 08:30
  • Unrelated: your curly braces don't match up. You'll need another closing brace (for `main()`) and the `do {` isn't needed. – Jamal Sep 22 '13 at 08:31

5 Answers5

1
bool isnum(const std::string& arg){
  for(std::string::iterator it=arg.begin();it!=arg.end();it++)
     if(!isdigit(*it))
         return false;
  return true;
}
Simple Fellow
  • 4,315
  • 2
  • 31
  • 44
0

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;
}

Good Luck,

hasan
  • 23,815
  • 10
  • 63
  • 101
0

Use the return value of std::cin >> cardnumber expression:

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

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

LihO
  • 41,190
  • 11
  • 99
  • 167
0
#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;
}
hasan
  • 23,815
  • 10
  • 63
  • 101
0

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 read this ,

Community
  • 1
  • 1