Hello ladies and gents,

Ive been trying to find a solution for this exercise wich goes as follows:

Rewrite the Student_info structure, and the 'read' and 'grade' functions, so that they calculate each student's grades as part of reading the input, and store only the final grade.

Because the exercise is 'rather large', I won't post it, but was wondering if any of you, who have Accelerated C++ have the solution for this exercise. I have searched threw Google but came up with nothing and it's the last exercise from chapter four I have to do. But, thing is, though I'm moving on to the next chapter, I would love to find out what it is actually the solution for this exercise.

So, if any of you guys or girls have it, I would appreciate it if you could share it, I'm just dying to know how I should have solved it.

Thanks.

hi jobe,
even if the problem may be large better post the problem and your program... there are lot more learning c++ and already know c++ without Acc c++... so better post it..
bye

Well seeing as noone replied with any answer to the question, I thought it best if I tried to find the solution one step at a time, afterall, I have to know it.

So, starting of with the Student_info structure:

struct Student_info
{
	std::string name;
	double midterm, final;
	std::vector<double> homework;
};

and looking at the exercise I decided to remove two double variables midterm, final and the vector homework and, adding another variable called finalGrade.

Reason for removing them is that all the grades have to be calculated in the functions themselves and you only have to save the final grade and for that I don't see a reason why I should keep the vector.

I was thinking of adding another variable int count to it, so, I could count the amount of grades I have entered, but, then I thought, this could be a local variable aswell because the calculation happens in the function.

So, my structure looks like this now:

struct Student_info
{
	std::string name;
	double finalGrade;
};

Going to the read-functions:

istream& read(istream& is, Student_info& s)
{
	is >> s.name >> s.midterm >> s.final;

	read_hw(is, s.homework);	//read and store all the student's homework grades

	return is;
}

istream& read_hw(istream& in, vector<double>& hw)
{
	if (in)
	{
		//get rid of previous contents
		hw.clear();

		//read homework grades
		double x;
		while (in >> x)
			hw.push_back(x);

		//clear the stream so that input will work for the next student
		in.clear();
	}

	return in;
}

The function read as you can see has two parameters, an input parameter and a Student_info object. Since I deleted the two variables (midterm, final) I have to delete these from the input stream and leave only the s.name.

But then, as this function calls the read_hw function, I have to use two arguments in the call, the input argument can be left, but the homework no longer is needed so can be deleted. However, I have to change this into s.finalGrade because otherwise, I won't be able to input the average grade into the finalGrade from that particular student. Thing is, if doing so, I can't use the second parameter of read_hw because this relates to a vector<double> and not to a parameter of a struct?
Anyway, the way I picture the read_hw function to be is:

istream& read(istream& is, Student_info& s)
{
	is >> s.name;

	read_hw(is, s.finalGrade);	//read and store all the student's homework grades

	return is;
}

Now comes the tricky part for me because now, my second argument in calling the read_hw function doesn't equal the second parameter in the read_hw function.
So, question is how do I solve this. I also believe that the calculation for the finalGrade has to be done in the read_hw function and that this result should be put into the finalGrade member of the struct Student_info.

So, my effort for the read_hw function looks like this:

istream& read_hw(istream& in, Student_info& s)
{
	double count = 0.0;
	double grades = 0.0;

	if (in)
	{
		//get rid of previous contents
		in.clear();

		//read homework grades
		cout << "Read in the grades, to exit, press Ctrl 'z'." << endl;
		double x;
		while (in >> x)
		{
			if (count == 0.0)
				x *= 0.2;
			else
				x *= 0.4

			grades += x;
			count++;
		}
		s.finalGrade = grades/count;
	}

	return in;
}

I'll keep it to this for now and once any of you guys or girls can help me out with these, I'll go further with the grade functions.

Thanks.

Hi guys,

Just thought that it would be easy for you if you could see the full version of this example, so, I included a zip file.

Hi Jobe,
I have just started learning c++ after going through c.
I am still learning the concepts so at this moment I can't answer your problem. I am a learning so I am just looking for different types of problems so I asked you for your one.. :o Thanks for posting such a huge reply.. sorry I couldn't help.. but i will see if someone else helps..
bye and sorry again!!!!

Hi Jobe,
I have just started learning c++ after going through c.
I am still learning the concepts so at this moment I can't answer your problem. I am a learning so I am just looking for different types of problems so I asked you for your one.. :o Thanks for posting such a huge reply.. sorry I couldn't help..

No problem HW, have fun in learning C++, I know I have ;)

but i will see if someone else helps..
bye and sorry again!!!!

LOL, Ive noticed it, and though I think it's nice, it really isn't necessary, I'm sure if anyone can help, they'll do so. Reading your reply in Comwizz his thread towards Narue, she isn't obligated to help, if she wants to, she will, apparantly for a certain reason, she feels that it's better not to, whatever that might be, I respect that decission.

Anyway, if any have usefull tips, don't be shy and please tell, I'm at a stand still. Though not giving up yet ;)

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.