I need help with this program, could you please help? You\'re working for a comp
ID: 3770375 • Letter: I
Question
I need help with this program, could you please help?
You're working for a company that wants to take client contact information as it is entered by a salesperson from a stack of business cards that he or she has collected, and output the information to a file (contacts.dat) in alphabetical order. The C++ program should prompt the salesperson for each of the following values for each card:
Last name
First name
Middle name or intial
Title
Company name
Street address
City
State
ZIP code
Phone number
Fax number
Email address
After the data are entered for each card, the user should be asked if there is another card to enter. Each card should be instered into a sorted list, whose key is the last name. After all the data are entered, iterate through the list, writing each card onto file contacts.dat.
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class ClientInfo {
public:
string LastName;
string Firstname;
string MiddleName; //Middle name or intial
string Title;
string CompanyName;
string Streetaddress;
string City;
string State;
string ZIPcode;
string Phonenumber;
string Faxnumber;
string Emailaddress;
};
void sort(ClientInfo clients[], int noOfClients) {
string temp = "";
for(int i = 0 ; i< noOfClients; i++)
for(int j=0; j < noOfClients-1; j++) {
if(clients[j+1].LastName.compare(clients[j].LastName) < 0) {
temp = clients[j].LastName;
clients[j].LastName = clients[j+1].LastName;
clients[j+1].LastName = temp;
}
}
}
void writeToFile(char* filename, ClientInfo clients[], int noOfClients) {
ofstream myfile;
myfile.open (filename);
for(int i = 0 ; i< noOfClients; i++) {
myfile << " Last name : " << clients[i].LastName
<< " First name: " << clients[i].Firstname
<< "Middle name or intial : " << clients[i].MiddleName
<< "Title : " << clients[i].Title
<< "Company name: " << clients[i].CompanyName
<< "Street address: " << clients[i].Streetaddress
<< "City: " << clients[i].City
<< "State: " << clients[i].State
<< "ZIP code: " << clients[i].ZIPcode
<< "Phone number: " << clients[i].Phonenumber
<< "Fax number: " << clients[i].Faxnumber
<< "Email address: "<< clients[i].Emailaddress
<<" ";
}
myfile.close();
}
int main() {
ClientInfo clients[1024];
int noOfClients = 0;
char ch;
while(ch != 'N' || ch != 'n') {
clients[noOfClients] = ClientInfo();
cout << "Enter client information " <<endl;
cout << "Last name" <<endl;
getline(cin, clients[noOfClients].LastName );
cout << "First name : " << endl;
getline(cin, clients[noOfClients].Firstname );
cout << " Middle name or intial : " <<endl;
getline(cin, clients[noOfClients].MiddleName );
cout << " Title : " <<endl;
getline(cin, clients[noOfClients].Title );
cout << " Company name : " <<endl;
getline(cin, clients[noOfClients].CompanyName );
cout << " Street address : " <<endl;
getline(cin, clients[noOfClients].Streetaddress );
cout << " City : " <<endl;
getline(cin, clients[noOfClients].City );
cout << " State : " <<endl;
getline(cin, clients[noOfClients].State );
cout << " ZIP code : " <<endl;
getline(cin, clients[noOfClients].ZIPcode );
cout << " Phone number : " <<endl;
getline(cin, clients[noOfClients].Phonenumber );
cout << " Fax number : " <<endl;
getline(cin, clients[noOfClients].Faxnumber );
cout << " Email address : " <<endl;
getline(cin, clients[noOfClients].Emailaddress );
noOfClients++;
<<" ";
cout << "Are you want to continue (y/n)?" <<endl;
cin >> ch;
if(ch == 'N' || ch == 'n')
break;
}
sort(clients,noOfClients );
writeToFile("D:/ravi/Cheg/contacts.txt",clients,noOfClients );
return 0;
}