+ 1

how to get total in c++

//main #include "header.h" int main() { person obj; obj.get(); obj.display(); grades o; o.getG(); o.displayG(); score s; s.getM(); s.tot(); s.displayM(); return 0; } //header.h #include <iostream> using namespace std; const int subjects = 5; class person { private : int age; string name; public: person(){ age=0; name="n"; } ~person(){ } void get(); void display(); }; class grades { private: int grade; public: grades(){ grade =0; } void getG(); void displayG(); }; class score { private: int marks; int total; public: public: score(){ marks =0; total = 0; } void getM(); void tot(); void displayM(); }; //imp.cpp #include "header.h" void person::display() { cout<<"Name is "<<name<<endl; cout<<"Age is "<<age<<endl; } void person:: get() { cout<<"Enter the name "<<endl; cin>>name; cout<<"Enter the age "<<endl; cin>>age; } void grades::displayG() { cout<<"The Grade is "<<grade<<endl; } void grades:: getG() { cout<<"Enter the Grade "<<endl; cin>>grade; } void score::displayM() { cout<<"The score is "<<total<<endl; } void score:: getM() { for (int i; i<= subjects;i++) cout<<"Enter the score of the subject "<<i<<endl; cin>>marks; } void score :: tot() { total = total +marks; } I'm struggling at getting the for statement at void score:: getM() to work if anyone can help me it would be great;

14th Oct 2022, 8:55 PM
Khaled
Khaled - avatar
1 Respuesta
0
In the score::getM() for statement, i is not fully initialized with a value. Also, there need to be curly brackets around the contents of the for statement. for(int i = 0; i <= subjects; i++) { cout << "Enter the score of the subject " << i << endl; cin >> marks; }
25th Oct 2022, 11:13 PM
Peter Nicholas
Peter Nicholas - avatar