Schedule‎ > ‎

CS-11 Practice Final

Instructions:

Complete the following programing problems. The point value of each problem is shown inside the parenthesis before the problem statement. You can use either Cygwin+Notepad++ or Code::Blocks to compile and run your code. Magic numbers are allowed and block comments are not required.

Partial Credit: On the Final,  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.

Grading:  The Practice Final is to help you prepare for the Final.  You won't get points for the Practice Final,  it will help you do better on the Final which is similar.  I will post a Solution to the Practice Final in Key Sample Code on Fri. May 18 by midnight.  I put up a Practice Final Assignment in Canvas so you could use it to upload your files for safekeeping when you go back & forth from school.  

Curio definition:  something (such as a decorative object) considered novel, rare, or bizarre (Merriam-Webster).  A Curio Store is like a gift store (ref. Sharon Strelitz).

STARTER FILES: There are 5 files required for this project:  curio.h,  curio.cpp and curioapp.cpp, curio.txt and Makefile (if you didn't use CodeBlocks).

1.  curio.h starter code:
#include <cstdlib>
#include <climits>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

class Curio {
public:

private:
    string name;
    double price;
    int quantity;
};

2.  curio.txt  data file for curio.cpp source code.  Don't forget to make UNIX line endings and add an extra blank line. 

3.  A sample Makefile for you to copy & modify:
# define target dependencies and files
productapp: productapp.o product.o
	g++ -o productapp productapp.o product.o

# define how each object file is to be built
productapp.o: productapp.cpp product.h
	g++ -c productapp.cpp -Wall -Wextra -Wpedantic -std=c++11  

product.o: product.cpp product.h
	g++ -c product.cpp -Wall -Wextra -Wpedantic -std=c++11 

# clean up
clean:
	rm -f productapp.exe *.o

4.  You must create the curio.cpp and curioapp.cpp files. 

Requirements Specifications

  1. (5) Use the starter code above to create your curio.h file.  In a file named curioapp.cpp, code a main() function and include the curio.h file.  Compile your code to verify it works.  Create a new file called curio.cpp which includes the curio.h file.
  2. (5) In curioapp.cpp, 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 user and "abc123" for password. Calling this function must produce output like the following:

    Username: Sharon
    Password: 123
    Invalid login!
    Username: Sharon
    Password: abc123
    Please enter your full name: Sharon Strelitz
    Welcome to my virtual store, Sharon Strelitz!
    
  3. (10) Using the starter code, write one get() and one set() function for each member variable of the Curio class following the naming standards for these types of functions. In addition, code a function named getValue() that has no parameters and returns the price times the quantity. Remember to add the const keyword to all accessor functions.
  4. (5) Write a default (no-parameter) constructor for the class that sets default values for all the numeric member variables.
  5. (5) Write an overloaded constructor for the class that sets all member variables to appropriate values passed to the parameters.
  6. (10) Write a member function named print() that has no parameters and returns no values, but displays the data of the class to cout all on one line like this:
    China plate         5.99    30    179.70
    

    Remember to add the const keyword to this accessor function.

  7. (5) Write a member function for the class with the signature void read(ifstream& fin), that has an ifstream parameter passed by reference, reads data from the ifstream object 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.
  8. (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.
  9. (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. (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.

  11. (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.

  12. (10) Add a saveData() non-member function to your program that stores a vector of Curio data 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);
    

    Call the 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 curio.txt input file.

  13. (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.txt file:

    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
    
  14. (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: ztilertS norahS
    
  15. (10) 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.
    • practiceBugs.cpp: code to debug
    • products.txt: data file  (remember to save with UNIX line endings and put an extra blank line at end of file).

Remember: In the real final exam you will need to upload your 4 files (curio.h,  curio.cpp and curioapp.cpp and Makefile (if you didn't use CodeBlocks)) in a .zip file to Canvas. 

 

Comments