Trouble writing files

I can get it to build but it wont run. what am i missing.

[code]
Put the code you need help with here.
//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);

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 userEnter = ' ';
do
{
cout << "Enter A to append " << endl;
cout << "S to display" << endl;
cout << "E to exit" << endl;
cin >> userEnter;
userEnter = toupper(userEnter);

switch (userEnter)
{
case 'A':
writeData();
break;
case'S':
readData();
break;
}

}while (userEnter != 'E');


}//end menu
void writeData(void) {
//Write the Address Info to a file
string name, address, city, zip;
ofstream outMystream(FileName, ios::app);
if (outMystream.is_open())
{
cout << "Enter your name: ";
getline(cin, name);
outMystream << name << "#";
}
outMystream.close();

//loop while user still has data to write to file
//eg outStream<<name<<”#”; //where # is the delimiter

}//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 inMyStream(FileName);

if (inMyStream.is_open())
{

//set character to use as a line between record displays
string recBreaks = "";
recBreaks.assign(20, '-');

int fieldCount = 0; //keep track of the number of fields read
int recordCount = 1; //keep track of the number of records read

//read the first field
fieldCount = 1;
string fieldBuffer;
getline(inMyStream, fieldBuffer, '#');

while (!inMyStream.eof())
{

//display the field
switch (fieldCount)
{
case 1:
cout << recBreaks << endl;
cout << "record # " << recordCount << endl;
cout << "Name...." << fieldBuffer << endl; break;
case 2:
cout << "Street.." << fieldBuffer << endl; break;
case 3:
cout << "City...." << fieldBuffer << endl; break;
case 4:
cout << "State..." << fieldBuffer << endl; break;
case 5:
cout << "Zip....." << fieldBuffer << endl;
fieldCount = 0;
recordCount++; break;
}

//read the next field
getline(inMyStream, fieldBuffer, '#');
fieldCount++;
}
cout << recBreaks << endl;

inMyStream.close();

}//end read data
}
Last edited on
You're missing and ending code tag! I laugh at my own puns.

Anyway, if you're talking about it not running at all, then make sure you're using whatever you're using correctly so it actually runs your code.

If you mean the code isn't working properly, well lets dive in. If you press A and want to change your name, it'll skip over your input because of getline. Simply, you have to press "A" then enter to get to writeData, meaning there's still a '\n' (new line character) in the buffer. getline sees the '\n' and then moves on, not caring that you didn't enter anything.

1
2
3
4
cout << "Enter your name: ";
cin.ignore(32767, '\n'); //Ignore the '\n' So that getline actually stops for user input!
getline(cin, name);
outMystream << name << "#";


Also, your only letting the user input their name's while also asking for all the other type of information. However, you output the information as if they've filled in for all the other fields, meaning the person's "name" comes out as their "state". It will be correct when you finish the program to ask for all data, but at the same time you'll need to make sure to error handle so that they ACTUALLY input the data, otherwise you'll display things with the wrong context.
Last edited on
Topic archived. No new replies allowed.