Question

In the book_store.cpp file, add the code for the process_orders method. The argument that is passed...

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++

Homework Answers

Answer #1
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;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Code a C file, following these instructions: This lab is about getting the input from a...
Code a C file, following these instructions: This lab is about getting the input from a file. Let’s assume the words are provided in one file, passed as an argument to your program. The names of the files are provided as arguments to your program (i.e., they are copied by the shell in the argv variable), and each file is a text file with lots of words. Open the manual page for open() system call and add to your code...
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
The following code to run as the described program on python. The extra test file isn't...
The following code to run as the described program on python. The extra test file isn't included here, assume it is a text file named "TXT" with a series of numbers for this purpose. In this lab you will need to take a test file Your program must include: Write a python program to open the test file provided. You will read in the numbers contained in the file and provide a total for the numbers as well as the...
we will be taking data in as a file. you cannot use the data values in...
we will be taking data in as a file. you cannot use the data values in your source file but you must read in everything into variables and use it from there. First we need to include <fstream> at the top of our file. We will need to create an input file stream to work and ifstream. ifstream is a datatype. Create a variable with the datatype being ifstream. Variable is drfine by using the member accessor operator to call...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
Write regular expressions that will match IP addresses and usernames in a Microsoft IIS log file....
Write regular expressions that will match IP addresses and usernames in a Microsoft IIS log file. First, you will write a regular expression that matches IP addresses within the re.compile function call on line 36. Next, you will write a regular expression that matches usernames (in the format domain\username) on line 50. Finally, in steps 3 and 4 you will write a loop for each step that displays all the IP addresses (gathered into the list ipAddresses) and usernames (gathered...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain account numbers (10 digits) along with the account type (L:Loan and S:Savings) and their current balance. The second file (transactions.txt) will contain transactions on the accounts. It will specifically include the account number, an indication if it is a withdrawal/deposit and an amount. Both files will be pipe delimited (|) and their format will be the following: accounts.txt : File with bank account info...