CS107 - Lab 7


Overview

In this lab you will continue working with C++ functions. You will see problems that we have not explicitly discussed in class. You are expected to learn and figure out how things work by looking at the examples given throughout the lab. Before starting, make sure to review the examples and handouts discussed in class.

As you write your programs remember to pay attention to the following issues of style:


Problems

  1. Maintaining a bank account

    Write a program to maintain a bank account balance. The user is first asked to enter an initial balance (use a function to do this).

    Then the user is (repeatedly) asked to enter a transaction type:

    What would you like to do? 
    1 - deposit
    2 - withdrawal
    3 - balance inquiry
    4 - quit
    
    If the user enters anything else than 1,2,3,4 the program should print:
    This is not a valid option. Try again. 
    What would you like to do? 
    1 - deposit
    2 - withdrawal
    3 - balance inquiry
    4 - quit
    

    If the user presses 1, the program should further ask for the deposit amount and call a function to update the balance.

    If the user presses 2, the program should further ask for the withdrawal amount and call a function to update the balance.

    If the user presses 3, the progam should call a function to display the current balance.

    If the user presses 4, the program should print the final balance, say Good Bye, and quit. Otherwise it should go back to asking th euser to enter a transaction type.

    Structure your program so that the main function looks as follows:

    int main() {
       int option = 0; //some dummy initial value
       float balance; 
    
       //ask for initial balance; 
       //here you will call a function to initialize balance
    
       while (option != 4) {
          option = getOption();//this function should make sure the option is valid
          if (option == 1) {
              //here you will call the function to do a deposit
    	  //...
    	  };
          if (option == 2) {...};
          if (option == 3) {...};
          if (option == 4) {...};
        }
        return 0;
    }
    

    Your program should be dummy-proof. That is, it should handle special cases like entering a negative deposit amount or an withdrawal amount larger than current balance.

    Look carefully at the main program given above. What functions do you need to write, and what are their parameters? Do you need to pass them by value or by reference?

    There are several ways to write your functions. For instance, to initialize the balance, you may write a function

    //This function asks the user for an initial balance and return it
    float initializebalance() {
    //...
    }
    
    If you design the function this way, you will assign the value returned by the function to your variable that stores the current balance. Or, you could chose to write the function as:
    //The parameter balance is assumed to represent the current balance. This
    function asks the user for an initial balance and updates the current balance.
    void initializebalance(float &  balance) {
    //...
    }
    
    If you design the function this way, you will call the function with the current balance as argument, which will be updated by the function.

    To do a deposit you may write a function

    //This function asks the user for a deposit amount and returns this amount
    float deposit() {
    //...
    }
    
    or
    //The parameter balance is assumed to represent the current balance. This
    //function asks the user for a deposit amount and returns the new balance.
    //The parameter is not modified. 
    float deposit(float bal) {
    //...
    }
    
    or
    //The parameter balance is assumed to represent the current balance.
    //This function asks the user for a deposit amount and updates the
    //balance. 
    void deposit(float & bal) {
    //...
    }
    

    You can choose to write these functions any way you want. Note: In general it is preferred not to use parameters passed by reference if possible. Make sure to include comments in your code!


  2. Writing poetry

    In this program you will generate (random) poetry. Take a look at the following skeleton program, poet.cpp.

    Read it carefully and understand what each function does, and how all all functions work together to generate verses and stanzas.

    There is (just) one rule for generating a verse:

    verse ==> 'The' adjective noun verb adjective object
    Your task is to extend this program to generate better poetry. For instance, you could add more words to the word lists, add more word lists (prepositions, interjections, et cetera), add different rules for creating verses, add rhyme, do not use the same word twice in a verse, add stanzas with different number of verses, add more stanzas, et cetera. Use your imagination!! The program will be graded on the poem generated and on how sophisticated your program is.


What to turn in:

Send me only the .cpp files in the XCode project. Rename the main.cpp in the XCode folder to something from which I can easily identify the author, the lab number, and the problem number (no spaces in the name, please!)

If you work in a team submit only one file per team.

Make sure you include your name on the top line of all your .cpp programs!!