Implementing Classes into a Program

So I wrote a code for a project and it worked fine, but another part of the project is to implement classes. I've been trying to write a class for the operations, but I cannot seem to get it. Any help would be great! (The code is about 300 lines, so I shortened it to one specific part. If someone helps with the addition, I'm sure I could figure out the rest of the operations.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  cout << "Welcome to the learning program.\nEnter 1 to practice addition\nEnter 2 to practice subtraction\nEnter 3 to practice multiplication\nEnter 4 to practice division\nEnter 5 to practice permutations and combinations: ";
    cin >> num; // user inputs a number to choose an operation to practice
    
    if (num == 1){ // if the user chooses 1, they will practice addition
        do {
        num1 = 1 + rand() % 20; // establishes the first number as a random number
        num2 = 1 + rand() % 20; // establishes the second number as a random number
        cout << "What is the sum of " << num1 << " and " << num2 << "?" << endl;
        cout << "Sum: ";
        cin >> answer; // the user will input their answer
        
        sum = (num1 + num2); // the sum is found by adding the two numbers
    
        rightStatement = 1 + rand() % 5; // sets a random number to choose a random right statement
        wrongStatement = 1 + rand() % 5; // sets a random number to choose a random wrong statement
        
    if (answer == sum) { // if the user's answer is correct, the following will occur
        switch (rightStatement)
        {
            case 1: // if the number is 1, this statement will be displayed
            cout << "Good job! That's five points for you";
            break; 
            case 2: // if the number is 2, this statement will be displayed
            cout << "I knew you were a smart kid!";
            break;
            case 3: // if the number is 3, this statement will be displayed
            cout << "I'm not surprised you got the answer right!";
            break;
            case 4: // if the number is 4, this statement will be displayed
            cout << "Fantastic!";
            break;
            case 5: // if the number is 5, this statement will be displayed
            cout << "Amazing work!";
            break;
        }
    points = points + 5; // if they get the answer right, the user gets 5 points
    cout << "\nYou now have " << points << " points\n"; // displays the amount of points
    streakcount++;
    cout << "You now have a streak of " << streakcount << "\n\n";
    }
    
    else { // if the user's answer is wrong, the following will occur
        switch (wrongStatement)
        {
            case 1: // if the number is 1, this statement will be displayed
            cout << "Your answer is as wrong as your parents' decision to keep you!";
            break;
            case 2: // if the number is 2, this statement will be displayed
            cout << "Go back to third grade cuz that's where you belong.";
            break;
            case 3: // if the number is 3, this statement will be displayed
            cout << "I'm not surprised you got the answer wrong!";
            break;
            case 4: // if the number is 4, this statement will be displayed
            cout << "Not the brightest, huh?";
            break;
            case 5: // if the number is 5, this statement will be displayed
            cout << "Better luck next year.";
            break;
        }
    points = points - 7; // subtracts 7 points from the user for getting the wrong answer
    cout << "\nYou now have " << points << " points\n"; // displays the amount of points
    
    if (counter > 1){
    streakcount = streakcount - streakcount;
    cout << "You lost your streak\n\n";
    }
    }
    counter++; // increases the counter by one
    } 
    while ((points >= 0) && (counter <= 20)); // the loop will run 20 times as long as the user has a positive amount of points
    }
Last edited on
Why not just develop a single class that contains four functions that deals with add/subtract/multiply/divide?
That's what I've been attempting to do but I am unsure how to go about it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class mathOperations // class that contains your basic math functions
{
    public:
        double add(double x, double y) { return (x+y); };
        double divide(double x, double y) { return (x/y); }
        double multiply(double x, double y) { return (x*y); }
        double subtract(double x, double y) { return (x-y); }
};

int main()
{
    // ...
    return 0;
}


This is one way of doing it.
Last edited on
Topic archived. No new replies allowed.