-1

I tried using isdigit and isspaceat 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;
}
asmmo
  • 6,922
  • 1
  • 11
  • 25
  • You can make your life easier if you just use [`std::string`](https://en.cppreference.com/w/cpp/string/basic_string) for strings. C++ isn't C. Also, that is not how you do `struct`s in C++. – bitmask Apr 05 '20 at 10:04
  • Use the logical OR operator ||. If the variable is not a digit OR if is a space, then it would be invalid. Second,you will need to implement a for loop that checks each 15 elements of the isbn_code[] array to see if each one is a space OR a not a digit. Those are the two big things that I see. Good luck. – pctopgs Apr 05 '20 at 10:08

1 Answers1

0

Assuming that the rest of your code is ok, it seems that your are checking only the element in 14th position in your isbn_code array:

cin >> books->isbn_code[13];

So, if the array that you give in input is simply '123' the 14th position has a value that is random (i.e., preallocated in some manner that you do not know).

Eddymage
  • 1,008
  • 1
  • 7
  • 22
  • sorry that part was wrong. i've changed it to cin >> books[0].isbn_code[13]. is this still wrong? – Ying Shuang Apr 05 '20 at 09:59
  • In this way, you are accessing to the 14th element of the `isbn_code` array of the first element of your `book` array. – Eddymage Apr 05 '20 at 10:12
  • owhh so what does the [0] means? – Ying Shuang Apr 05 '20 at 10:22
  • With `DATA books[50];` you created an array with 50 elements and each element is of type `DATA`. Each element of `books` is a struct (by your definition) which members `year_published`, `quantity`, `isbn_code[15]`. Is it clear the definitions of array and of struct? – Eddymage Apr 05 '20 at 10:27