c++ practice turned to frustration


I offered to make my girlfriend a grade calculator for some c++ practice. I've got this far to find out that it outputs all my module scores as 0 and the overall score to be a ridiculously small number... Any assistance would be appreciated to calm my nerves before my exam on Monday. I wouldn't be surprised if there are a number of stupid mistakes, I've been awake for 36 hours :(
Thanks,
Jake

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
  #include <iostream>
#include<sstream>
#include <vector>

using namespace std;


double input(string &moduleName)
{
	int assessNum, x, y;
	double moduleScore = 0;
	vector <int> moduleMark;
	vector <int> moduleWeight;
	cout << "Enter module name: ";
	cin >> moduleName;

	cout << endl << "Enter number of assessments in this module: ";
	cin >> assessNum;


	for (int i = 0; i < assessNum; i++)
	{
		cout << endl << "Enter mark in each assessment, followed by its weight (as a percentage)" << endl
			<< "Assessment " << i + 1 << " mark :";
		cin >> x;
		moduleMark.push_back(i);

		cout << endl << "Assessment " << i + 1 << " weight :";
		cin >> y;
		moduleWeight.push_back(y);
		cout << endl;
	}

	for (int i = 0; i < moduleMark.size(); i++)
		moduleScore = moduleScore + (moduleMark[i] * (moduleWeight[i] / 100));

	return moduleScore;
}

double overall(double arr1[5])
{
	double score = 0;

	for (int i = 0; i < 5; i++)
		score = score + arr1[i];

	return score;
}


int main()
{
	double scores[5];
	string moduleName = "poop";

	for (int i = 0; i < 6; i++)
		scores[i] = input(moduleName);

	for (int i = 0; i < 6; i++)
		cout << "Module " << i + 1 << "score " << scores[i] << "%" << endl;

	cout << endl << "Overall mark is " << overall(&scores[5]);

}
Last edited on
A girlfriend and/or exam? Whatever.


Line 26.

Line 35. Integer division. Make the 100 a 100.0.
The overall function takes a pointer as argument. yes it looks like an array but arr1 is actually a pointer. Just so that you understand what is going on I will rewrite the overall function using pointer syntax. This shows exactly what is going on. I don't mean you should change your code, just look at it.

1
2
3
4
5
6
7
8
9
double overall(double* arr1)
{
	double score = 0;

	for (int i = 0; i < 5; i++)
		score = score + *(arr1 + i);

	return score;
}


The problem is when you call the function and pass &scores[5] as argument. scores[5] is the sixth element in the scores array, but the array only has 5 elements so this is out of bounds. So the function will actually access scores[5...9] which are all out of bounds it will probably just read what happens to be stored after the array in memory.

What you should do instead is to pass a pointer to the first element in the array: overall(&scores[0]); or overall(scores); will also work because the array will automatically decay to a pointer to the first element when it's passed to the function.

Another problem with your code is the two for loops in the main function because scores[i] is out of bounds when i=5.
Last edited on
Thanks for the help, moduleScore is still coming up as zero every time though :/
Oh wait, Line 26, I see now...
Topic archived. No new replies allowed.