Final Exam Preparation


Part 1:

Final Practice
Final Practice Answer Key


Part 2: One of the following programs will appear on your final

Customer Data: Area Codes

  • Assume you work for a company that tracks customer information, including name, gender and phone numbers
  • Your company has a file called customers.txt which contains the following information:
Jiming Wu
F
4082123458
James Brown
M
8315678432
Leanna Perez
F
4087654433
Xing Li
M
8313214555
Stacey Cahill
O
8312123333
Mohammed Abbas
M
4083134444
Kumari Chakrabarti
F
4086667777
Shakil Smith
M
4082123333
Jung Ahrin
F
8319257788
Pedro Martinez
M
4086162323
Ally Gu
O
4089256776
Tamara White
F
8317778978
Alvin Ngo
M
4089256677
Abir Fadel
M
8316645325
Brad Feinman
M
8312023443
Xiaohang Yue
M
8318990033
  • Half of the 16 customers in this file have a phone number with an 831 area code.
  • Half have a phone numbers have a 408 area code.
  • Write a program that reads in all of the customer information using a loop, and stores the names of the customers in two different string arrays - each array of SIZE 8.
  • One array should store the customers with a 408 area code and one array should store the customers with an 831 area code.
  • Hint: Use substr to determine the area code of the customer's phone number.
  • When the customers are stored in each array, they should be stored as MR., MS. or MX. depending on the gender below their name in the file.
  • In other words, whenever the customers.txt file contains an M below the customer name, you should place the word MR. before the name.
  • Whenever the customers.txt file contains an F below the customer name, you should place the word MS. before the name.
  • Finally, whenever the file contains an O below the customer name, you should place the word MX. before the name.
  • You must also capitalize each name by calling the capitalizeName function.
  • I recommend calling capitalizeName before storing the name inside the array.
  • Finally, you will be required to write the two arrays to a file.
  • Each array should be written to a different file.
  • The 408 area code array should be written to a file called SJCustomers.txt
  • The 831 area code array should be written to a file called SCCustomers.txt
  • You must write the arrays to the file by calling the printArray function.
  • Note that you will need to call printArray twice - once for each array.
  • Note that for all file I/O you are required to check for failure!
Function requirements
  • capitalizeName Function:
    • The function is named capitalizeName

    • It take in a string parameter BY REFERENCE

    • It capitalizes all of the letters in the string using ASCII

    • This function must use a for loop. It cannot call any outside functions that we have not discussed in this class.

    • It returns nothing.

  • printArray Function:
    • The function is named printArray
    • It takes in three parameters:
      •  the first parameter is an array of strings,
      • the second parameter is an int for the size of the array,
      • the third parameter is a string for the name of a text file in which to write out the data
    • The function must creates a new output file stream variable to open the file whose name is passed in as a parameter
    • It also checks for failure if the file cannot open
    • It uses a for loop to print out the contents of the array in the file, with each element on its own line
    • It then closes the output file stream.
    • It returns nothing.
  • On the final, you will be required to write a prototype, comment and full function definition for each function
  • The prototypes and comments are given in the starter code below for this assignment, but will not be provided on the final.

  • Copy and paste the starter code below into a new file called areaCodes.cpp

/*
* Name
* Section info
*/
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

void capitalizeName(string& name);
//uses ASCII to capitalize all of the letters in the string name
//returns nothing

void printArray(string names[], int size, string fileName);
//prints out an array to the given fileName

int main() {
    //variables used when reading in from file
    string name, gender, number;
   
    //declare your 2 arrays here - one for each area code
    const int SIZE = ???;
   
      //code to open input file here
 
    //to keep track of index number in each array
    int i = 0;
    int j = 0;
    while(getline(fin, name)) {
        //read in gender
        //if statements related to gender
     
        //call capitalizeName
       
        //read in area code
        //if statements to determine area code and assign to correct array
       
    }

    //call printArray function twice
    //close input file stream
}

//write your functions here

  • Below is the output that your program should give inside of the SJCustomers.txt file
MS. JIMING WU
MS. LEANNA PEREZ
MR. MOHAMMED ABBAS
MS. KUMARI CHAKRABARTI
MR. SHAKIL SMITH
MR. PEDRO MARTINEZ
MX. ALLY GU
MR. ALVIN NGO

  • It should also give the below is the output in a second file called SCCustomers.txt file
MR. JAMES BROWN
MR. XING LI
MX. STACEY CAHILL
MS. JUNG AHRIN
MS. TAMARA WHITE
MR. ABIR FADEL
MR. BRAD FEINMAN
MR. XIAOHANG YUE

Customer Data: Phone Numbers
  • Assume you work for a company that tracks customer information, including name, gender and phone numbers
  • Your company has a file called customers.txt which contains the following information:

Jiming Wu
F
4082123458
James Brown
M
8315678432
Leanna Perez
F
4087654433
Xing Li
M
8313214555
Stacey Cahill
O
8312123333
Mohammed Abbas
M
4083134444
Kumari Chakrabarti
F
4086667777
Shakil Smith
M
4082123333
Jung Ahrin
F
8319257788
Pedro Martinez
M
4086162323
Ally Gu
O
4089256776
Tamara White
F
8317778978
Alvin Ngo
M
4089256677
Abir Fadel
M
8316645325
Brad Feinman
M
8312023443
Xiaohang Yue
M
8318990033
  • Your task is to read in the data from the above file using a loop, format the data, and write it into a new file called contacts.txt
  • Note that for all file I/O you are required to check for failure!
  • When you are finished, contacts.txt should contain the following information:

Ms. Jiming Wu
Phone: (408) 212-3458
Mr. James Brown
Phone: (831) 567-8432
Ms. Leanna Perez
Phone: (408) 765-4433
Mr. Xing Li
Phone: (831) 321-4555
Mx. Stacey Cahill
Phone: (831) 212-3333
Mr. Mohammed Abbas
Phone: (408) 313-4444
Ms. Kumari Chakrabarti
Phone: (408) 666-7777
Mr. Shakil Smith
Phone: (408) 212-3333
Ms. Jung Ahrin
Phone: (831) 925-7788
Mr. Pedro Martinez
Phone: (408) 616-2323
Mx. Ally Gu
Phone: (408) 925-6776
Ms. Tamara White
Phone: (831) 777-8978
Mr. Alvin Ngo
Phone: (408) 925-6677
Mr. Abir Fadel
Phone: (831) 664-5325
Mr. Brad Feinman
Phone: (831) 202-3443
Mr. Xiaohang Yue
Phone: (831) 899-0033


  • In other words, whenever the customers.txt file contains an M below the customer name, the contacts.txt file should have the word Mr. before the name.
  • And, whenever the customers.txt file contains an F below the customer name, the contacts.txt file should have the word Ms. before the name.
  • Finally, whenever the file contains an O below the customer name, the contacts.txt file should have the word Mx. before the name.
  • In addition, the unformatted phone numbers in customers.txt must be formatted as (XXX) XXX-XXXX inside of contacts.txt.
Array requirements
  • You are required to store the names and phones in two separate - but parallel - arrays. In other words, the same index in each array should store the information about one unique customer. For example, index 0 in the names array should store the name of Jiming Wu and index 0 in the phones array should store her phone number.
  • You can assume for the purpose of the program that your arrays are both of SIZE 16 when you declare them.

Function requirements
  • formatPhone function:
    • The function is named formatPhone

    • It take in a string parameter BY REFERENCE

    • It alters the string to be a properly formatted phone number (XXX) XXX-XXXX

    • It cannot call any outside functions that we have not discussed in this class.

    • It returns nothing.

  • printArrays Function:
    • The function is named printArrays
    • It takes in four parameters:
      •  the first and second parameters are an array of strings,
      • the third parameter is an int for the size of both the arrays,
      • the fourth parameter is a string for the name of a text file in which to write out the data
    • The function must creates a new output file stream variable to open the file whose name is passed in as a parameter
    • It also checks for failure if the file cannot open
    • It uses a for loop to print out the contents of the array in the file, with each element on its own line, in the following format:
names[i]
Phone: phones[i]
    • It then closes the output file stream.
    • It returns nothing.
  • On the final, you will be required to write a prototype, comment and full function definition for each function
  • The prototypes and comments are given in the starter code below

  • Copy and paste the starter code into a new C++ file called phoneContacts.cpp
/*
* Name
* Section info
*/

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

void formatPhone(string& phone);
//formats a phone number to be in the following format (XXX) XXX-XXX
//returns nothing

void printArrays(string names[], string phones[], int size, string fileName);
//prints out the contents of two arrays to the given fileName
//using the following format:
//names[i]
//Phone: numbers[i]

int main() {
    //write the program logic here
    string name, gender, number;
    //declare your 2 arrays here - one for names and one for phone numbers
    const int SIZE = ??;
  
    //code to read from customers.txt here
    //don't forget to check for input file failure!

  
    int i = 0;//to keep track of index in the arrays
    while(?????) {
        //read in gender
        //if statements related to gender
          //assign to names array
        //read in phone
        //call formatPhone
        //assign to phone number array
       
 }

     //call printArrays here
    //close input file stream

     
}

//write your functions here