Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program should do the following. 1. It should accept a series of names and a

ID: 3793095 • Letter: T

Question

The program should do the following. 1. It should accept a series of names and addresses from the console. 2. The user's input should be written to a text file in the CSV format described in the lecture, but do not include the field names in the first row of the file. 3. Read the records from the text file, and display them in a user-friendly format. 4. Provide a menu to allow the user to append records to the file, display the records, or exit the application. Build upon the code below to complete the assignment.

//Specification: Append and display records in a address database #include #include #include using namespace std; void menu(void); void writeData(void); void readData(void); string * split(string, char); const char FileName[] = "TestAddress.txt"; int main () { menu(); return 0; } //end main void menu(void) { //allow user to choose to append records, display records or exit the program }//end menu void writeData(void){ //Write the Address Info to a file }//end write data void readData(void){ //read data from a file //use the split function to break a //deliminated line of text into fields }//end read data string * split(string theLine, char theDeliminator){ //Break theline into fields and save the fields to an array. //Each field will occupy one element in a character array. //theLine is a string with fields separated with theDeliminator character. //Assumes the last field in the string is terminated with a newline. //Useage: string *theFields = split(lineBuffer, ','); //determine how many splits there will be so we can size our array int splitCount = 0; for(int i = 0; i < theLine.size(); i++){ if (theLine[i] == theDeliminator) splitCount++; } splitCount++; //add one more to the count because there is not an ending comma //create an array to hold the fields string* theFieldArray; theFieldArray = new string[splitCount]; //split the string into seperate fields string theField = ""; int commaCount = 0; for(int i = 0; i < theLine.size(); i++){ //read each character and look for the deliminator if (theLine[i] != theDeliminator) { theField += theLine[i]; //build the field } else { //the deliminator was hit so save to the field to the array theFieldArray[commaCount] = theField; //save the field to the array theField = ""; commaCount++; } } theFieldArray[commaCount] = theField; //the last field is not marked with a comma... return theFieldArray; } //end split

Explanation / Answer

Code:

//Specification: Append and display records in a address database

#include<iostream>
#include<string>
#include<fstream>
#include<cstring>
using namespace std;

void menu(void);
void writeData(void);
void readData(void);
string * split(string, char);
const char FileName[] = "TestAddress.txt";

int main ()

{
menu();
return 0;

} //end main

void menu(void)
{


//allow user to choose to append records, display records or exit the program
char ch;
while(1){
   cout<<"============================"<<endl;
   cout<<"1. Append data to file (a)"<<endl;
   cout<<"2. Display data (d)"<<endl;
   cout<<"3. Exit (e)"<<endl;
   cin>>ch;

   switch(ch){
       case 'a':
           writeData();
           break;  
       case 'd':
           readData();
           break;
       default:
           return;
       }
}

}//end menu

void writeData(void){
//Write the Address Info to a file

string str;

ofstream ofs(FileName,ios::app);

if(!ofs.is_open()){
   cout<<"Error in opening the file"<<endl;
   return;
}

cout<<"Enter data:";

getline(cin,str,'!');
   ofs<<str<<endl;


ofs.close();

}//end write data

void readData(void){

//read data from a file
//use the split function to break a
//deliminated line of text into fields

ifstream ifs(FileName);

char str[256];
string *tmp;
cout<<"The data from the file is"<<endl;
cout<<"========================================="<<endl;
while(ifs.getline(str,256))
{
    tmp=split(str,',');
}
cout<<"========================================="<<endl;


}//end read data

string * split(string theLine, char theDeliminator){

//Break theline into fields and save the fields to an array.
//Each field will occupy one element in a character array.
//theLine is a string with fields separated with theDeliminator character.
//Assumes the last field in the string is terminated with a newline.
//Useage: string *theFields = split(lineBuffer, ',');
//determine how many splits there will be so we can size our array
int splitCount = 0;
for(int i = 0; i < theLine.size(); i++){
if (theLine[i] == theDeliminator)
   splitCount++;
}
splitCount++; //add one more to the count because there is not an ending comma
//create an array to hold the fields
string* theFieldArray;
theFieldArray = new string[splitCount]; //split the string into seperate fields
string theField = "";
int commaCount = 0;
for(int i = 0; i < theLine.size(); i++){
//read each character and look for the deliminator
if (theLine[i] != theDeliminator) {
theField += theLine[i]; //build the field
}
else {
   //the deliminator was hit so save to the field to the array
   theFieldArray[commaCount] = theField; //save the field to the array
   theField = ""; commaCount++;
}
}
theFieldArray[commaCount] = theField;
//the last field is not marked with a comma...

for(int i=0;i<=commaCount;i++)
   cout<<theFieldArray[i]<<" ";
cout<<endl;
return theFieldArray;

} //end split

Output:

============================
1. Append data to file (a)
2. Display data (d)
3. Exit (e)
a
Enter data:
Marshal,Microsoft,300000 !
============================
1. Append data to file (a)
2. Display data (d)
3. Exit (e)
d
The data from the file is
=========================================


Bob HCLTechnologies 25000


Marshal Microsoft 300000
=========================================
============================
1. Append data to file (a)
2. Display data (d)
3. Exit (e)

e