Need help creating & storing files in C++

I am working on an address book for my computer science class. I have a programm that will take the data display the data and can also destory the data. The problem I am have is after I have enter data that I wish to keep. I then try to enter more data but keep over righting the data already enter. on new record function I have tried using cin.getline(); and plan old cin>>char; I get the same result either way my data keeps being overwrote.

Please Help, Russell.

#include "stdafx.h"
#include
#include
#include
#include
#include
using namespace std;
//********************************************************************
void selectCommand();
void Openfile();
void NewRecord();
void DeleteRecord();
//*********************************************************************
void main()
{
selectCommand();

system("pause");
}
//*********************************************************************
void selectCommand()
{
char ans;
cout<<endl<<"Would you like to start a (N)ew Record, (V)iew Records, (D)elete Records
or (E)xit"<<endl;
cin>>ans;

if(toupper(ans)=='V')
Openfile();

if(toupper(ans)=='N')
NewRecord();

if(toupper(ans)=='D')
DeleteRecord();

if(toupper(ans)=='E')
system("cls");
}
//*********************************************************************
void Openfile()
{
char fname[100];
char lname[100];
char address[100];
char city[100];
char state[100];
char zip[100];
char phone[100];
char answer;
system("cls");
ifstream infile;
infile.open("address.txt",ios::in);
do
{
infile>>fname;
cout<<fname<<endl;

infile>>lname;
cout<<lname<<endl;

infile>>address;
cout<<address<<endl;

infile>>city;
cout<<city<<endl;

infile>>state;
cout<<state<<endl;

infile>>zip;
cout<<zip<<endl;

infile>>phone;
cout<<phone<<endl;
cout<<"*****************
";

}while(!infile.eof());
infile.close();
cout<<"Do you wish to keep your address book (O)pen or (C)losed: ";
cin>>answer;

if(toupper(answer)=='O')
selectCommand();

if(toupper(answer)=='C');
system("cls");
}
//*********************************************************************
void NewRecord()
{
char fname[100];
char lname[100];
char address[100];
char city[100];
char state[100];
char zip[100];
char phone[100];
char answer;

ofstream outfile;
cout<<"****************************
";
cout<<"Entering file writing function"<<endl;
outfile.open("address.txt",ios::out);

cout<<endl<<"Enter First Name:
";
cin>>fname;
cin.ignore(100, '
');

cout<<endl<<"Enter Last Name:
";
cin>>lname;
cin.ignore(100, '
');

cout<<endl<<"Enter Street Address:
";
cin>>address;
cin.ignore(100, '
');

cout<<endl<<"Enter City:
";
cin>>city;
cin.ignore(100, '
');

cout<<endl<<"Enter State:
";
cin>>state;
cin.ignore(100, '
');

cout<<endl<<"Enter Zip Code:
";
cin>>zip;
cin.ignore(100, '
');

cout<<endl<<"Enter Phone Number:
";
cin>>phone;
cin.ignore(100, '
');

outfile<<fname<<endl;
outfile<<lname<<endl;
outfile<<address<<endl;
outfile<<city<<endl;
outfile<<state<<endl;
outfile<<zip<<endl;
outfile<<phone;
outfile.close();

cout<<"Do you wish to keep your address book (O)pen or (C)losed: ";
cin>>answer;

if(toupper(answer)=='O')
selectCommand();

if(toupper(answer)=='C');
system("cls");

}
//****************************************************************************
void DeleteRecord()
{
char answer;
cout<<"*****************************
";
cout<<"Entering destory file function"<<endl;
cout<<"Contents of file destroyed"<<endl;
ofstream outfile;
outfile.open("address.txt",ios::out);
outfile.close();
cout<<"Do you wish to keep your address book (O)pen or (C)losed: ";
cin>>answer;

if(toupper(answer)=='O');
selectCommand();

if(toupper(answer)=='C');
system("cls");

}

Comments

  • : I am working on an address book for my computer science class. I have a programm that will take the data display the data and can also destory the data. The problem I am have is after I have enter data that I wish to keep. I then try to enter more data but keep over righting the data already enter. on new record function I have tried using cin.getline(); and plan old cin>>char; I get the same result either way my data keeps being overwrote.

    outfile.open("address.txt",ios::out);

    That will create address.txt each time it is invoked, and thus overwrite the old file. Instead of ios::out, try ios::app.

    I'm surprised it works at all to be honest since you're not resetting the file pointer, or clearing the eof bit after each read of the file.

    You can save some code with your file reading by using a string class member... the char array *should* work as well.

    [code]
    readfile()
    {
    string line;

    while(getline(ifstream_obj, line))
    cout << line << endl;
    }[/code]That would really depend on how the output file is written though... I didn't really check it.

    :
    : Please Help, Russell.
    :
    : #include "stdafx.h"
    : #include <iostream>
    : #include
    : #include
    : #include
    : #include
    : using namespace std;
    : //********************************************************************
    : void selectCommand();
    : void Openfile();
    : void NewRecord();
    : void DeleteRecord();
    : //*********************************************************************
    : void main()
    : {
    : selectCommand();
    :
    : system("pause");
    : }
    : //*********************************************************************
    : void selectCommand()
    : {
    : char ans;
    : cout<<endl<<"Would you like to start a (N)ew Record, (V)iew Records, (D)elete Records
    or (E)xit"<<endl;
    : cin>>ans;
    :
    : if(toupper(ans)=='V')
    : Openfile();
    :
    : if(toupper(ans)=='N')
    : NewRecord();
    :
    : if(toupper(ans)=='D')
    : DeleteRecord();
    :
    : if(toupper(ans)=='E')
    : system("cls");
    : }
    : //*********************************************************************
    : void Openfile()
    : {
    : char fname[100];
    : char lname[100];
    : char address[100];
    : char city[100];
    : char state[100];
    : char zip[100];
    : char phone[100];
    : char answer;
    : system("cls");
    : ifstream infile;
    : infile.open("address.txt",ios::in);
    : do
    : {
    : infile>>fname;
    : cout<<fname<<endl;
    :
    : infile>>lname;
    : cout<<lname<<endl;
    :
    : infile>>address;
    : cout<<address<<endl;
    :
    : infile>>city;
    : cout<<city<<endl;
    :
    : infile>>state;
    : cout<<state<<endl;
    :
    : infile>>zip;
    : cout<<zip<<endl;
    :
    : infile>>phone;
    : cout<<phone<<endl;
    : cout<<"*****************
    ";
    :
    : }while(!infile.eof());
    : infile.close();
    : cout<<"Do you wish to keep your address book (O)pen or (C)losed: ";
    : cin>>answer;
    :
    : if(toupper(answer)=='O')
    : selectCommand();
    :
    : if(toupper(answer)=='C');
    : system("cls");
    : }
    : //*********************************************************************
    : void NewRecord()
    : {
    : char fname[100];
    : char lname[100];
    : char address[100];
    : char city[100];
    : char state[100];
    : char zip[100];
    : char phone[100];
    : char answer;
    :
    : ofstream outfile;
    : cout<<"****************************
    ";
    : cout<<"Entering file writing function"<<endl;
    : outfile.open("address.txt",ios::out);
    :
    : cout<<endl<<"Enter First Name:
    ";
    : cin>>fname;
    : cin.ignore(100, '
    ');
    :
    : cout<<endl<<"Enter Last Name:
    ";
    : cin>>lname;
    : cin.ignore(100, '
    ');
    :
    : cout<<endl<<"Enter Street Address:
    ";
    : cin>>address;
    : cin.ignore(100, '
    ');
    :
    : cout<<endl<<"Enter City:
    ";
    : cin>>city;
    : cin.ignore(100, '
    ');
    :
    : cout<<endl<<"Enter State:
    ";
    : cin>>state;
    : cin.ignore(100, '
    ');
    :
    : cout<<endl<<"Enter Zip Code:
    ";
    : cin>>zip;
    : cin.ignore(100, '
    ');
    :
    : cout<<endl<<"Enter Phone Number:
    ";
    : cin>>phone;
    : cin.ignore(100, '
    ');
    :
    : outfile<<fname<<endl;
    : outfile<<lname<<endl;
    : outfile<<address<<endl;
    : outfile<<city<<endl;
    : outfile<<state<<endl;
    : outfile<<zip<<endl;
    : outfile<<phone;
    : outfile.close();
    :
    : cout<<"Do you wish to keep your address book (O)pen or (C)losed: ";
    : cin>>answer;
    :
    : if(toupper(answer)=='O')
    : selectCommand();
    :
    : if(toupper(answer)=='C');
    : system("cls");
    :
    : }
    : //****************************************************************************
    : void DeleteRecord()
    : {
    : char answer;
    : cout<<"*****************************
    ";
    : cout<<"Entering destory file function"<<endl;
    : cout<<"Contents of file destroyed"<<endl;
    : ofstream outfile;
    : outfile.open("address.txt",ios::out);
    : outfile.close();
    : cout<<"Do you wish to keep your address book (O)pen or (C)losed: ";
    : cin>>answer;
    :
    : if(toupper(answer)=='O');
    : selectCommand();
    :
    : if(toupper(answer)=='C');
    : system("cls");
    :
    : }
    :
    :
  • : : I am working on an address book for my computer science class. I have a programm that will take the data display the data and can also destory the data. The problem I am have is after I have enter data that I wish to keep. I then try to enter more data but keep over righting the data already enter. on new record function I have tried using cin.getline(); and plan old cin>>char; I get the same result either way my data keeps being overwrote.
    :
    : outfile.open("address.txt",ios::out);
    :
    : That will create address.txt each time it is invoked, and thus overwrite the old file. Instead of ios::out, try ios::app.
    :
    : I'm surprised it works at all to be honest since you're not resetting the file pointer, or clearing the eof bit after each read of the file.
    :
    : You can save some code with your file reading by using a string class member... the char array *should* work as well.
    :
    : [code]
    : readfile()
    : {
    : string line;
    :
    : while(getline(ifstream_obj, line))
    : cout << line << endl;
    : }[/code]That would really depend on how the output file is written though... I didn't really check it.
    :
    : :
    Thank you, changing the IOS to append, instead of out, fixed the problem that I was having. I don't understand what the file pointer is or how to reset the eof. These are things my instructor has not covered, if you fell up to explaining more about this I would love it, if not I understand. As for the code you sent, if I understand correctly this code will replace nearly all the code in my open file function.
    Thank You Again, Russell

    : : Please Help, Russell.
    : :
    : : #include "stdafx.h"
    : : #include <iostream>
    : : #include
    : : #include
    : : #include
    : : #include
    : : using namespace std;
    : : //********************************************************************
    : : void selectCommand();
    : : void Openfile();
    : : void NewRecord();
    : : void DeleteRecord();
    : : //*********************************************************************
    : : void main()
    : : {
    : : selectCommand();
    : :
    : : system("pause");
    : : }
    : : //*********************************************************************
    : : void selectCommand()
    : : {
    : : char ans;
    : : cout<<endl<<"Would you like to start a (N)ew Record, (V)iew Records, (D)elete Records
    or (E)xit"<<endl;
    : : cin>>ans;
    : :
    : : if(toupper(ans)=='V')
    : : Openfile();
    : :
    : : if(toupper(ans)=='N')
    : : NewRecord();
    : :
    : : if(toupper(ans)=='D')
    : : DeleteRecord();
    : :
    : : if(toupper(ans)=='E')
    : : system("cls");
    : : }
    : : //*********************************************************************
    : : void Openfile()
    : : {
    : : char fname[100];
    : : char lname[100];
    : : char address[100];
    : : char city[100];
    : : char state[100];
    : : char zip[100];
    : : char phone[100];
    : : char answer;
    : : system("cls");
    : : ifstream infile;
    : : infile.open("address.txt",ios::in);
    : : do
    : : {
    : : infile>>fname;
    : : cout<<fname<<endl;
    : :
    : : infile>>lname;
    : : cout<<lname<<endl;
    : :
    : : infile>>address;
    : : cout<<address<<endl;
    : :
    : : infile>>city;
    : : cout<<city<<endl;
    : :
    : : infile>>state;
    : : cout<<state<<endl;
    : :
    : : infile>>zip;
    : : cout<<zip<<endl;
    : :
    : : infile>>phone;
    : : cout<<phone<<endl;
    : : cout<<"*****************
    ";
    : :
    : : }while(!infile.eof());
    : : infile.close();
    : : cout<<"Do you wish to keep your address book (O)pen or (C)losed: ";
    : : cin>>answer;
    : :
    : : if(toupper(answer)=='O')
    : : selectCommand();
    : :
    : : if(toupper(answer)=='C');
    : : system("cls");
    : : }
    : : //*********************************************************************
    : : void NewRecord()
    : : {
    : : char fname[100];
    : : char lname[100];
    : : char address[100];
    : : char city[100];
    : : char state[100];
    : : char zip[100];
    : : char phone[100];
    : : char answer;
    : :
    : : ofstream outfile;
    : : cout<<"****************************
    ";
    : : cout<<"Entering file writing function"<<endl;
    : : outfile.open("address.txt",ios::out);
    : :
    : : cout<<endl<<"Enter First Name:
    ";
    : : cin>>fname;
    : : cin.ignore(100, '
    ');
    : :
    : : cout<<endl<<"Enter Last Name:
    ";
    : : cin>>lname;
    : : cin.ignore(100, '
    ');
    : :
    : : cout<<endl<<"Enter Street Address:
    ";
    : : cin>>address;
    : : cin.ignore(100, '
    ');
    : :
    : : cout<<endl<<"Enter City:
    ";
    : : cin>>city;
    : : cin.ignore(100, '
    ');
    : :
    : : cout<<endl<<"Enter State:
    ";
    : : cin>>state;
    : : cin.ignore(100, '
    ');
    : :
    : : cout<<endl<<"Enter Zip Code:
    ";
    : : cin>>zip;
    : : cin.ignore(100, '
    ');
    : :
    : : cout<<endl<<"Enter Phone Number:
    ";
    : : cin>>phone;
    : : cin.ignore(100, '
    ');
    : :
    : : outfile<<fname<<endl;
    : : outfile<<lname<<endl;
    : : outfile<<address<<endl;
    : : outfile<<city<<endl;
    : : outfile<<state<<endl;
    : : outfile<<zip<<endl;
    : : outfile<<phone;
    : : outfile.close();
    : :
    : : cout<<"Do you wish to keep your address book (O)pen or (C)losed: ";
    : : cin>>answer;
    : :
    : : if(toupper(answer)=='O')
    : : selectCommand();
    : :
    : : if(toupper(answer)=='C');
    : : system("cls");
    : :
    : : }
    : : //****************************************************************************
    : : void DeleteRecord()
    : : {
    : : char answer;
    : : cout<<"*****************************
    ";
    : : cout<<"Entering destory file function"<<endl;
    : : cout<<"Contents of file destroyed"<<endl;
    : : ofstream outfile;
    : : outfile.open("address.txt",ios::out);
    : : outfile.close();
    : : cout<<"Do you wish to keep your address book (O)pen or (C)losed: ";
    : : cin>>answer;
    : :
    : : if(toupper(answer)=='O');
    : : selectCommand();
    : :
    : : if(toupper(answer)=='C');
    : : system("cls");
    : :
    : : }
    : :
    : :
    :



  • : Thank you, changing the IOS to append, instead of out, fixed the problem that I was having. I don't understand what the file pointer is or how to reset the eof. These are things my instructor has not covered, if you fell up to explaining more about this I would love it, if not I understand.

    [blue]To get an idea of what's going on, create "test.txt" and put a few lines in it, run the snippit without removing the comments, and then again after removing the comments.[code]#include
    #include
    #include

    using namespace std;

    int main()
    {
    string buf;

    fstream inFile("test.txt");

    if(inFile.fail())
    {
    cerr << "Unable to access input file.";
    return (1);
    }

    for(int i=0; i < 5; i++)
    {
    cout << endl << i << endl << endl;

    while(getline(inFile, buf))
    cout << buf << endl;

    // inFile.clear();
    // inFile.seekg(0L, ios::beg);
    }

    inFile.close();

    return (0);
    }[/code]You *should* begin to understand what's going on. As each read occurs, the eof bit is set (once all data has been read), and the file marker (cursor, indicator, pointer, whatever) points to eof. Before you can read the file again, the eof bit must be reset - clear(), and the file marker needs to be positioned backed to the beginning of the file - seekg(0L, ios::beg)

    In your program, I guess the streams are working themselves out. I really don't know - perhaps the combination of creating and reading.[/blue]

    As for the code you sent, if I understand correctly this code will replace nearly all the code in my open file function.
    : Thank You Again, Russell

    [blue]As it's currently written, I don't see any reason that it wouldn't work. IIRC, you weren't using the locals for anything but each line in the file. If you had to use all fields in the function for some reason, then it wouldn't work as the previous line would be overwritten with the next.

    HTH[blue]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories