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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
/*
Christopher Carlos
Lab 7 Ex 3
This program will append and display records
in an address database.
*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void menu(void);
void writeData(void);
void readData(void);
string * split(string, char);
ofstream outFileObj("C:\\Users\\Chris\\Documents\\append_test.txt", ios::app);
ifstream inFileObj("C:\\Users\\Chris\\Documents\\append_test.txt", ios::app);
const char FileName[] = "C:\\Users\\Chris\\Documents\\append_test.txt";
string lineString;
char userChoice;
int main ()
{
menu();
return 0;
} //end main
void menu(void)
{
//allow user to choose to append records, display records or exit the program
cout << "1. (a)ppend " << endl
<< "2. (s)how records " << endl
<< "3. (e)xit ";
cout << "What would you like to do? ";
cin >> userChoice;
do
{
if ((userChoice == 'a') || (userChoice == 'A'))
{
writeData();
}
else if ((userChoice == 'S') || (userChoice == 's'))
{
readData();
}
else if ((userChoice == 'e') || (userChoice == 'E'))
{
cout << "Now exiting..." << endl;
return;
}
cout << "1. (a)ppend " << endl
<< "2. (s)how records " << endl
<< "3. (e)xit " << endl;
cout << "What would you like to do? ";
cin >> userChoice;
} while ((userChoice != 'e') || (userChoice != 'E'));
return;
}//end menu
void writeData(void)
{
//Write the Address Info to a file
//open file
string name, street, city, state, zipCode;
ofstream outStream(FileName, ios::app);
if (outStream.is_open())
{
//outFileObj << "Name" << "," << "Street" << "," << "City" << "," << "State" << "," << "Zip Code" << endl;
do
{
//write into file with outFileObj, read input with inFileObj
cout << "Name.......... ";
cin.ignore(100, '\n');
getline(cin, name);
cout << "\nStreet........ ";
getline(cin, street);
cout << "\nCity.......... ";
getline(cin, city);
cout << "\nState......... ";
getline(cin, state);
cout << "\nZip Code...... ";
getline(cin, zipCode);
outStream << name << "," << street << "," << city << "," << state << "," << zipCode << endl;
cout << "Would you like to append another record? (Y/N): ";
cin >> userChoice;
}while((userChoice == 'y') || (userChoice =='Y'));
outStream.close();//close file
}
else {cout << "Error: File Open Failed";}
return;
}//end write data
void readData(void)
{
//read data from a file
getline(inFileObj, lineString);
split(lineString, ',');
//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 unsigned 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 unsigned 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
|