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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

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