Reading a file and storing into class variables

Hi all
I have a problem in which I have to read txt file and store the values into the class variables.
The txt file I have is
1
2
3
4
5
6
02/08/2011,ABC,buy,100,20.00
05/08/2011,ABC,buy,20,24.00
06/08/2011,ABC,buy,200,36.00
15/08/2011,ABC,sell,150,30.00
19/10/2011,ABC,sell,40,35.50
03/11/2011,ABC,buy,100,27.25


The header class for storing above info is given below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef H_TRANSACTION
#define H_TRANSACTION
#include"date.h"
#include<string>

using namespace std;

class transaction
{
public:
	//Function definition for setting transaction
	void set_tx(int,int,int,string,string,int,double);

	//Function definition for x_code
	string get_code()const;
	void show_code()const;

	//Function definition for activity
	string get_activity()const;
	void show_activity()const;

	//Function definition for shares
	int get_shares()const;
	void show_shares()const;

	//Function definition for price
	double get_price()const;
	void show_price()const;

	//Function to print the transaction details
	void print_tx()const;

	//Constructor with parameters for transaction class
	transaction(int =1,int =1,int =2000,string ="",string ="",int =0,double =0.0);
private:
	Date date1;
	string tx_code;
	string tx_activity;
	int tx_shares;
	double tx_price;
};

#endif 



Now I want to read the file and send the values to the corresponding valuables of the 'transaction' class.

This what I have tried so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include"transaction.h"

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

int main()
{
	//open file
	ifstream in;
	in.open("share-data.txt");
	//To check input file presence
	if(!in)
	{
		cout<<"File not found.Program terminates"<<endl;
		system("pause");
		return 1;
	}
	int day,month,year,shares;
	string x_code,activity;
	double price;
	int size=0;
	transaction tx;
	while(!in.eof())
	{
		getline(in,tx,',');
		tx.set_tx(day,month,year,x_code,activity,shares,price);
		tx.print_tx();
		size++;
	}
	cout<<size<<endl;
	//transaction *tx;


	
	system("pause");
	return 0;
}


The date should be seperated into three int's and passed inside. Can anyone please guide me
Note
There is date class used inside the transaction header file through composition
It is usually a bad idea to have public get / set functions for every private variable - you may as well have all the variables public.

So, instead you can have constructors that set the variables. Also you can set several variables with one function. If you want to print them all, then do this with one function as well.

You could have a class function that loads the data from the file, that way the function will have access to the private member variables.

If you need to do calculations with variables, then put them into a class function.

HTH

Edit: If you do these things, then all of the work is done by the class, which is part of the idea of encapsulation into a class. Your main should do very little apart from creating the objects and calling their functions.
Last edited on
So first I should make my member variables as protected
then include functions ,to read and store file input, inside the class itself
then also include calculation functions inside the class itslef

Am I getting it right?
Yes, although they only need to be protected if you are going to inherit from that class.

Also, it is conventional to prepend member variables name with m_

1
2
3
4
string m_Code;
string m_Activity;
unsigned m_Shares;
double m_Price;



I name my Classes with an initial C as in CDate.

Your constructor looks odd -it has no variables in it, although you are trying to set them. int =2000 doesn't make sense to me. IMO that should not have compiled. You can have types & variable names as a parameter list, set the value of the member variables from the value of the arguments inside the implementation of the function.

Presumably you have a transaction.cpp with the implementation of the functions.

HTH
Ok I will change my variables and class names as you suggested and actually it make sense.

Im trying to combine default constructor and normal constructor under one constructor. The book I studied says, I can initialize the variables without specifying the variable name, in the declaration of default constructor.

Yeah I have transaction.cpp with the implementation.

Topic archived. No new replies allowed.