How to make this nested class work?

  • Context: MHB 
  • Thread starter Thread starter FallArk
  • Start date Start date
  • Tags Tags
    Class Work
Click For Summary
SUMMARY

The discussion focuses on implementing a nested class structure in C++ to calculate grades through user input. The main class, Grade, contains a nested class Textbook that captures textbook exercise scores. The user input loop in the getExercises method fails to terminate correctly when the user inputs -1, causing repeated prompts. The solution involves correcting the loop logic and ensuring that the total points calculation reflects the actual number of exercises completed.

PREREQUISITES
  • Understanding of C++ class and object-oriented programming concepts
  • Familiarity with nested classes in C++
  • Knowledge of input/output operations in C++ using cin and cout
  • Basic grasp of control flow statements, particularly loops
NEXT STEPS
  • Review C++ nested class implementation and usage
  • Learn about C++ control flow, focusing on for loops and break statements
  • Explore error handling in C++ to manage invalid user inputs
  • Investigate best practices for calculating and displaying totals in C++ applications
USEFUL FOR

C++ developers, students learning object-oriented programming, and anyone interested in implementing user input-driven applications for grade calculations.

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.
 

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