Hohlkopf
Hohlkopf

Reputation: 15

C++ User interaction in classes

In first semester CS when I had procedural programming the lecturer always told not to mix program functionality and interaction with the user in the same functions. Now I have object oriented programming and I am confused how to implement interaction with the user in the classes.

Here I started coding a project that lets the user solve math equations and I wrote a class 'Task' like this. Is it a bad practice in object oriented programming to mix input/output with the rest of the code?


#include <iostream>
#include <ctime>
#include <tgmath.h>

using namespace std;

enum operation{
    add, sub, mult, divis, random
};

class Task{
    private:
        int min;
        int max;
    public:
        Task(int mi, int ma){
            min = mi;
            max = ma;
        }
        void setIntervall(int mi, int ma){
            min = mi;
            max = ma;
        }
        int getRand(){
            return (rand() % ((max-min) + 1)) + min;
            int min = max / 10;
            return (rand() % (max - min)) + min;
        }
        void printTask(operation op){
            if(op == random)
                op = (operation)(rand() % 4);
            if(op == add)
                cout << getRand() << " + " << getRand() << endl;
            if(op == sub)
                cout << getRand() << " - " << getRand() << endl;
            if(op == mult)
                cout << getRand() << " * " << getRand() << endl;
            if(op == divis)
                cout << getRand() << " / " << getRand() << endl;
        }
        bool checkifRight(double result);
};



int main(){

    srand(time(NULL));

    Task Task(15,100);
    
    return 0;
}

Upvotes: 1

Views: 220

Answers (1)

TheEagle
TheEagle

Reputation: 5992

cout << something is not exactly "interaction with the user", it just informs the user of something, so this is ok I think. And as you have no input in your code, you are fine so far.

But there is no sense in having multiple if statements in a row to check which operation is being use, use else if instead.

Upvotes: 1

Related Questions