Solved

C++ class problems: saving to datafile

Posted on 2004-08-16
5
236 Views
Last Modified: 2010-04-01
hello experts,\


i have to save some data to a sequestial file but i can't, probably because of the mthod i have used to create the file

the following code is in my .h file and is my class;
#include <iostream>
#include <fstream>
#include <string>


using namespace std;

class person
{
      private:
            string name;
            int age;
            

      public:
            person();
            ~person();
            void getName();
            void getAge();
            void SaveData();
      
            

};

person::~person()
{
}

person::person()
{
      name ="";
      age = 0;
}

void person::getName()
{
      cout << "Enter Person Name:";
      string Name;
      cin >> Name;

}

void person::getAge()
{
      cout << "Enter Person Age:";
      string Age;
      cin >> Age;
}

I am calling the function in my .cpp file like so:

//my first class

#include<h:\cpp\myclasses\person.h>


void main()
{

      person me;
      me.getName();
      me.getAge();
      
      
}

how do i save the name and age to file, using the following type of code, where should this code go e,t,c.;
was callig it from my .cpp, but getting no data.

//save data
            
outCustomer << Name << '\t' << Age << endl;
outCustomer.close();


thanks
0
Comment
Question by:claracruz
5 Comments
 
LVL 19

Expert Comment

by:mrwad99
Comment Utility
In order to output to a file in C++ you set up an output file stream (ofstream)

ofstream myOutputFile("MyFile.txt");

You can then input to this stream like

myOutputFile >> someValue >> endl;

and when you are done you close the file

myOutputFile.close();

HTH
0
 
LVL 19

Expert Comment

by:mrwad99
Comment Utility
Ooops

Should be: you can then output to this stream like

myOutputFile << someValue << endl;

what I originally gave would read *from* the stream

You would put this code wherever you need to do output.  You could even put it in a function, passing the ofstream as an argument.
0
 
LVL 4

Author Comment

by:claracruz
Comment Utility
I already know all that.....

The question I am asking is based on my class,

please write a function that saves the data collected i.e. name and age to a data file.

1. how do i write the function?

2. where does the function to write to file go??

3. when does void main() come into play.

if you copy the above code and compile it, you could see how it works.... now i need a set of code that will save the data collected to a sequential file.

would be great if you could provide full code that works.

thanks.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
Comment Utility
You should consider whether you want one file for all customers or one file for each customer. The latter is easier but less efficient. Also you have a problem when there are customers having the same name.

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


using namespace std;

class person
{
     private:
          string name;
          int age;
         

     public:
          person();
          ~person();
          void getName();
          void getAge();
          void SaveData();
          void save();
          bool load(const string& NameGiven);
          bool getNameAndLoad();

};

person::~person()
{
}

person::person()
{
     name ="";
     age = 0;
}

void person::getName()
{
     cout << "Enter Person Name:";
     cin >> name;

}

void person::getAge()
{
     cout << "Enter Person Age:";
     cin >> age;
}



//save data
void person::save()
{
    string filename = name;
    filename += ".txt";
    ofstream outCustomer(filename.c_str());
    outCustomer << name << '\t' << age << endl;
    outCustomer.close();
}

bool person::load(const string& NameGiven)
{
     
    string filename = NameGiven;
    filename += ".txt";

    bool ok = true;
   
    ifstream inCustomer(filename.c_str());
    if (inCustomer.fail())
          return false;
    inCustomer >> name;
    if (inCustomer.fail())
          ok = false;
    inCustomer >> age;
    if (inCustomer.fail())
          ok = false;
   
    inCustomer.close();
    return ok;
}

// You may use these functions like that

bool person::getNameAndLoad()
{
     cout << "Enter Person Name:";
     string NameTyped;
     cin >> NameTyped;
     
     if (load(NameTyped))
     {
         cout << "Person " << name << " already stored. He/She has an age of " << age << endl;
         return true;   // person loaded
     }
     else
     {
          name = NameTyped;
          getAge();
          save();
          cout << "Person " << name << ", Age " << age << " , stored to file  " << name << ".txt" << endl;
          return false;  // new customer
     }

}

// And in main:

void main()
{
     char answer;
     person p;
     while (true)
     {
          cout << "Do You Want To Add Another Person [Y/N]? ";
          cin >> answer;
          if (answer == 'n' || answer == 'N')
              return;

          p.getNameAndLoad();

          // you may add a copy of the person p to a container like vector<person>.
          // however you need a copy constructor then
     }    
     
}



Regards, Alex
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
>>3. when does void main() come into play.


from http://www.cplusplus.com/doc/tutorial/tut1-1.html

The main function is the point by where all C++ programs begin their execution. It is independent of whether it is at the beginning, at the end or in the middle of the code - its content is always the first to be executed when a program starts. In addition, for that same reason, it is essential that all C++ programs have a main function.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now