0.00 output to calculate %

I need to create a class that will display a list of modules in an array. In the array must be module name, module code, assignment 1 mark (in percentage), assignment 2 marks (also in percentage) and the semester mark which must be calculated. The weight of the assignments is 30% and 70%, respectively. The semester mark should be calculated as (A1*0.30)+(A2*0.70). For example: (60*0.30)+(65*0.70) - should output 63.5% as the semester mark. This can be seen in line 95 of my code, return (assign1*0.30) + (assign2*0.70);, but as the title of states, I am getting an output of "0.00%"

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
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <string>
using namespace std;

class Module
{
public:
    Module();
    Module(string nameX, string codeX, float assign1X, float assign2X, float semMarkX);
    void setName(string nameX);
    void setCode(string codeX);
    void setAss1(float assign1X);
    void setAss2(float assign2X);
    void setMark(float semMarkX);
    string getName() const;
    string getCode() const;
    float getAss1() const;
    float getAss2() const;
    float getMark() const;

private:
    string name;
	string code;
	float assign1;
	float assign2;
	float semMark;

};

Module::Module()
{
    name = " ";
    code = " ";
    assign1 = 0;
    assign2 = 0;
    semMark = 0;
}

Module::Module(string nameX, string codeX, float assign1X, float assign2X, float semMarkX)
{
    name = nameX;
    code = codeX;
    assign1 = assign1X;
    assign2 = assign2X;
    semMark = semMarkX;
}

void Module::setName(string nameX)
{
    name = nameX;
}
void Module::setCode(string codeX)
{
    code = codeX;
}
void Module::setAss1(float assign1X)
{
    assign1 = assign1X;
}
void Module::setAss2(float assign2X)
{
    assign2 = assign2X;
}
void Module::setMark(float semMarkx)
{
    semMark = semMarkx;
}

string Module::getName() const
{
    return name;
}
string Module::getCode() const
{
    return code;
}
float Module::getAss1() const
{
    return assign1;
}
float Module::getAss2() const
{
    return assign2;
}
float Module::getMark() const
{
    return (assign1*0.30) + (assign2*0.70);
}

int main()
{
    Module AssignmentMarks[4];
    string nm, cd;
    float a1, a2, sm;

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    for (int i = 0; i < 4; i++)
    {
        cout << endl <<"Please enter details for all registered modules "<< i+1 << ": " << endl;
        cout << endl << "Enter the module name: " << endl;
        getline(cin, nm, '\n');
        AssignmentMarks[i].setName(nm);
        cout << endl << "Enter the module code: " << endl;
        getline(cin, cd, '\n');
        AssignmentMarks[i].setCode(cd);
        cout << endl << "Enter your mark for Assignment 1 (without % symbol): " << endl;
        cin >> a1;
        AssignmentMarks[i].setAss1(a1);
        cout << endl << "Enter your mark for Assignment 2 (without % symbol): " << endl;
        cin >> a2;
        AssignmentMarks[i].setAss2(a2);
        cout << endl << "Your semester mark is: " << sm << "%" << endl;
        AssignmentMarks[i].setMark(sm);
        cin.get();
    }
}
Last edited on
How do you expect sm to get a value? You never assign anything to it.
Hello StrangerThings,

The code in the for loop in "main" is hard to read. It is becoming one big blur.

Your for loop with some blank lines:
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
for (int i = 0; i < 4; i++)
{
	cout << endl << "Please enter details for all registered modules " << i + 1 << ": " << endl;
	cout << endl << "Enter the module name: " << endl;
	getline(cin, nm);

	AssignmentMarks[i].setName(nm);
	cout << endl << "Enter the module code: " << endl;
	getline(cin, cd);

	AssignmentMarks[i].setCode(cd);
	cout << endl << "Enter your mark for Assignment 1 (without % symbol): " << endl;
	cin >> a1;
		
	AssignmentMarks[i].setAss1(a1);
	cout << endl << "Enter your mark for Assignment 2 (without % symbol): " << endl;
	cin >> a2;
		
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. Added. Needed before the next "getline".

	AssignmentMarks[i].setAss2(a2);
		
	cout << endl << "Your semester mark is: " << sm << "%" << endl;
		
	AssignmentMarks[i].setMark(sm);
		
	cin.get();
}

My first question is for line 23. What is the value of "sm" at this point. When I tested the program it contained a garbage value. You have defined "sm", but never initialized it or gave it a proper value before it was use.

Then in the next line you are using that garbage value to set a variable in the class.

You refer to return (assign1*0.30) + (assign2*0.70); from the "float Module::getMark() const" function, but you never call this function, so how do you know it is returning (0) zero?

For these lines of code:
1
2
3
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
You could just as easily say:
std::cout << std::fixed << std::setprecision(2); // <--- Requires header file <iomanip>.
With "setprecision" having a value greater than zero it will print ".00" if it should come up. You could also put the "showpoint" in the line if you want.

A quick run of the program gave me this:

Please enter details for all registered modules 1:

Enter the module name:
Mod 1

Enter the module code:
A123

Enter your mark for Assignment 1 (without % symbol):
90

Enter your mark for Assignment 2 (without % symbol):
95

Your semester mark is: -92559631349317830736831783200707727132248687965119994463780864.00%

Your semester mark is: 93.50%

Please enter details for all registered modules 2:

Enter the module name:


The first "semester mark" is using the uninitialized variable "sm" and the second is using the function call " AssignmentMarks[i].getMark()".

Another point: getline(cin, nm); is all you need as the third parameter has a default value of "\n", so you do not need to restate something that is already there.

Andy

Topic archived. No new replies allowed.