transform program to program using class

Hi, i have my program to load info about students from .txt file

1
2
3
4
5
Mrkvicka Jozef 2 1.75 2.1 0.4 1 0.49 1.27 
Hrasko Jan 0.1 0.38 1 1.2
Mladek Peter 1.6 0.4 2 1.3 1.8
Petrzlen Robert 0.6 0 1.1 1.1 0.6 0.59 
Horvath Ivan 1.6 0.8 0.7 1.3 1


My program is ruuning perfectly only problem is that i must transform it to program uses class, class student . Am very beginer in using classes, so far i do very easy programs using classes.

so far i have this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "stdafx.h"
#include "student.h"

student::student(vector<STUDENT> studenti)
{
	vStudent=studenti;
}

student::~student(void)
{
}

STUDENT student::getStudent()
{
	return vStudent;
}

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
#include <vector>
#include "stdafx.h"
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>

struct STUDENT {
	string meno;
	string priezvisko;
	vector<double> body;
	double sucet;
	double znamka;
};

class student
{
public:
	student(vector <STUDENT> studenti);
	STUDENT getStudent();
	~student(void);
	void vypisStudent();
private:
    vector<STUDENT> vStudent;
};


i try to make it but its too difficult for me. in that student.h and student .cpp is 50 errors..:(

So if anybody could tranform this code to code using class student.h i would rly appreciate it

Here is my code
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "stdafx.h"
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;


struct STUDENT {
	string meno;
	string priezvisko;
	vector<double> body;
	double sucet;
	double znamka;
};

int _tmain(int argc, _TCHAR* argv[])
{
	vector<double> body;
	ifstream ifsSubor;
	string sRiadok;
	vector<STUDENT> studenti;
	double bod;
	STUDENT s;
	stringstream ss;
	double SUCET;
	int ZNAMKA;
	string hladaj;
	bool a=false;
	ifsSubor.open("vstup.txt");

if (ifsSubor.is_open()) {
while(getline(ifsSubor, sRiadok))
	{
		SUCET=0;
		ZNAMKA=0;
		ss.clear();
		s.body.clear();
		ss<<sRiadok;
		ss >> s.priezvisko;
		ss >> s.meno;
	
		while (ss >> bod) 
		{
			SUCET=SUCET+bod;
			s.body.push_back(bod);

		}
		s.sucet=SUCET;
		 
			if (SUCET>=10)                  {ZNAMKA=1;} ;
			if ((SUCET<10) &&  (SUCET>=8))  {ZNAMKA=2;} ;
			if ((SUCET<8)  &&  (SUCET>=5))  {ZNAMKA=3;} ;
			if ((SUCET<5)  &&  (SUCET>=2))  {ZNAMKA=4;} ;
			if (SUCET<2)                    {ZNAMKA=5;} ;

		s.znamka=ZNAMKA;

	studenti.push_back(s);
	}

    int i=0;
	vector<STUDENT>::iterator it;

for(it = studenti.begin(); it != studenti.end(); ++it)
	{
		cout.width(10);
		cout << left<<(*it).priezvisko <<"   "<< (*it).meno<<" \t"; 

			for (int i=0; i<it->body.size(); i++)
				cout<<it->body[i] <<"\t";
				cout<<endl;
	}
	cout<<endl;

for(it = studenti.begin(); it != studenti.end(); ++it)
	{
		cout.width(10);
		cout <<left << (*it).priezvisko <<"   " << (*it).meno<<" \t" << (*it).sucet<<" \t" << (*it).znamka<<" \t" ; 
		cout<<endl;
	}

	cout<<endl;
	cout<<"Zadaj priezvisko hladaneho studenta"<<endl;
	cin>>hladaj;
	cout<<endl;

for(it = studenti.begin(); it != studenti.end(); ++it) 
	{
		cout.width(10);
		if ((*it).priezvisko==hladaj) {cout<<(*it).priezvisko <<"   " << (*it).meno<<"\t" << (*it).sucet<<"\t" << (*it).znamka<<" \t"<<endl;
		a=true;}
	}

		if (a==false) {cout<<"hladany student sa v zozname nenachadza"<<endl;}
}

else {cout<<"vstupny subor sa nepodarilo otvorit"<<endl;}

return 0;

}

Last edited on
JOLO wrote:
i try to make it but its too difficult for me. in that student.h and student .cpp is 50 errors..:(

50 errors might look overwhelming but note that one error often causes a bunch of extra errors in the code that follows so it's usually not as bad as it looks. Start at the top and fix the first error in the list, and then see what errors you have left.
but is that student.h and student.cpp ok? and declaration of that structre in class?
and should i use that structure or just declare that parameters in public, and create vector in main

class student
{
public:
string meno;
string priezvisko;
vector<double> body;
double sucet;
double znamka;
getname();
.
.
.
~student(void);
void vypisStudent();
private:
Last edited on
and if i just try to start program with student.h and .cpp , w/o any code in main there is 20 errors. just try to strat it like me only with student.h and .cpp and uwill see
In student.h I get a lot of errors because you have forgot to specify that you mean string and vector in the std namespace. Adding std:: in front of all occurrences of string and vector in that file fixes the problem.

In student.cpp you have again forgot to put std:: in front of vector on line 4. You have also specified that getStudent() should return a STUDENT object but you are trying to return the whole vStudent vector so that generates another error.
ok, i fix all errors. sorry for that i forgot add std::. next time i try to better look on code before i post that something dont work

i got this now
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
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;

struct STUDENT {

	string meno;
	string priezvisko;
	vector<double> body;
	double sucet;
	double znamka;
};

class student
{
public:
	student(vector <STUDENT> studenti);
	STUDENT getStudent();
	~student(void);
	void vypisStudent();
private:
    vector<STUDENT> vStudent;
	STUDENT s;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include "student.h"
using namespace std;

student::student(vector<STUDENT> studenti)
{
	vStudent=studenti;
}

student::~student(void)
{
}

STUDENT student::getStudent()
{
	return s;
}


so with that class, and declaration will it work like my old code or i must add something?
oh and where should i put this , to main or should i add some functionto class?
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
vector<double> body;
	ifstream ifsSubor;
	string sRiadok;
	vector<STUDENT> studenti;
	double bod;
	STUDENT s;
	stringstream ss;
	double SUCET;
	int ZNAMKA;
	string hladaj;
	bool a=false;
	ifsSubor.open("vstup.txt");

if (ifsSubor.is_open()) {
while(getline(ifsSubor, sRiadok))
	{
		SUCET=0;
		ZNAMKA=0;
		ss.clear();
		s.body.clear();
		ss<<sRiadok;
		ss >> s.priezvisko;
		ss >> s.meno;
	
		while (ss >> bod) 
		{
			SUCET=SUCET+bod;
			s.body.push_back(bod);

		}
		s.sucet=SUCET;
		 
			if (SUCET>=10)                  {ZNAMKA=1;} ;
			if ((SUCET<10) &&  (SUCET>=8))  {ZNAMKA=2;} ;
			if ((SUCET<8)  &&  (SUCET>=5))  {ZNAMKA=3;} ;
			if ((SUCET<5)  &&  (SUCET>=2))  {ZNAMKA=4;} ;
			if (SUCET<2)                    {ZNAMKA=5;} ;

		s.znamka=ZNAMKA;

	studenti.push_back(s);
	}
Last edited on
so i try, but more i try to make it , more errors is in code (atm 57), i dont know where i do mistakes pls help my main:

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
 #include "stdafx.h"
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <fstream>
#include "student.h"
using namespace std;

struct STUDENT {

	string meno;
	string priezvisko;
	vector<double> body;
	double sucet;
	double znamka;
};


int _tmain(int argc, _TCHAR* argv[])
{  
	student objStudent;
	STUDENT s;
	vector<double> body;
	ifstream ifsSubor;
	string sRiadok;
	double bod;
	stringstream ss;
	double SUCET;
	int ZNAMKA;
	string hladaj;
	bool a=false;
	ifsSubor.open("vstup.txt");
	int i=0;
	string hladaj;
	bool a;
if (ifsSubor.is_open()) {

while(getline(ifsSubor, sRiadok))
	{
		SUCET=0;
		ZNAMKA=0;
		ss.clear();
		s.body.clear();
		ss<<sRiadok;
		ss >> s.priezvisko;
		ss >> s.meno;
	
		while (ss >> bod) 
		{
			SUCET=SUCET+bod;
			s.body.push_back(bod);

		}
		s.sucet=SUCET;
		 
			if (SUCET>=10)                  {ZNAMKA=1;} ;
			if ((SUCET<10) &&  (SUCET>=8))  {ZNAMKA=2;} ;
			if ((SUCET<8)  &&  (SUCET>=5))  {ZNAMKA=3;} ;
			if ((SUCET<5)  &&  (SUCET>=2))  {ZNAMKA=4;} ;
			if (SUCET<2)                    {ZNAMKA=5;} ;

		s.znamka=ZNAMKA;

		objStudent->pridajStudenta(s);
	}

	vector<STUDENT> container1 = objStudent.getStudent();
	vector<student>::iterator it;

for(it = container1.begin(); it != container1.end(); ++it)
	{
		cout.width(10);
		cout << left<<(*it).priezvisko <<"   "<< (*it).meno<<" \t"; 

			for (int i=0; i<it->body.size(); i++)
				cout<<it->body[i] <<"\t";
				cout<<endl;
	}
	cout<<endl;

for(it = container1.begin(); it != container1.end(); ++it)
	{
		cout.width(10);
		cout <<left << (*it).priezvisko <<"   " << (*it).meno<<" \t" << (*it).sucet<<" \t" << (*it).znamka<<" \t" ; 
		cout<<endl;
	}

	cout<<endl;
	cout<<"Zadaj priezvisko hladaneho studenta"<<endl;
	cin>>hladaj;
	cout<<endl;

for(it = container1.begin(); it != container1.end(); ++it) 
	{
		cout.width(10);
		if ((*it).priezvisko==hladaj) {cout<<(*it).priezvisko <<"   " << (*it).meno<<"\t" << (*it).sucet<<"\t" << (*it).znamka<<" \t"<<endl;
		a=true;}
	}

		if (a==false) {cout<<"hladany student sa v zozname nenachadza"<<endl;}

}
else {cout<<"vstupny subor sa nepodarilo otvorit"<<endl;}


return 0;

}



my student.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
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;

struct STUDENT {

	string meno;
	string priezvisko;
	vector<double> body;
	double sucet;
	double znamka;
};


class student
{
public:
	student();
	~student(void);
	vector<STUDENT> getStudent();
	void pridajStudenta(STUDENT Student);
private:
	vector<STUDENT> studenti;
};


and my student.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
#include "student.h"
using namespace std;

student::student()
{

}

student::~student(void)
{
}

void student::pridajStudenta(STUDENT Student)
{
	studenti.push_back(Student);
}


STUDENT student::getStudent()
{
	return studenti;
}

	
student.h line 22: getStudent returns vector<STUDENT>
student.cpp line 19: returns STUDENT, not vector<STUDENT>

main.cpp line 10, student.h line 7: struct STUDENT is defined multiple times.

main.cpp lines 31, 35: hladaj defined multiple times.
main.cpp lines 32, 36: a defined multiple times.

main.cpp line 65: objStudent is not a pointer.
main.cpp line 69: vector<student> is inconsistent. Should be vector<STUDENT>.





well i fix your mistake, and there is still 45 errors :(
Post your current code.
OK, so i figure it out but am not sure if its ok. Would u change something or some advices to imrove my code, is it good using of class?
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "stdafx.h"
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <fstream>
#include "student.h"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{  
	student objStudent;
	STUDENT s;
	vector<double> body;
	ifstream ifsSubor;
	string sRiadok;
	double bod;
	stringstream ss;
	double SUCET;
	int ZNAMKA;
	string hladaj;
	bool a=false;
	ifsSubor.open("vstup.txt");
	int i=0;
if (ifsSubor.is_open()) {

while(getline(ifsSubor, sRiadok))
	{
		SUCET=0;
		ZNAMKA=0;
		ss.clear();
		s.body.clear();
		ss<<sRiadok;
		ss >> s.priezvisko;
		ss >> s.meno;
	
		while (ss >> bod) 
		{
			SUCET=SUCET+bod;
			s.body.push_back(bod);

		}
		s.sucet=SUCET;
		 
			if (SUCET>=10)                  {ZNAMKA=1;} ;
			if ((SUCET<10) &&  (SUCET>=8))  {ZNAMKA=2;} ;
			if ((SUCET<8)  &&  (SUCET>=5))  {ZNAMKA=3;} ;
			if ((SUCET<5)  &&  (SUCET>=2))  {ZNAMKA=4;} ;
			if (SUCET<2)                    {ZNAMKA=5;} ;

		s.znamka=ZNAMKA;

		objStudent.pridajStudenta(s);
	}

	vector<STUDENT> container1 = objStudent.getStudent();
	vector<STUDENT>::iterator it;

for(it = container1.begin(); it != container1.end(); ++it)
	{
		cout.width(10);
		cout << left<<(*it).priezvisko <<"   "<< (*it).meno<<" \t"; 

			for (int i=0; i<it->body.size(); i++)
				cout<<it->body[i] <<"\t";
				cout<<endl;
	}
	cout<<endl;

for(it = container1.begin(); it != container1.end(); ++it)
	{
		cout.width(10);
		cout <<left << (*it).priezvisko <<"   " << (*it).meno<<" \t" << (*it).sucet<<" \t" << (*it).znamka<<" \t" ; 
		cout<<endl;
	}

	cout<<endl;
	cout<<"Zadaj priezvisko hladaneho studenta"<<endl;
	cin>>hladaj;
	cout<<endl;

for(it = container1.begin(); it != container1.end(); ++it) 
	{
		cout.width(10);
		if ((*it).priezvisko==hladaj) {cout<<(*it).priezvisko <<"   " << (*it).meno<<"\t" << (*it).sucet<<"\t" << (*it).znamka<<" \t"<<endl;
		a=true;}
	}

		if (a==false) {cout<<"hladany student sa v zozname nenachadza"<<endl;}

}
else {cout<<"vstupny subor sa nepodarilo otvorit"<<endl;}


return 0;

}


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
#include "stdafx.h"
#include "student.h"
using namespace std;

student::student()
{

}

student::~student(void)
{
}

void student::pridajStudenta(STUDENT Student)
{
	studenti.push_back(Student);
}


vector<STUDENT> student::getStudent()
{
	return studenti;
}

	

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
#include <vector>
#include "stdafx.h"
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;

struct STUDENT {

	string meno;
	string priezvisko;
	vector<double> body;
	double sucet;
	double znamka;
};


class student
{
public:
	student();
	~student(void);
	vector<STUDENT> getStudent();
	void pridajStudenta(STUDENT Student);
private:
	vector<STUDENT> studenti;
};
student.cpp Line 1, 11. Get rid of stdafx.h, _tmain and TCHAR. These are Microsoft specific. Make this a simple and portable console project. _tmain and TCHAR make your program non-portable.

student.h line 19. Since this is a collection of students, I would make the name of the class plural (students) to indicate that it contains multiple students.

student.h line 24: I would return vector<STUDENT> by reference. Returning by value causes the entire vector to be copied.

main.cpp line 65: I would make the loop variable unsigned int. int causes a compiler warning due to a type mismatch with the return type of body.size().

main.cpp line 15: I'm unclear what this vector is used for. I see you iterate through it at line 65, but I don't see you ever put anything in the vector.







Topic archived. No new replies allowed.