Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,069 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,602 people online right now. Registration is fast and FREE... Join Now!




Help- can't get file to exit What am I doing wrong?

 
Reply to this topicStart new topic

Help- can't get file to exit What am I doing wrong?

clevs
21 Apr, 2008 - 03:08 PM
Post #1

New D.I.C Head
*

Joined: 1 Apr, 2007
Posts: 13


My Contributions
I have to create a program with will produce a file that can be used like a phone book. The program will prompt user to enter first, last name and phone number. The loop should stop when done is entered as the first name. I can get the program to loop and get correct output but can't get the program to stop when I enter done. It asks for first name, I enter done and it continues to ask for last name then nothing further is prompted for entry but doesn't exit
[code]




/* This program is being used to create a phone list and end by typing in done as first name. */


//Header Files

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

void printtitle()
{
cout << "*******************************************" << endl;
cout << "************ PHONE BOOK *******************" << endl;
cout << "*******************************************" << endl;
}
int main()
{
//declare variables
string first_name;
string last_name;
string number;
string done;
ofstream outfile;


do
{
//open file for appending
outfile.open ("C:\\IO_Files\\Address_Book.txt", ios::app);

//get input
cout << "First Name: " << endl;
cin >> first_name;
if (first_name == done)
{
return 0;

}
cout << "Last Name: " << endl;
cin >> last_name;
cout << "Phone Number: " << endl;
cin >> number;

//append to file
outfile << " " << first_name << " " << last_name << " " << number << endl;
}
while (!outfile.eof());

outfile.close();
return 0;
}
User is offlineProfile CardPM
+Quote Post

corliss
RE: Help- Can't Get File To Exit What Am I Doing Wrong?
21 Apr, 2008 - 03:32 PM
Post #2

D.I.C Head
Group Icon

Joined: 25 Oct, 2006
Posts: 119



Thanked: 1 times
Dream Kudos: 50
My Contributions
The reason why it is not working is because you are not putting values into your variables.
You need.....
CODE

//declare variables
string first_name;
string last_name;
string number;
string done="done";
ofstream outfile;

This will set the string done to "done".
I would recomned "nulling" all your variables as well.

Error checking would also be good to have for the file that you are appending to.
Pusdocode~
CODE

string path ="C:\thisfile.txt"
if (!File.Exists(path))
make a new file
open the file
and write the data


All is well in the land of Ninja coders
p.s. surround your code with the square bracket and code code tags. It makes it nice and it's the rules in DIC land.

This post has been edited by corliss: 21 Apr, 2008 - 03:58 PM
User is offlineProfile CardPM
+Quote Post

clevs
RE: Help- Can't Get File To Exit What Am I Doing Wrong?
21 Apr, 2008 - 06:02 PM
Post #3

New D.I.C Head
*

Joined: 1 Apr, 2007
Posts: 13


My Contributions
Thanks changing the string done to string done = "done" did the trick. Thought I had tried that but this time it worked. Really appreciate help.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 06:57PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month