Hello eripiomundus,
Along with what jlb has asked the errors that you should have received is good information of what to fix.
It took me awhile to figure out the problems because some were not easy to find.
In your ".cpp" files it is best not to use the line
using namespace std;
as it
WILL get you in trouble some day. Even now it is a bit of a problem I think.
As a note in "main" you include the header file "Header.h" before the using statement, so the header file can not make use of the using statement. In the end it is better to learn to qualify what is in the standard name space such as ; "std::cout", "std::cin", "std::endl", and " "std::flush" along with others. Learning this now is easier than trying to learn t all at once later.
Another note: When creating the header file and ".cpp" file for a class it is better to name the files the same as the class. For the header files I like to use the extension ".hpp" so as not to confuse the header file with a C header file using the extension ".h".
In your header file the include guard uses the name "Students". This "#define" overshadows the class name. It is best to use all caps with a "#define". As you will see in my revised code this made a big difference.
Note: for, while loops and all manner of if statements with only one line of code do not need the {} unless you have plans to add to it later.
I revised your code to look like this:
main:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <iostream>
#include <string>
#include <limits>
#include "Student.hpp"
int main()
{
Students students("John Smith");
students.inputMarks();
students.studentSummary();
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
//system("pause");
return 0;
}
|
Student.hpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#pragma once
#ifndef STUDENTS
#define STUDENTS
class Students
{
public:
Students(std::string n);
void studentSummary();
void inputMarks();
private:
std::string sName;
int sNum;
static int nextID;
int sMarks[4];
};
#endif // !STUDENTS
|
Student.cpp:
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
|
#include <iostream>
#include <string>
#include "Student.hpp"
int Students::nextID = 10000;
Students::Students(std::string n)
{
sName = n;
sNum = nextID++;
for (int i = 0; i < 4; i++)
sMarks[i] = 0;
}
void Students::inputMarks()
{
for (int i = 0; i < 4; i++)
{
std::cout << "Please enter the marks for assessment " << i + 1 << " > " << std::flush;
std::cin >> sMarks[i];
std::cout << std::endl;
}
}
void Students::studentSummary()
{
int runningTotal = 0;
std::cout << "Student name > " << sName << std::endl;
std::cout << "Student No > " << sNum << std::endl;
for (int i = 0; i < 4; i++)
{
std::cout << "Marks for assignment " << i << " > " << sMarks[i] << std::endl;
runningTotal += sMarks[i];
}
std::cout << "Total marks > " << runningTotal << " > ";
if (runningTotal > 80)
std::cout << "HD\n";
else if (runningTotal > 70)
std::cout << "D\n";
else if (runningTotal > 60)
std::cout << "C\n";
else if (runningTotal > 50)
std::cout << "P\n";
else
std::cout << "N\n";
}
|
In "main" it is best not to use "system" anything. Not only is it not portable it limits the code to Windows only. The four lines I added above the "system" call are a fair replacement. And as the comments say do not forget to include the header file "<limits>".
Also notice how the {} for blocks line up in the same column. Not only does this make the code easier to read, with proper indenting, it makes it easier to find a missing closing }. Although there is nothing wrong with what you did I find that it makes for neater code.
Hope that helps,
Andy