Hello everyone.

I am trying to write a simple program that shows me at a glance what services were given to my clients over the last year. I am trying to teach myself C++ as I go. The problem I am having is reading a text file into a class. My class is written in a header file.

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

class customers
{
      public: 
              customers(){};
              ~customers(){};
                                                                       
             string getf_name(){return f_name;}
             string getl_name(){return l_name;}
             int getJan(){return Jan;}
             int getFeb(){return Feb;}
             int getMar(){return Mar;}
             int getApr(){return Apr;}
             int getMay(){return May;}
             int getJun(){return Jun;}
             int getJul(){return Jul;}
             int getAug(){return Aug;}
             int getSep(){return Sep;}
             int getOct(){return Oct;}             
             int getNov(){return Nov;}
             int getDec(){return Dec;}
             string getnotes(){return notes;}
      protected:
                 string f_name;
                 string l_name;
                 int Jan;
                 int Feb;
                 int Mar;
                 int Apr;
                 int May;
                 int Jun;
                 int Jul;
                 int Aug;
                 int Sep;
                 int Oct;
                 int Nov;
                 int Dec;
                 string notes;
             };
                      
class Monthly : public customers
  {
      public:
             Monthly(string _f_name="",string _l_name="",int _Jan=0,int _Feb=0,int _Mar=0,int _Apr=0,int _May=0,int _Jul=0,int _Jun=0,int _Aug=0,int _Sep=0,int _Oct=0,int _Nov=0,int _Dec=0,string _notes)
             {f_name=_f_name;l_name=_l_name;Jan=_Jan;Feb=_Feb;Mar=_Mar;Apr=_Apr;May=_May;Jun=_Jun;Jul=_Jul;Aug=_Aug;Sep=_Sep,Oct=_Oct,Nov=_Nov;Dec=_Dec;notes=_notes;};
             ~Monthly(){};
             };

A short version of my text file looks like this:

My list is about 300 names... So I only posted a short list to give you an idea.

"Earl","Booth",2,2,23,2,4,2,2,23,2,2,4,12,""
"Carol","Hudson",2,2,234,2,2,234,2,2,234,2,2,234,"3 months behind as of 11/10"
"Bob","Ramis",2,2,2,2,2,2,2,2,2,2,2,2,"2/10 - Evidence of old termite tube located upon inspection. Bob insists it is a new tube. Destroyed top half of tube and marked the level. Will re-inspect each month to see if tube reforms. No other evidence found. House is under contract. All repairs made look solid."

Now, how can I read this text file, put it in the class so I can access it? I have tried to read the file in from the tutorials with little success, a word here, a sentence there... and when I tried to put it into the class... horrendous results. I can have my secretery reformat the text file any way needed. From what I've read, I think the class is so so.. ok...

Eventually, I want to be able to update the files monthly, and save a year to year file, but I'd be happy to be able to tell what we've done for the people at this point. Any help would be appreciated.

Thank you in advance

You should definitely use a std::vector<int> to store these things. You could also then define an enum to contain the name of the years to access the correct element of the vector

JANUARY = 0, etc

Good luck,

Dave

Wow, that code is pretty bad. I don't know why you aren't using arrays for the months.
Dont know why you are using inheritance when its obvious that composition is best.
I'm sorry, but you need to learn the basics a lot more, before you move onto classes,
inheritance and such.

Well That's part of learning I guess. I note you said it looked pretty bad, to bad you didn't offer some suggestions other than "Learn more"...

Learn more. :) In a more concrete sense what I believe firstPerson was indicating (but I don't want to speak for him) was that you should look into using an array for the months so, int months[12]; instead of all your separate variables and January would be months[0] . Look up enums in the long run to do what David was suggesting, but getting the arrays in there will go a long way towards consolidating that code. See if you can come up with a single "getter" function to get any month you specify rather than one for each month. As David mentioned look into vectors instead of the array for more flexibility etc.

The second point that FP was emphasizing is the "is-a" /"has-a" ideas of inheritance. Normally to have the kind of relationship you have designated (with Monthly inheriting from Customer) is an "is-a" relationship. If you had a class vehicle and derived motorcycle from it you could say a motorcycle is a vehicle. A better approach for you would be to have Monthy "has a"n array of customers (or even better a vector of them), which involves something like:

class Monthy
{
   public:
        //methods here
   private:
        Customer customerbase[25];
        //or   std::vector<Customer> customerbase;  //#include<vector> for this
}

which illustrates composition.

Hopefully that gives you something to google on. Post back as stuff comes up.

commented: Sometimes, I'm too frustrated to say much. Thanks +5

Well That's part of learning I guess. I note you said it looked pretty bad, to bad you didn't offer some suggestions other than "Learn more"...

By offer suggestion do you mean write some code so you can copy-paste from? If not then
I did offer a suggestion; which was to use arrays, and composition. But here is some
code to get a idea of what I mean :

#include <iostream>
#include <cassert>
#include <string>

using namespace std;
 
enum Number { ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE};

string convertNumberToString(const Number& n){
  assert(n> 0 && n < 10 );
  string ret;
  switch(n){
	  case ONE: ret = "ONE"; break;
	  case TWO: ret = "TWO"; break;
	  case THREE: ret = "THREE"; break;
	  //...
	  default: ret = "INVALID"; break;
  }
  return ret;
}

int main(){	
  using namespace std;
  Number n = THREE;
  cout << convertNumberToString( n ) << endl;
  return 0;
}

Wow..

Ok, let me first clearify... I wasn't trying to sound nasty, hateful, nor sarcastic towards you. If I did, I apologize. I appreciate all comments, and advice. Now let me explain my intent a little further. When pop had this company, he wrote the text files. Day in, and day out, when the tickets from the techs came back to the office, he would edit the text file. Now that dad has past, and the company dropped into my lap, I wanted to try to find a better way. So, I started looking into C++. I asked a few questions at the local college on what I should teach myself, and the professor told me as a rule of thumb...

For any program I should play the verb/noun game. Meaning, Figure out your project, then list all the nouns... customers, chemicals, milage, gas consumption, payroll....
Once I have the important list of nouns, those are made into "Classes".
Then for each class, make a list of sub nouns... or the lesser important nouns. Those are members. Then your verbs for each class are your methods, and some dribble about welcome to the world of OOP... or something...

Having this advice from a college professor, I feel it should be pretty good. Ok. So I'll go with that advice. The most important noun for my company is customer. That is where I started. A customer class. The months, types of service,notes are members, looking them up, adding data, and saving it will be my methods.

When teaching yourself anything, (especially C++ I have found) you accept the fact that you get a swiss cheese education. As it stands now, 2 months into it, I have more holes than cheese. That is why I asked how is it done. I didn't ask anyone to write a code, I'll figure that out to some degree. I just needed to know the procedure. What I needed to look into, to read up on. I figured by explaining I could get words, and a line here and there, you may be able to offer some insight, hoping you know why I'm not getting chunks of data, but either 1 word, or the whole line. I know there is something I'm doing wrong. That was all....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.