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.
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.
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
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.
Write the definition of an initiation function:
Write a driver program to test this function:
Write the definition of an output function:
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:
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.
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.
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: