Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone check my code. Its not showing the records in C++ i cant find what im missing. Thanks #include #include #include using namespace std;

Can someone check my code. Its not showing the records in C++ i cant find what im missing. Thanks

#include

#include

#include

using namespace std;

void menu(void);

void writeData(void);

void readData(void);

string * split(string, char);

const char FileName[] = "TestAddress.txt";

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

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 choice = ' ';

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 >> choice;

if (choice == 'n' || choice == '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 <

//read data from a file

//use the split function to break a deliminated line of text into fields

string *theField = split(firstLine, ',');

cout << "Record #: " << record << endl;

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();

cin.ignore(2);

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions