Adding a book to a document

Everything is fine except for Case 5. I need to add a book from the user. It lets me to write it to the .dat file, but when I try to run it again I get an error saying string out of rang. How do I fix that? I only posted case 5 because everything else works


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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
case 5:
        {
            
            cout << "Enter author: ";
            cin.ignore();
            getline(cin, S1);
            cout << "Enter title: ";
            getline(cin, S2);
            cout << "Enter publisher: ";
            getline(cin, S3);
            cout << "Enter date: ";
            getline(cin, S4);
            cout << "Enter category (1-letter code): ";
            cin >> S5;
            
            
            ofstream output;
            output.open("books.dat", ios::app);
            if (output.fail())
            {
                cout << "books.data file opening failed. Aborting..\n";
                exit(1);
            }
            else
            {
                output << S1 << " " << S2  << " " << S3 << " " << S4 << " " << S5;
            }
            
            
           while (!BOOKS_data.eof())
            {
                getline(BOOKS_data, line);
                if (line.length() > 0)
                {
                    catalog[i] = make_book(line);
                    p = i++;
                }
            }
            
            BOOKS_data.close();
            new_books.close();

            
            cout << "New book was added to list.";
            break;
        }

Last edited on
Please use code tags.
It wouldnt let me post my full code. I think it has to do with the .eof line but im not forsure.
No no no, you didn't understand me. Use code tags means: enclose your code in [.code] [./code] (without the dots). This way your code looks neat instead of copy-pasted plaintext terror.

For example:

1
2
3
4
int main()
{
  return 0;
}


This way it's easier to read.
I didnt know how to do that. I did it now.
Okay. What is BOOKS_data? The code isn't complete, and sometimes might not make sense(what is p? what is make_book? what is catalog?), but if I were to guess, you first open open BOOKS_data somewhere in the program, close it in case 5, and then run again with BOOKS_data closed. I might be wrong however - it would be nice if we could get full output and maybe some stack trace.
Topic archived. No new replies allowed.