MHB How to make this nested class work?

  • Thread starter Thread starter FallArk
  • Start date Start date
  • Tags Tags
    Class Work
Click For Summary
The discussion revolves around creating a grading program using classes and subclasses in C++. The main function is fixed and calls various methods to gather grades for different components, including exercises, clickers, labs, traces, assignments, tests, prefinals, and finals. The user has implemented a `Grade` class with a nested `Textbook` subclass to handle textbook exercise inputs. The user aims to develop a template for grade input that can be reused for other components. However, they are facing issues with the loop in the `getExercises` method, which does not exit correctly when the user inputs -1, causing the program to repeatedly prompt for input. Additionally, they are struggling to correctly calculate the total points for the textbook exercises based on user input and the number of iterations in the loop. The user seeks assistance to resolve these logic errors and achieve the desired functionality in their grading program.
FallArk
Messages
127
Reaction score
0
So, I was asked to make a program that will calculate my own grade, and it is required to use subclasses inside a class.
main is posted below and cannot be changed:
Code:
int main() {
Grade gradeMe;
gradeMe.getExercises();
gradeMe.getClickers();
gradeMe.getLabs();
gradeMe.getTraces();
gradeMe.getAssignments();
gradeMe.getTests();
gradeMe.getPrefinal();
gradeMe.getFinal();
gradeMe.displayTotals();
}

what I have done:
Code:
class Grade {
public:
    class Textbook{
public:
    int textbookPts;
    int i = 1;
    int bookPts() {
        cout << "Enter textbook exercise #" << i << " (out of 10 points): ";
        cin >> textbookPts;
        ++i;
        return textbookPts;
    }
    };
    Textbook Text[10];
    int getExercises() {
        for (int x = 0; x < 10; ++x){
            Text[x].bookPts();
            textbookSum += Text[x].bookPts();
            if (Text[x].bookPts() == -1)
                break;
            textbookTotal = x * 10;
        }
        cout << textbookSum << " out of " << textbookTotal << " points for textbook grade" << endl;
        return textbookSum;
    }
private:
    int textbookSum = 1;
    int textbookTotal = 0;
};

My idea is to find the general case of this program, then use it as a template to fill the others. I was thinking that create a class that will ask the user(myself in this case) to input the grade, and then declare this subclass in class Grade, the getexercises function then have a for loop to keep asking for input until the user input -1. But as of now, the loop will not exit when -1 is entered, and it keeps going #1... #2... #3... #1... etc, instead of going 1...2...3...4...5...6...
Help!:confused:
 
Technology news on Phys.org
I also was trying to set the sum of what the user get on the textbook exercises equal to the sum of the inputs, and the total of textbook exercises points equal to how many time the loop executed times the points of one exercises. The program is not really doing what I think it would, I must have messed up somewhere.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
12
Views
3K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 17 ·
Replies
17
Views
2K