MHB How to make this nested class work?

  • Thread starter Thread starter FallArk
  • Start date Start date
  • Tags Tags
    Class Work
AI Thread 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
35
Views
4K
Replies
1
Views
1K
Replies
5
Views
3K
Replies
23
Views
2K
Replies
75
Views
6K
Replies
36
Views
3K
Replies
17
Views
2K
Back
Top