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