Help on classes with txtfile

Hi i am doing some practice problems and i can't seem to figure out how to do this. basically we have a students number of test scores, then the name followed by the scores they have in a text file. Then we have to make a class with a constructor, copy constructor, destructor, and overload the = operator and the input and output operator. Are we suppose to call the text file in the input overload operator?

Here is what i have so far.

This is my header file.

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
#ifndef STUDENTTESTSCORES_H
#define STUDENTTESTSCORES_H
#include <string>
#include <iostream>

using namespace std;

 class StudentTestScores{
    private:
	string studentName;
	double *testScores;
	int numTestScores;
   public:
	StudentTestScores(string name = "", int numScores = 0);	// class constructor	
	StudentTestScores(const StudentTestScores &other);      // class copy constructor
	~StudentTestScores();  				// class destructor

	//overloading = operator							
	StudentTestScores StudentTestScores:: operator=(const StudentTestScores   &other) 
{
	numTestScores = other.numTestScores;    
	studentName = other.studentName;
	delete [] testScores;
	testScores = new double[numTestScores];
	for (int i=0; i < numTestScores; ++i)
		testScores[i] = other.testScores[i];
	return other;
}
friend ofstream & operator << (ofstream & ostr,  const StudentTestScores &right) 
{
	ostr << right.numTestScores;
	ostr << right.studentName << '\t';
	for (int i=0; i < right.numTestScores; ++i)
		ostr << '\t'<<right.testScores[i];
	ostr << endl;

	return ostr;
}

friend   ifstream& operator >> (ifstream & istr, StudentTestScores &right)
{
	istr >> right.numTestScores;
	istr >> right.studentName >> '\t';
	for (int i=0; i < right.numTestScores; ++i)
		istr >> '\t'<<right.testScores[i];
	istr >> endl;

	return istr;
}


};
#endif  


i am 100% sure the overloading the input is wrong

here is the implementation of the constructor copy constructor and desctructor
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
#include <iostream>
#include "StudentTestScores.h"
using namespace std;

StudentTestScores::StudentTestScores(string name = "", int numScores = 0)
{
	studentName = name; 
	numTestScores = numScores;
	if (numScores <= 0)
		testScores = NULL;
	else
		testScores = new double[numTestScores];
}

StudentTestScores::StudentTestScores(const StudentTestScores& obj)
{
	studentName = obj.studentName;
	numTestScores = obj.numTestScores;
	testScores = new double [numTestScores];
	for (int i = 0; i < numTestScores; ++i)
		testScores[i] = obj.testScores[i];
}

StudentTestScores::~StudentTestScores()
{
	delete [] testScores;
}



and here is the notepad file

1
2
3
4
3
Justin Bieber	4	91.4	69.1	84.6	81.0	81.5
Miley Cyrus	3	80.0	80.0	90.0	83.3	
Kim K	4	90.5	75.6	61.4	81.6	77.2


The program is suppose to use all the information and read from the notepad and output the exact things as the notepad file
Last edited on
anybody?
i am 100% sure the overloading the input is wrong
Yes;)
1
2
3
4
5
6
7
8
9
friend   ifstream& operator >> (ifstream & istr, StudentTestScores &right)
{
	istr >> right.numTestScores;
	istr >> right.studentName >> '\t';
	for (int i=0; i < right.numTestScores; ++i)
		istr >> '\t'<<right.testScores[i];
	istr >> endl;
	return istr;
}


And this:
1
2
3
4
5
6
7
8
9
10
11
	//overloading = operator							
	StudentTestScores & StudentTestScores:: operator=(const StudentTestScores   &other) // Notice &
{
	numTestScores = other.numTestScores;    
	studentName = other.studentName;
	delete [] testScores;
	testScores = new double[numTestScores];
	for (int i=0; i < numTestScores; ++i)
		testScores[i] = other.testScores[i];
	return other *this;
}
ah ok thank you =D, and also so how do i make it so that it reads from the textfile and outputs what ever the textfiles says?
You mean something like this:
1
2
3
4
5
6
7
ifstream ifs(...);
...
StudentTestScores sts;
while(ifs >> sts)
{
  cout << sts;
}
?
um i don't think i learned that yet, how does that read from a notepad?
are you suppose to put that into the overloading the input operator?
if you just want to output the content of a text file use getline:

http://www.cplusplus.com/reference/string/string/getline/?kw=getline

1
2
3
4
5
6
7
ifstream ifs(...);
...
string str;
while(getline(ifs, str))
{
  cout << str << endl;
}


um i don't think i learned that yet, how does that read from a notepad?
you learned how to overload stream operators, but not how to use them?

are you suppose to put that into the overloading the input operator?
No, it uses the overloaded stream operators
Last edited on
Topic archived. No new replies allowed.