tags:

views:

137

answers:

1

I have a book class that takes title, author, copyright, ISBN number, and checkout for a book object. However, I'm getting a runtime error when the program runs. After the user inputs the title and presses enter, the program skips down, showing the rest of the outputs, and then terminates the program giving a runtime error.

I tried to catch an exception, but I didn't get anything.

Code:

#include "std_lib_facilities.h"

class Book{
public:
       string what_title();
       string what_author();
       int what_copyright();
       void store_ISBN();
       void is_checkout();
private:
        char check;
        int ISBNfirst, ISBNsecond, ISBNthird;
        char ISBNlast;
        string title;
        string author;
        int copyright;
};

string Book::what_title()
{
       cout << "Title: ";
       cin >> title;
       cout << endl;
       return title;
}

string Book::what_author()
{
       cout << "Author: ";
       cin >> author;
       cout << endl;
       return author;
}

int Book::what_copyright()
{
    cout << "Copyright Year: ";
    cin >> copyright;
    cout << endl;
    return copyright;
}

void Book::store_ISBN()
{
     bool test = false;
     cout << "Enter ISBN number separated by spaces: ";
     while(!test){
     cin >> ISBNfirst >> ISBNsecond >> ISBNthird >> ISBNlast;
     if((ISBNfirst || ISBNsecond || ISBNthird)<0 || (ISBNfirst || ISBNsecond || ISBNthird)>9)
                   error("Invalid entry.");
     else if(!isdigit(ISBNlast) || !isalpha(ISBNlast))
          error("Invalid entry.");
     else test = true;}     
}

void Book::is_checkout()
{
     bool test = false;
     cout << "Checked out?(Y or N): ";
     while(!test){
     cin >> check;
     if(check = 'Y') test = true;
     else if(check = 'N') test = true;                                
     else error("Invalid value.");}
}

int main()
{
    Book one;
    one.what_title();
    one.what_author();
    one.what_copyright();
    one.store_ISBN();
    one.is_checkout();
    keep_window_open();
}

Not sure what the problem could be. Any help is appreciated, thanks.

Output example:

Title: One Flew Over the Cuckoo's Nest (next lines aren't actually spaced in between and all output at once) Author:

Copyright Year:

Enter ISBN number separated by spaces:

This application has requested the Runtime to terminate it in an unusual way. Please contact support for more information.

Also don't worry about the keep_window_open and error functions. They are part of the std_lib_facilities.h and are most likely not causing the problem. Error just outputs an error message if a problem is encountered.

+2  A: 

The problem here is that the C++ input streams do not remove malformatted input that they encounter. In other words if you try to read in a number and the stream contains, for example the character 'x' (not a number), that character isn't removed from the input stream. Additionally, if I remember correctly, that will also put the input stream in an error state causing well-formatted input to also fail. Although there is a mechanism for testing the state of the input stream and removing malformattted input and clearing the error flags, I have personally found it simpler to always read into a string (using ">>" or "getline") and then to parse the string. In the case of a number, for example, you can use the "strtol" or "strtoul" functions.

Michael Aaron Safyan
Yeah I was sitting here thinking and realized that it's only taking the "The" of the string and using the other inputs for the next functions. I will try your suggestion.
trikker
Yup getline worked! Just needed a push in the right direction, thanks.
trikker