CS 010 - Introduction to Computer Science I
Lab 8. File I/O, Structures

Points(10 overall)

Collaboration policy:

Collaboration on this lab exercise is strongly ENCOURAGED. The exercise is intended for practice, not assessment -- most people who try should get all participation points. Feel free to ask for help from, and provide help to, others. You shouldn't blindly copy solutions from one another (or from anywhere else) nor simply write code for someone else (even if you explain it as you write), but you can certainly help each other debug, give plenty of suggestions and hints, **explain** why things work or don't work, etc. If you get done early, feel free to walk around and help others.


Part 1

For this part it would be a good exercise to make your own file called input.txt using emacs. Open a file called input.txt and type some words into it or copy-and-paste some sentences found on the interenet to this file. Make sure this file is either created in the same directory as your program for this lab, or copy it to this directory if it was created somewhere else.

One way to do this is to cd into the directory where you will be writing your program. Then type:

emacs input.txt &

If there was no file by this name already in that directory you will have a blank emacs window. You can now type your words or copy-and-paste into this to create your input.txt. Don't forget to save it before trying to use it in you program.

Now write a program that takes its input from this file full of words called input.txt and outputs it to a file called output.txt. You can just read it in word by word and output each word to the output file. Don't forget to put the space between each word when you output it.

The statements

string word;
cin >> word;

will read in in all characters from a user until it sees a space or newline (whitespace) and put them in the string variable word. If you wanted to do the same thing but read it from a file instead of from the user, you would need to replace cin with your file object you must declare and connect (open) to the file input.txt. Do not forget to check that the file opened correctly using the member function fail().

The statement:

cout << word << " ";

will output the word plus a space after it to the screen. If you wanted to do the same thing but output it to a file instead, you would need to replace cout with a file object you must declare and connect (open) to the file output.txt. This must be a different file object than the one you connected to input.txt.

After you run your program, you need to open output.txt to see what was written to it. You can just open it the same way you open your program files like as5.cpp.


Part 2

Rewrite your previous program but output the words one word per line. Right after the word, on the same line, you should output the length of the word. Recall the length() member function for string objects given in lecture. The statements:

string word;

cout << "Enter a word\n";
cin >> word; cout << word.length() << endl;

would output to the screen the length (number of characters) of the variable word that contains the value the user entered. So, if the user entered "Hello", the output would be 5. If the user entered "end." the output would be 4.

If your input file had the sentences,

"hello world! This is the file of words."

Your output file should look something like this:

hello 5
world! 6
This 4
is 2
the 3
file 4
of 2
words. 5

Part 3

With your partner, define a structure of type Product. The member variables should be code (int), wholesale_price (double), retail_price (double), number (int), and reorder_val (int).

Also with your partner discuss the pre and post-conditions for the following two functions. Then one partner should write the first function, initProduct, and the other partner should write the second function, outputProduct.


initProduct function

Write the definition of an initiation function:

       void initProduct(Product& new_product)

This function should initialize all member variables of the passed Product object.
The function should prompt the user for the product code, the wholesale price, the number in stock, and the reorder value for this product.
The function should only allow the user to enter values between 1000 and 9999 for the product code.
All products have a 10% markup, so the function should calculate the retail price as 1.1 times the wholesale price.

Write a driver program to test this function:

A driver program is just a main function that calls the function being tested and then outputs the values returned by the function to see if they are what is expected.


outputProduct function

Write the definition of an output function:

       void outputProduct(Product new_product)

This function should output your Product information in the following format:

code    W cost    R cost  # in stock    Reorder #
----   --------  -------- ----------    ---------

1111   $  10.00  $  11.00         15           50
2222   $ 100.00  $ 110.00        100           10
3333   $   5.00  $   5.50      12000          100

Your output function should just output one Product object. So, the preceding example would need 3 calls to this function to output the 3 products (1111, 2222, 3333).
The column headings would need to be output before these function calls. You should do this with a separate void function that when called just outputs the heading.

Write a driver program for the outputProduct function:

This again is just a main function that either gets values from the user for the Product member variables, code, wholesale_price, retail_price, number, and reorder_val or you can just give constant values to them in the program (just assign known values using assignment statements in main).
You should not have any code that calculates the retail_price, just get it from the user or assign a known value like all the other member variables.
This way, you already know what values are in the Product object you pass in to the output function.
If there is an error, you can be assured it comes from your output function or the call to the output function and not coming from some other function like the initProduct function.


Main function

Once the two functions have been tested and you feel confident they work correctly, you and your partner should now work on the main together that uses their function along with your function. If you get done testing your function before your partner, you should help them finish testing their function before starting on the main function.

Your program should prompt the user to enter info for 3 products. Then output the inventory in the above format. That means for this part you will declare 3 objects of type Product.


Part 4 (work on Part 4 and Part 5 together with your partner)

Modify your program so that it allows the user to enter as many products as they want and make it print the output to a file (inventory.txt) instead of the screen. That means you will only declare one object of type Product and put your functions in a do-while loop that asks the user if they want to continue.


Part 5

Modify your program so that it will output to another file (reorder.txt) all items that need to be reordered. A product needs to be reordered if its # in stock is lower than its reorder #. In the example output of Part 1, product 1111 would need to be reordered since its # in stock, 15, is lower than its reorder #, 50.


Challenge Exercises:

Please feel free to help your fellow labmates reach this point!!

The following exercises are for your own benefit. You are not required to do these. In fact, they may contain material not covered in class or lab yet. As such, the TAs will give priority to helping students on the previous sections.


Challenge 1: