Assignment 21
Due Saturday, December 10 at midnight on Canvas


Assignment 21.1 Social Security Numbers 2 (10 pts)

  • Write a program that reads in a series of numbers, as strings, from a file and converts them to valid social security numbers.
  • The numbers are contained in file is called numbers.txt.
  • The numbers must read into the file using a loop, and stored as an array of strings. Note that you may assume that the size of the array is known ahead of time to be 10.
  • As it reads in each number, it should convert the number to a valid SSN in the format XXX-XX-XXXX by calling a function named formatSSN.
  • Here is the information about this function:
    • The function is named formatSSN
    • It take in a string variable by reference
    • It alters the string to be a properly formatted social security number, in the format XXX-XX-XXXX
    • This function must use the substring function to format the numbers
    • It returns nothing.
  • The program must also contain a second function which writes out the contents of the array in a file named socials.txt
  • Here is the information about the second function:
    • The function is named printArray
    • It takes in two parameters - one is an array of strings, and the second is an int for the size of the array
    • It creates a new output file stream variable and opens up a file called socials.txt
    • 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.
    • It then closes the file.
    • It returns nothing.
  • Copy and paste the starter code below into a new file called socialArray.cpp

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

void formatSSN(string& number);
//converts a string to a formatted social security number

void printArray(string nums[], int size);
//prints out an array to a file named socials.txt

int main() {
    //write the program logic here
    string number;


}

//write your functions here

  • Copy and paste the below data into a file called numbers.txt:

555666777
333444555
111888494
333222567
123444555
888999000
111666434
222456333
111222999
456889939

  • Below is the output that your program should give inside of the socials.txt file
555-66-6777
333-44-4555
111-88-8494
333-22-2567
123-44-4555
888-99-9000
111-66-6434
222-45-6333
111-22-2999
456-88-9939
Once you have the correct output inside of the socials.txt file, submit your socialArray.cpp file to Canvas


Assignment 21.2: Customer Data 2 (10 pts)
  • Write a program that reads in a series of names, along with their gender, from a file.
  • The file is called names.txt.
  • The program uses a loop to read in each name from the file and store each person's title (Mr. or Ms.) + name in an array called customers.
  • The program should then call two functions.
  • The first function:
    • The function is named capitalizeName
    • It take in a string parameter BY REFERENCE
    • It capitalizes all of the letters in the string
    • This function must use a for loop. It cannot call any outside functions that we have not discussed in this class.
    • It returns nothing.
  • The second function:
    • The function is named printArray
    • It takes in two parameters - one is an array of strings and the second is an int for the size
    • It creates a new output file stream variable and opens up a file called customers.txt
    • 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.
    • It then closes the file
    • It returns nothing
  • Inside of main, read in the values in the names.txt file using a while loop (required).
  • Alter each name to have Mr or Ms in front of the name depending on the gender specified beneath the name in the names.txt file. You must also capitalize each name and store it in an array of strings.
  • Note that you may assume that the size of the array is known ahead of time to be 8.
  • Finally, call the printArray function, to write the array into a file called customers.txt
  • Copy and paste the starter code into a new C++ file called contactsArray.cpp

#include <iostream>
//add two more libraries here for file I/O
using namespace std;

void capitalizeName(string& name);
//Capitalizes all of the letters in a string

void printArray(string names[], int size);
//Writes the contents of an array of strings into a file called customers.txt

int main() {
    string name, gender;
    return 0;
   
}

//Write your two functions here

  • Copy and paste the below data into a file called names.txt:
Barbara Stanton
F
Timoteo Martinez
M
Grace Gu
F
Xing Xiao
M
Dung Kim
F
Tanya White
F
Alan Ngo
M
Abir Fadel
M
  • Below is the output that your program should give in a file called customers.txt:
MS. BARBARA STANTON
MR. TIMOTEO MARTINEZ
MS. GRACE GU
MR. XING XIAO
MS. DUNG KIM
MS. TANYA WHITE
MR. ALAN NGO
MR. ABIR FADEL
  • When your code is providing the correct output in the contacts.txt file, upload your contactsArray.cpp file to Canvas



Assignment 20.1 Social Security Numbers (10 pts)

  • Write a program that reads in a series of numbers, as strings, from a file and converts them to valid social security numbers.
  • The numbers are contained in file is called numbers.txt.
  • This program must use a loop to read in each number from the file and store it temporarily as a string variable
  • As it reads in each number, it should convert the number to a valid SSN in the format XXX-XX-XXXX by calling a function named formatSSN.
  • Here is the information about this function:
    • The function is named formatSSN
    • It take in a string variable by reference
    • It alters the string to be a properly formatted social security number, in the format XXX-XX-XXXX
    • This function must use the substring function to format the numbers
    • It returns nothing.
  • The program will use a loop to read in each number and then call the formatSSN function on each of the strings from the file.
  • After each string is converted to a formatted social security number, write it out to a file called socials.txt
  • Copy and paste the starter code below into a new file called social.cpp

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

void formatSSN(string& number);
//converts a string to a formatted social security number

int main() {
    //write the program logic here
    string number;


}

//write your function here

  • Copy and paste the below data into a file called numbers.txt:

555666777
333444555
111888494
333222567
123444555
888999000
111666434
222456333
111222999
456889939

  • Below is the output that your program should give inside of the socials.txt file
555-66-6777
333-44-4555
111-88-8494
333-22-2567
123-44-4555
888-99-9000
111-66-6434
222-45-6333
111-22-2999
456-88-9939
Once you have the correct output inside of the socials.txt file, submit your social.cpp file to Canvas


Assignment 20.2: Customer Data (10 pts)
  • 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:

James Brown
M
4085678432
Leanna Perez
F
4087654433
Xing Li
M
4083214555
Mohammed Abbas
M
4083134444
Kumari Chakrabarti
F
4086667777
Shakil Smith
M
4082123333
Jung Ahrin
F
4089257788
Pedro Martinez
M
4086162323
  • 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
  • When you are finished, contacts.txt should contain the following information:

Mr. James Brown
408-567-8432
Ms. Leanna Perez
408-765-4433
Mr. Xing Li
408-321-4555
Mr. Mohammed Abbas
408-313-4444
Ms. Kumari Chakrabarti
408-666-7777
Mr. Shakil Smith
408-212-3333
Ms. Jung Ahrin
408-925-7788
Mr. Pedro Martinez
408-616-2323
  • 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.
  • In addition, the unformatted phone numbers in customers.txt must be formatted as XXX-XXX-XXXX inside of contacts.txt.
  • To format the phone numbers, you must write a function with the following signature:

void formatPhone(string& phone);
//Takes an unformatted 10-digit phone number as a string
//Alters the string to be formatted like so XXX-XXX-XXXX
  • Additionally, you must use a while loop to read in the customer information.
  • Copy and paste the starter code into a new C++ file called contacts.cpp

#include <iostream>
//add two more libraries here for file I/O
using namespace std;

void formatPhone(string& phone);
//Takes an unformatted 10-digit phone number as a string
//Formats the string in the format XXX-XXX-XXXX

int main() {
    string name, gender, phone;
   
    //declare your input file stream variable here
    //open the customer.txt file
    //check for failure
   
    //decare your output file stream variable here
    //open contacts.txt file
    //check for failure
   
    while () { //Will you need fin >> or getline() inside the while loop test to read in the name?
        getline(fin, gender);
        //one more call to getline here to read in the number
        //if statement here
       
        //function call to formatPhone here to alter the phone number

    }
   
    //close your input and output file streams
   
    return 0;
   
}


  • When your code is providing the correct output in the contacts.txt file, upload your contacts.cpp file to Canvas



Assignment 20.3: Number of Numbers (10 pts)

  • For this assignment you will be working with arrays.
  • Open a new C++ file called nums.cpp
  • Prompt a user to enter in the size of the array (the number of numbers).
  • Next, using a for loop, prompt the user to enter that many numbers, and store each one in your array until your array is at full capacity.
  • Using the same, or a second, for loop, sum the numbers and report their sum to the user.
  • Also, multiply the numbers together and report their product to the user.
  • The sum and the product should be printed to one decimal value only.
  • Your program should work identically to the sample output below:

Enter the number of numbers: 4
Enter 4 numbers:

Number 1: 3.2
Number 2: 6.7
Number 3: 2.9
Number 4: 4.6

The sum of the numbers is: 17.4
And the product is: 286.0


  • Another run of the program might give the following output:

Enter the number of numbers: 8
Enter 8 numbers:

Number 1: 2.1
Number 2: -5.6
Number 3: 9.0
Number 4: 8.7
Number 5: -2.2
Number 6: 8.2
Number 7: 9.5
Number 8: 1.4

The sum of the numbers is: 31.1
And the product is: 220931.3

  • Upload nums.cpp to Canvas when you are finished.