Advice on how to structure

Hi,

So, I'm trying to create a program that:
1) Reads in numbers (separated by a comma) from a text file.
2) Put these into a class that has member variables for each of these numbers.
3) Push this back into a vector.

I've created classes (and associated functions) for both the class to hold the variables and the vector to hold each instance of these objects.

I just can't work out how to plug them all together.

1) Is it necessary to have the vector as an object on a stack or should I simply declare a vector object as type 'vector'

2) Do I read in from the file, use mutator functions to set the object variable and then push this object on the the stack?
If so, I'm struggling to get the syntax for this.
(The problem exists at the bottom part of the readfile() function.)
Can anyone correct my attempts?

Any advice will be appreciated.

Thanks!

main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// minitry.cpp : Trying to load numbers into an object and then a vector.

#include <iostream>
#include "vecpart.h"
#include "cbit.h"

using namespace std;

int main()
{
    cout << "Putting numbers into a vector!" << endl;
    
    VecBit *numList = new VecBit;
    readfile(*numList);

    cout << "Program finished." << endl;

}


cbit.h:
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
#ifndef CBIT_H_INCLUDED
#define CBIT_H_INCLUDED

using namespace std;

class Cbit
{
public:
	// Constructor
	Cbit();

	// Destructor
	~Cbit();

	// Accessor functions
	int getOne();
	// Access the contents of member variable 'one'
	int getTwo();
	// Access the contents of member variable 'two'

	// Mutator functions
	void setOne(int);
	// Modify the contents of member variable 'one'
	void setTwo(int);
	// Modify the contents of member variable 'two'

private:
	int one; 
	// Member variable
	int two;
	// Member variable
};


#endif // !CBIT_H_INCLUDED 


cbit.cpp:
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
#include "Cbit.h"

Cbit::Cbit()
{
	one = 0;	
	two = 0;
}


// Destructor
Cbit::~Cbit()
{
	// Nothing to see here!
}

// Accessor functions
int Cbit::getOne()
{
	return one;
}

int Cbit::getTwo()
{
	return two;
}

// Mutator functions
void Cbit::setOne(int nOne)
{
	one = nOne;
}

void Cbit::setTwo(int nTwo)
{
	two = nTwo;
}


vecpart.h:
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
#ifndef VECPART_H_INCLUDED
#define VECPART_H_INCLUDED

#include <vector>
#include "cbit.h"
#include "vecpart.h"
#include <fstream>
#include <string>
#include <sstream>


using namespace std;

class VecBit
{
public:
	// Standard contructor
	VecBit();

	// Standard destructor
	~VecBit()

private:
	vector<Cbit> numList;
};


#endif // VECPART_H_INCLUDED

void readfile(vector<VecBit>);


vecpart.cpp:
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <vector>
#include "vecpart.h"
#include "cbit.h"


using namespace std;


// Standard contructor
VecBit::VecBit()
{
	// Nothing to see here!
}

// Standard destructor
VecBit::~VecBit()
{
	// Nothing to see here!
}

void readfile(vector<VecBit> *numList )
{
	int one1;
	int two2;

	ifstream inFile;
	inFile.open("minitry.txt");
	// Create a filestream and open it.

	// Check for error & deliver message if it does.
	if (inFile.fail())
	{
		cerr << "Error opening file." << endl;
		exit(1);
	}

	string line;		// line read in from file.
	string myString;	// scratchpad space
	Cbit *bunch = new Cbit;  // Object to hold data

	while (getline(inFile, line))												
	{
		// While no end of file.
		stringstream ss(line);	
		// Read in the contents 
		getline(ss, myString, ',');		
		// Read up until the next delimiter as a string
		one1 = stoi(myString);
		// Convert from string to integer
		*bunch.setOne(one1);
		// Set the value of 'one' as what we've just read in

		getline(ss, myString, ',');
		// Read up until the next delimiter as a string
		two2 = stoi(myString);
		// Convert from string to integer
		*bunch.setTwo(two2);
		// Set the value of 'two' as what we've just read in

		numList.push_back(bunch);
		// push this onto the vector
	}

	inFile.close();
}
Last edited on
> 1) Reads in numbers (separated by a comma) from a text file.
> 2) Put these into a class that has member variables for each of these numbers.
How many numbers?

From your reader, it seems you have pairs of numbers per line, is that right?

FWIW, you shouldn't need any * variables or calls to new to do this assignment.
At this point, it's only a small list but I wish to make my code expandable to handle a list of indeterminate length (i.e. possibly thousands.)

Yes, here is my sample file:

1
2
3
4
5
6
7
8
9
10
11
12
13
23,45
24,1
1,32
4,3
9,37
21,15
41,30
33,26
8,18
6,12
9,34
34,27
31,39


Which part do you mean does not have to be passed by pointer?

Thanks.
In summary.

1. Make readfile() a member function of your class. As it stands, it can't access the private member.

main.cpp
1
2
    VecBit myVec;
    myVec.readfile();



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class VecBit
{
public:
	// Standard contructor
	VecBit();

	// Standard destructor
	~VecBit()

	// consider passing the filename as a parameter
	void readfile();

	

private:
	vector<Cbit> numList;
};



> Cbit *bunch = new Cbit; // Object to hold data
Just Cbit bunch will suffice.

Your vector push_back will make a copy if it, so you're free to just keep reusing the same variable in your function.
Thank you very much!

That appears to have worked!
Last edited on
Topic archived. No new replies allowed.