In the book_store.cpp file, add the code for the process_orders method. The argument that is passed to this method is the name of a file that will be read in the method. Each line in the file contains an order number (integer), isbn number (character array), and amount ordered (integer). If an order number is on a line, it is guaranteed that there will be an isbn number and amount ordered to go along with it. Create an input file stream variable. Create variables for the three pieces of information on a line of the file. Display the Order Listing report header as shown in the Part 5 Output portion of the assignment write-up. Using the input file stream variable, open the file that’s name was passed into the method. The file that was passed to the method contains text data, so there is nothing special that needs to be added to the open statement. After opening the file, code a decision statement that will test to make sure the file opened correctly. If the file did not open correctly, display an error message and exit the program. This method should use a loop to read the file. Make sure to follow the priming read/secondary read pattern that has been discussed in the lecture. The priming read/secondary read should read the order number from the file (since this is simple text data, just use the regular input operator to read the data). The loop should execute as long as end of file has not been reached. In the body of the loop: • read the isbn number (again, just use the regular input operator) • read the amount ordered (again, just use the regular input operator) • code a cout statement that displays the order number, isbn number, and amount ordered that were just read from the file. • read the next order number (this is the secondary read) Close the file. The goal at this point is just to make sure that the file can be properly read.
C++
class MyClass { // The class
public: // Access specifier
int order_number;
void process_orders (filename) {
std::ifstream file(filename);
int order_number;
int amount_ordered;
char []isbn_number;
int i=1;
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
char str[] = line;
// Returns first token
char *token = strtok(str, " ");
// Keep printing tokens while one of the
// delimiters present in str[].
i=1;
while (token != NULL)
{
stringstream toconvert(token);
if(i==1)
{
toconvert >> order_number;
cout << "order number is: " << order_number;
}
else if(i==2)
{
toconvert >> isbn_number;
cout << "isbn_number is: " << isbn_number;
}
else if(i==3)
{
toconvert >> amount_ordered;
cout << "amount_ordered is: " << amount_ordered;
}
i=i+1;
token = strtok(NULL, "-");
}
}
file.close();
}
else {
// show message:
std::cout << "Error opening file";
exit(0);
}
}
};
int main() {
MyClass myObj; // Create an object of MyClass
myObj.process_orders(filename); // Call the method
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.