*********C++ ********* Create a C++ console application that will store and retr
ID: 3821225 • Letter: #
Question
*********C++ *********
Create a C++ console application that will store and retrieve names and addresses in a text file.
The program should do the following.
It should accept a series of names and addresses from the console.
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.
Read the records from the text file, and display them in a user-friendly format.
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 <iostream>
#include <fstream>
#include <string>
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
Step 2: Processing Logic
Using the pseudocode below, write the code that will meet the requirements.
The pseudocode for the writeData function is shown below.
Start
open the text file to append
start do while loop
Allow user to enter name
store name (using getline method)
Allow user to enter city
store city (using getline method)
.
.
write name, city, etc. to the file
end loop
close the file
End
The program input should appear similar to this.
Append Records
Name..........John Smith
Street.........902 Union Ave
City............Any Town
State...........TX
Zip Code......78552
"Enter another Record? (Y/N) "
The file structure should look like this.
John Smith, 902 Union Ave, Any Town, TX, 79552
Eric Jones, 345 State Way, Fresno, CA, 93432
...
The file output should appear similar to the following.
Show Records
__________________________________________
Record #1
Name...........John Smith
Street..........902 Union Ave
City.............Any Town
State...........TX
Zip Code......78552
__________________________________________
Record #2
Name...........Eric Jones
Street..........345 State Way
City.............Fresno
State...........CA
Zip Code.......93432
__________________________________________
(A)ppend Records, (S)how Records, (E)xit
Create a C++ console application that will store and retrieve names and addresses in a text file.
The program should do the following.
It should accept a series of names and addresses from the console.
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.
Read the records from the text file, and display them in a user-friendly format.
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 <iostream>
#include <fstream>
#include <string>
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
Step 2: Processing Logic
Using the pseudocode below, write the code that will meet the requirements.
The pseudocode for the writeData function is shown below.
Start
open the text file to append
start do while loop
Allow user to enter name
store name (using getline method)
Allow user to enter city
store city (using getline method)
.
.
write name, city, etc. to the file
end loop
close the file
End
The program input should appear similar to this.
Append Records
Name..........John Smith
Street.........902 Union Ave
City............Any Town
State...........TX
Zip Code......78552
"Enter another Record? (Y/N) "
The file structure should look like this.
John Smith, 902 Union Ave, Any Town, TX, 79552
Eric Jones, 345 State Way, Fresno, CA, 93432
...
The file output should appear similar to the following.
Show Records
__________________________________________
Record #1
Name...........John Smith
Street..........902 Union Ave
City.............Any Town
State...........TX
Zip Code......78552
__________________________________________
Record #2
Name...........Eric Jones
Street..........345 State Way
City.............Fresno
State...........CA
Zip Code.......93432
__________________________________________
(A)ppend Records, (S)how Records, (E)xit
Explanation / Answer
C++ console application that will store and retrieve names and addresses in a text file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void menu(void);
void writeData(void);
void readData(void);
string * split(string, char);
const char FileName[] = "c://TestAddress/TestAddress.txt"; //Os level path of the text file.
ifstream myAddress(FileName);
string name = " ";
string address = " ";
string street= " ";
string city = " ";
string state = " ";
string zipCode = " ";
int record = 0;
ofstream outMyAddress(FileName, ios::out);
int main ()
{
menu();
return 0;
} //end main
void menu(void)
{
//allow user to choose to append records, display records or exit the program
char userChoice = ' ';
//Display Menu
system("cls");
cout << " Name and Address database:" << endl;
cout << endl;
cout << " (A)ppend record, (S)how Record, (E)xit:";
cin >> userChoice;
//Users Choice
switch (userChoice)
{
case 'a':
case 'A'://Append Record
myAddress.open (FileName, ios::app);
if (myAddress.is_open())
{
writeData();
}
break;
case 's':
case 'S'://Show record
myAddress.open (FileName, ios:: in);
if (myAddress.is_open())
{
readData();
}
break;
case 'e':
case 'E'://Exit
myAddress.close();
break;
default:
cout << "Invalid choice" << endl;
cout << endl << endl << endl;
break;
}
return;
}//end menu
void writeData(void) //Write the Address Info to a file
{
char answer = ' ';
char response = ' ';
if(myAddress.is_open())
{
//entering loop
while (answer != 'n' || answer != 'N')
{
cout << endl;
getline(cin, name);
cout << " Enter name: ";
getline(cin, name);
cout << " Enter Street: ";
getline(cin, street);
cout << " Enter City: ";
getline(cin, city);
cout << " Enter State: ";
getline(cin, state);
cout << " Enter Zip Code: ";
getline(cin, zipCode);
cout << endl;
outMyAddress << name << ", " << street << " ," << city << ", " << state << " ," << zipCode << endl;
record++;
cout << " Would you like to enter another record? (Y/N)" << endl;
cin >> response;
if (response == 'n' || response == 'N')
{
return menu();
}
}
}
myAddress.close();
}//end write data
void readData(void)
{
ifstream inMyAddress(FileName, ios::in);
if(myAddress.is_open())
{
string firstLine;
inMyAddress >> firstLine;
getline (myAddress, firstLine, ' ');
cout << endl;
cout << "Reading the file(s)..." << endl;
cout << endl;
//read data from a file
//use the split function to break a deliminated line of text into fields
cout << "Record #: " << record << endl;
string *theField = split(firstLine, ',');
cout << "Name......" << theField[0] << endl;
cout << "Street......" << theField[1] << endl;
cout << "City......" << theField[2] << endl;
cout << "State......" << theField[3] << endl;
cout << "Zip Code......" << theField[4] << endl;
}
inMyAddress.close();
system("pause");
return menu();
}//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 < ((int)theLine.size()); i++){
if (theLine[i] == theDeliminator)
splitCount++;
return 0;
}
splitCount++;
//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 < ((int)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;
return theFieldArray;
} //end split
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.