I tried using isdigit
and isspace
at the same time but it wouldn't check for both at the same time.
If I typed "123" or "123" or "abc" or "abc", it wouldn't return error for checking.
What I want is to make sure that the user only enters integers.
And characters other than integers like alphabets, spaces and punctuations would be rejected.
typedef struct
{
int rack, level_no;
}LOCATION;
typedef struct
{
int year_published, quantity;
char isbn_code[15];
char author[55];
char title[105];
char publisher[55];
double price;
LOCATION loc;
}DATA;
int add_record()
{
DATA books[50];
bool valid = true;
system("CLS");
cout << "\t\t\t\t\t\t\t\t\t : :Add Book Record: :\n\n";
ifstream infile("books.txt", ios::app);
if (infile.is_open() && !infile.eof())
{
do
{
cout << "ISBN Code: ";
cin >> books->isbn_code[13];
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if(!isdigit(books->isbn_code[13]) && isspace(books->isbn_code[13]))
{
cout << "Your input is invalid. Please enter again.\n";
cout << "ISBN Code: ";
cin >> books->isbn_code[13];
valid = false;
}
else
valid = true;
} while (valid == true);
}
return 0;
}