When trying to run, program says terminate called after throwing an instance of 'std::invalid_argument'   what():  stoi Aborted (core dumped)

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

When trying to run, program says

terminate called after throwing an instance of 'std::invalid_argument'
  what():  stoi
Aborted (core dumped)

### Code Explanation: Reading Library Data from a File

Below is a segment of C++ code that reads data from an input file to fill an array of `Book` objects with details such as title, author, checkout status, and the number of holds.

```cpp
void readFile (ifstream &northFile, Library northLibrary[], Book northBooks[]){
    int i = 0;

    while(!northFile.eof()) {
        string temporary;
        int temp = stoi(temporary);

        getline(northFile, temporary, '*');
        northBooks[i].setTitle(temporary);

        getline(northFile, temporary, '*');
        northBooks[i].setAuthor(temporary);

        getline(northFile, temporary, '*');
        northBooks[i].setCheckedOut(temp);

        getline(northFile, temporary, '\n');
        northBooks[i].setNumHolds(temp);

        i++;
    }
}
```

### Detailed Explanation

1. **Function Definition**
    - The function `readFile` accepts three parameters:
        - `ifstream &northFile`: A reference to an input file stream from which data is read.
        - `Library northLibrary[]`: An array of `Library` objects.
        - `Book northBooks[]`: An array of `Book` objects to be populated with data from the file.

2. **Variable Initialization**
    - `int i = 0;`: Initializes an index counter `i` to zero.
    
3. **Reading Data in a Loop**
    - The `while` loop runs until the end of the file (`northFile.eof()`) is reached.

4. **Reading Each Piece of Data**
    - `string temporary;`: Declares a string variable to temporarily store data read from the file.
    - `int temp = stoi(temporary);`: Converts the `temporary` string to an integer and stores it in `temp`.

5. **Populating Book Array**
    - **Title**: 
        - `getline(northFile, temporary, '*');`: Reads a line from the file until a `'*'` delimiter is encountered. This data is assigned to `temporary`.
        - `northBooks[i].setTitle(temporary);`: Sets the title of the `i`-th `Book` object in the array `northBooks`.
    - **Author**: 
        - `getline(northFile, temporary, '*');`: Reads the next piece
Transcribed Image Text:### Code Explanation: Reading Library Data from a File Below is a segment of C++ code that reads data from an input file to fill an array of `Book` objects with details such as title, author, checkout status, and the number of holds. ```cpp void readFile (ifstream &northFile, Library northLibrary[], Book northBooks[]){ int i = 0; while(!northFile.eof()) { string temporary; int temp = stoi(temporary); getline(northFile, temporary, '*'); northBooks[i].setTitle(temporary); getline(northFile, temporary, '*'); northBooks[i].setAuthor(temporary); getline(northFile, temporary, '*'); northBooks[i].setCheckedOut(temp); getline(northFile, temporary, '\n'); northBooks[i].setNumHolds(temp); i++; } } ``` ### Detailed Explanation 1. **Function Definition** - The function `readFile` accepts three parameters: - `ifstream &northFile`: A reference to an input file stream from which data is read. - `Library northLibrary[]`: An array of `Library` objects. - `Book northBooks[]`: An array of `Book` objects to be populated with data from the file. 2. **Variable Initialization** - `int i = 0;`: Initializes an index counter `i` to zero. 3. **Reading Data in a Loop** - The `while` loop runs until the end of the file (`northFile.eof()`) is reached. 4. **Reading Each Piece of Data** - `string temporary;`: Declares a string variable to temporarily store data read from the file. - `int temp = stoi(temporary);`: Converts the `temporary` string to an integer and stores it in `temp`. 5. **Populating Book Array** - **Title**: - `getline(northFile, temporary, '*');`: Reads a line from the file until a `'*'` delimiter is encountered. This data is assigned to `temporary`. - `northBooks[i].setTitle(temporary);`: Sets the title of the `i`-th `Book` object in the array `northBooks`. - **Author**: - `getline(northFile, temporary, '*');`: Reads the next piece
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Exception Handling Keywords
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education