CS-11 Final Exam Practice
Complete the following programing problems. The point value of each problem is shown inside the parenthesis before the problem statement. You can use both Cygwin and TextPad to compile and run your code. Magic numbers are allowed and block comments are not required.
Important: Before you leave the actual final exam, you must upload your
.cppfile to the question from which you started this exam. Verify your source code file (
.cpp) is correct before uploading.
Partial Credit: Your code must compile for you to receive more than half-credit. Incomplete solutions may be awarded points depending on how close your answer is to the solution. If an entire answer is commented out then one half the points are taken off before the rest of the answer is evaluated. If only a line or two of code is commented out in the code for a problem, this indicates better understanding of the problem and more partial credit may be awarded.
Before you start: Download and save the following files using the specified names. Do not forget to save the text file in the UNIX format. You must add all the code you write to the
.cppfile except for the last problem.
After downloading, add your name in a comment at the top of the
- (5) In the file named
curio.cpp, code a
main()function. Compile your code to verify it works.
- (5) In the same file, write a non-member function with the following prototype that performs the actions stated in its comment block:/** Asks for the user's login name and password. Tests the entered name and password against the parameter values. If the login and password are not correct, prints an error message and asks the user to try again. When the correct login and password are entered, asks the user for their full name and provides a personalized greeting. @param user The user name to test against. @param password The password to test against. @return The name that the person entered. */ string login(string user, string password);
Call the function from
main()with an argument of your first name for
userand "abc123" for
password. Calling this function must produce output like the following:Username: ed Password: 123 Invalid login! Username: ed Password: abc123 Please enter your full name: Edward Parrish Welcome to my virtual store, Edward Parrish!
- (10) Using the starter code, write one
set()function for each member variable of the
Curioclass following the naming standards for these types of functions. In addition, code a function named
getValue()that has no parameters and returns the
quantity. Remember to add the
constkeyword to all accessor functions.
- (5) Write a default (no-parameter) constructor for the class that sets default values for all the numeric member variables.
- (5) Write an overloaded constructor for the class that sets all member variables to appropriate values passed to the parameters.
- (10) Write a member function named
print()that has no parameters and returns no values, but displays the data of the class to
coutall on one line like this:China plate 5.99 30 179.70
Remember to add the
constkeyword to this accessor function.
- (5) Write a member function for the class with the signature
void read(ifstream& fin), that has an
ifstreamparameter passed by reference, reads data from the
ifstreamobject and assigns the next set of the data read from the stream to the member variables in the order in which the file is listed.
- (5) In your
main()function, construct 3 objects of your class setting name, price and quantity. Call both constructors and all the set functions of your class to construct the objects. Add all the objects to a vector.
- (10) In your
main()function, develop the following menu to manage the operations of the store:Please choose one of the following operations: 0. Exit program 1. Report inventory 2. Load more data 3. Save data to file 4. Write report to file 5. Call recursive function Choice (0-4):
The menu must redisplay whenever a menu selection must be entered.
- (10) Write a non-member function (not part of the class) using the following prototype that performs the actions stated in its comment block:/** Displays all the Curios by calling print() for each Curio object. @param list The vector of Curio objects. */ void listData(vector<Curio>& list);
Call the function from
main()when the user selects menu item 1 (one) to produce the following report:Curios in my store: # Name Price Qty Value 1 China plate 5.99 30 179.70 2 Hand mirror 4.49 20 89.80 3 Glass apple 7.75 10 77.50
Notice the report spacing and justification, the number of decimal places for each column, and that all the decimal places line up. You must format the report correctly for full credit.
- (10) Write a non-member function (not part of the class) using the following prototype that performs the actions stated in its comment block:/** Loads the curio data from the specified file name into the vector of Curio objects. @param list The vector of Curio objects. @param fileName The name of the file from which to read. */ void loadData(vector<Curio>& list, string fileName);
Call this function from
main()when the user selects menu option 2 using the file name "curio.txt". Note that you must use an indefinite loop to read all the data from the file since the number of items in the file can change. Also, you must call the
read()function of each object within the loop to store the file data in the object.
- (10) Add a
saveData()non-member function to your program that stores a vector of
Curiodata into a file. Use the following prototype exactly for this function:/** Writes store data to an output file. @param list The vector of Curio objects. @param fileName The name of the file to which to write. */ void saveData(vector<Curio>& list, string fileName);
saveData()function when the user selects menu item 3 so that all the current vector elements are saved to a file named
saved.txt. The output of the
saveData()function must follow the same format as the
- (10) Write a non-member function (not part of the class) using the following prototype that performs the actions stated in its comment block:/** Writes a report to a file named report.txt. @param list The vector of Curio objects. @param username The users name. */ void writeReport(vector<Curio>& list, string username);
Call the function from
main()when the user selects menu item 4 to produce the following text in the
report.txtfile:Report for Edward Parrish: # Name Price Qty Value 1 China plate 5.99 30 179.70 2 Hand mirror 4.49 20 89.80 3 Glass apple 7.75 10 77.50
- (10) Write a non-member function using recursion that displays a string in reverse and that uses the following prototype. Do not use arrays or additional strings (besides the string parameter) to write the function./** Displays the string backwards. @param str The string to display in reverse. @param index An index needed for counting the recursion. */ void showBackwards(string str, int index);
Call the function from
main()when the user selects menu item 5 to start the recursion. After the user selects the menu item, call the function with the name entered during the login to display the following:Your name backwards is: hsirraP drawdE
- (10) As added practice, debug the following code and correct all the errors so that the program runs correctly. Not all errors are compiler errors so make sure to run the program and verify the output. Use the following data file to verify the compiled code.
Remember: In the real final exam you will need to upload your
.cppfile to the question from which you started this exam. You can only upload one file so upload your
curio.cppfile. It is a good idea to practice uploading with this practice exam.