C++ Assignment Assume you work for a company that tracks customer information, i
ID: 3908019 • Letter: C
Question
C++ Assignment
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
#include
#include
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
Explanation / Answer
#include<iostream>
#include<string>
#include<fstream>
#include <stdlib.h>
#include <ctype.h>
using namespace std;
string capitalize(string a){
string s = "";
for (int i = 0; i<a.length(); i++){
if (isalpha(a[i])){
s = s + (char)toupper(a[i]);
}
else {
s = s + a[i];
}
}
return s;
}
void printArray(string data[], int n, string f){
ofstream fout(f.c_str());
for (int i = 0; i<n; i++){
fout << data[i] << endl;
}
fout.close();
}
int main(){
ifstream fin("input.txt");
if (!fin){
cout << "Error in opening ";
return 0;
}
string data[8];
string data1[8];
string line,line1,line2;
int count1 = 0;
int count2 = 0;
while (getline(fin,line)){
getline(fin,line1);
getline(fin,line2);
if (line2[0] == '8' && line2[0] == '3' && line2[0] == '1'){
string s;
if (line1 == "M"){
s = "MR";
}
if (line1 == "F"){
s = "MS";
}
if (line1 == "X"){
s = "MX";
}
s = s+ line;
data[count1] = capitalize(s);
count1++;
}
if (line2[0] == '4' && line2[0] == '0' && line2[0] == '8'){
string s;
if (line1 == "M"){
s = "MR";
}
if (line1 == "F"){
s = "MS";
}
if (line1 == "X"){
s = "MX";
}
s = s+ line;
data1[count2] = capitalize(s);
count2++;
}
}
printArray(data, count1, "SJCustomers.txt");
printArray(data1, count2, "SCCustomers.txt");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.