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 either append records to the file, display the records or exit the application.
It seems when I run the program first off the menu doesn't work hehe I am more worried about the output atm. It allows you to enter the data, but when it goes to display the output it errors out after the address. Any ideas?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#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.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 choice = 'S';
cout << "What would you like to do with these records: " << endl << endl;
cout << "(A)ppend records, (S)how records, or (E)xit" << endl;
cin >> choice;
cin.ignore(1, '\n');
writeData();
cout << endl;
cout << endl;
readData();
}//end menu
void writeData(void){
//Write the Address Info to a file
char choice = 'Y';
string name = "";
string street = "";
string city = "";
string state = "";
int zipCode = 0;
ofstream outMyStream (FileName, ios::app);
do {
cout << "\nEnter The Name: ";
getline(cin, name);
cout << "\nEnter The Street: ";
getline(cin, street);
cout << "\nEnter The City: ";
getline(cin, city);
cout << "\nEnter The State: ";
getline(cin, state);
cout << "\nEnter The Code: ";
cin >> zipCode;
//write to the file
outMyStream << name << "," << street;
cout << "\nEnter another Record? (Y/N) ";
cin >> choice;
//discard any newline character in keyboard buffer
cin.ignore(1, '\n');
}
while (choice == 'Y');
outMyStream.close();
}//end write data
void readData(void){
//read data from a file
ifstream inMyStream (FileName);
//read the first record
string lineBuffer;
while (!inMyStream.eof() ){
getline (inMyStream, lineBuffer, '\n');
//divide the line into seperate fields
string *theFields = split(lineBuffer, ',');
cout << "Name...... " << theFields[0] << endl;
cout << "Street...." << theFields[1] << endl;
cout << "City......" << theFields[2] << endl;
cout << "State....." << theFields[3] << endl;
cout << "Zip code.." << theFields[4] << endl;
}
inMyStream.close();
}//end read data
string * split(string theLine, char theDeliminator){
//Break the line 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 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(unsigned int i = 0; i < theLine.size(); i++){
if (theLine[i] == ',')
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( unsigned int i = 0; i < theLine.size(); i++ ){
//read each character and look 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
|
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 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:
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