CS107 - Lab 7

Due Wednesday 11/03 at the beginning of class.

Overview: In this lab you will continue working with C++ functions.

As you write your program please remember to pay attention to issues of style, including:


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.

    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

    //action: ask the user for an initial balance and return it
    float initializebalance() {
    //...
    }
    
    or
    //parameter: the current balance
    //action: ask the user for an initial balance and update balance
    void initializebalance(float balance) {
    //...
    }
    
    To do a deposit you may write a function
    //action: asks the user for a deposit amount and returns this amount 
    float deposit() {
    //...
    }
    
    or
    //parameter: the current balance
    //action: asks the user for a deposit amount and returns the new balance
    float deposit(float bal) {
    //...
    }
    
    or
    //parameter: the current balance
    //action: 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:

To submit the algorithms first rename (Xcode->File->Rename) the file main.cpp, then drag it to the Desktop, then drag it into the csci107bf04->drop-box. The names that you choose should be your loginname followed by problem number. For instance if I were to submit the file for the first problem i would rename it as ltoma1.cpp.

The drop-box has been set up such that you don't have read access (or you could copy the solutions of your colleagues), only write access. Therefore you will not be able to see whether your file has been submitted or not. Check with me in class to make sure your files got submitted.

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!!