• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Compiling [C++] How To Make A Simple Calculation?

Aelu

root@Aelu:~#
Joined
Sep 23, 2015
Messages
95
Solutions
1
Reaction score
31
Location
127.0.0.1
Basically, say that I have the following code snippet...

Code:
#include <iostream>
using namespace std;

class Money {
 
public:
    double hours;
    double wage;
 

   int main()
   Money A;
   Money B;
   Money C;
   Money D;
   Money E;
   double MoneyEarned = 0.0;

   A.hours = 43.6;
   A.wage = 20.50;

   B.hours = 38.4;
   B.wage = 24.00;

   C.hours = 46.9;
   C.wage = 20.50;

   D.hours = 44.2;
   D.wage = 22.0;

   E.hours = 39.5;
   E.wage = 50.00;

   AEarned = A.wage * A.hours
   BEarned = B.wage * B.hours
   CEarned = C.wage * C.hours
   DEarned = D.wage * D.hours
   EEarned = E.wage * E.hours

   cout << "A earned: " << AEarned << endl;
   etc. etc. etc.
};

However, I do not want to do the "x.wage * x.hours" calculation and "cout << x earned: " << xEarned << endl;" shit over and over, so it would look something like this...
Code:
#include <iostream>
using namespace std;

class Money {
public:
    double hours;
    double wage;
    };
 
    int main() {
        Money A;
        Money B;
        Money C;
        Money D;
        Money E;
        double MoneyAmount = 0.0;
     
        A.hours = 43.6;
        A.wage = 20.50;
     
        B.hours = 38.4;
        B.wage = 24.00;
     
        C.hours = 46.9;
        C.wage = 20.50;
     
        D.hours = 44.2;
        D.wage = 22.00;
     
        E.hours = 39.5;
        E.wage = 50.00;
     
        MoneyAmount = Money(wage) * Money(hours);
        cout << "[x] earned: " << MoneyAmount << endl;
     
    return 0;
}

Now, I know that the above code won't work whatsoever, I'm not an idiot. That's just to give you guys an idea of what I'm looking for. Is this possible to do with just Classes and Objects? Or would I have to add a Constructor or Array or something?

What I'm looking for is something like this..

1. Create a Class "Money"
2. Create Objects in Class "Money" [A, B, C, D, E] with values "wage" and "hours".
3. Calculate ALL Object's "MoneyAmount" in one line with "wage * hours"
4. cout code with "[x] earned: x"
 
Solution
Ahh yes. Very very close. Although, is it possible to say like "A earned: x" etc? Without just printing the values. That's it tho!
Code:
#include <bits/stdc++.h>

using namespace std;

struct Employee {
    string name;
    double hours;
    double wage;

    double earned() {
        return hours*wage;
    }

};

class Money {
    public:
        vector<Employee*> data;

        Money(double *arr, const char **names, int size) {
            for(int i = 0; i < size; i+= 2, arr+=2, names++) {
                Employee* tmp = new Employee;
                tmp->name = string(*names);
                tmp->hours = *arr;
                tmp->wage = *(arr + 1);
                data.emplace_back(tmp);
            }
        }

        void...
I don't really know c++ but I think you can have more than one constructor inside a class similar to having more than one method with the same name, I believe it is called overloading.

Best to stick to the tutorial, book or whatever material/resource you are using to learn from and not skip ahead, this way you understand exactly what you are doing :)
 
Perhaps. Maybe there is a way to do it in an array? Here's a more simple version of the code.
Code:
#include <iostream>
using namespace std;

//New Class: Money
class Money {
     
public:
    double x; //Hours
    double y; //Wage
      
   int main()
   Money A;
   Money B;
   Money C;
   Money D;
   Money E;
  
   //Initial Total Values
   double tA = 0.0;
   double tB = 0.0;
   double tC = 0.0;
   double tD = 0.0;
   double tE = 0.0;
  
   //Hours + Wages :: x = Hours :: y = Wage
   A.x = 43.6;
   A.y = 20.50;
  
   B.x = 38.4;
   B.y = 24.00;
  
   C.x = 46.9;
   C.y = 20.50;
  
   D.x = 44.2;
   D.y = 22.0;
  
   E.x = 39.5;
   E.y = 50.00;
  
   //Calculations
   tA = A.x * A.y
   tB = B.x * B.y
   tC = C.x * C.y
   tD = D.x * D.y
   tE = E.x * E.y
  
   //Outputs
   cout << "A earned: " << tA << endl;
   oout << "B earned: " << tB << endl;
   cout << "C earned: " << tC << endl;
   cout << "D earned: " << tD << endl;
   cout << "E earned: " << tE << endl;
  
return 0;
};
 
Maybe like this?

Code:
#include <iostream>
#include <map>
using namespace std;

class Money {

public:
  double hours;
  double wage;

  Money(double hours, double wage) {
    this->hours = hours;
    this->wage = wage;
  }

  double earned() {
    return hours * wage;
  }
};

int main() {
  map<char, Money*> m;
  m['A'] = new Money(43.6, 20.50);
  m['B'] = new Money(38.4, 24.0);
  m['C'] = new Money(46.9, 20.50);
  m['D'] = new Money(44.2, 22.0);
  m['E'] = new Money(39.5, 50.0);

  for (auto pair: m) {
    cout << pair.first <<" earned: " << pair.second->earned() << endl;
    delete pair.second; //clean up allocated object
  }

  return 0;
}
 
Maybe like this?

Code:
#include <iostream>
#include <map>
using namespace std;

class Money {

public:
  double hours;
  double wage;

  Money(double hours, double wage) {
    this->hours = hours;
    this->wage = wage;
  }

  double earned() {
    return hours * wage;
  }
};

int main() {
  map<char, Money*> m;
  m['A'] = new Money(43.6, 20.50);
  m['B'] = new Money(38.4, 24.0);
  m['C'] = new Money(46.9, 20.50);
  m['D'] = new Money(44.2, 22.0);
  m['E'] = new Money(39.5, 50.0);

  for (auto pair: m) {
    cout << pair.first <<" earned: " << pair.second->earned() << endl;
    delete pair.second; //clean up allocated object
  }

  return 0;
}

Awesome man. That's exactly what I was looking for. Thanks for helping me, I'm still learning:)
But one thing, is there a way to replace the "m['A]" etc. things where it tells the code A's, B's, C's, D's, and E's wage and hours all at one time, instead of in a table like it is?

I thought that looked rather odd having main inside of the class. o_O
And yeah aha, I was up for several hours and on sleep aids when I was writing this, so trying to do it flawlessly was rather difficult.
 
How do you mean at one time? You still have to specify the values somehow. One thing you can do to avoid a big list of data is to read the values from a file or database.
 
How do you mean at one time? You still have to specify the values somehow. One thing you can do to avoid a big list of data is to read the values from a file or database.

I know this program would be completely useless, and I'd be 100% better off reading values from a file or database, but I'm just wondering if it's even possible in C++.

Like, somehow doing this..
1. Defining the people [A, B, C, D, E]
2. Defining wage and hours as x,y
3. Array of wage/hours
4. Defining A, B, C, D, and E's values in the array or arrays.
5. Then doing the cout << etc. like Elime did.

Like:
Code:
//Define A, B, C, D, and E
//Create an array something like..
int Hours[5] {43.6, 38.4, 46.9, 44.2, 39.5};
int Wage[5] {20.50, 24.0, 20.50, 22.0, 50.0};
//Set A, B, C, D, and E's values in the Arrays (I have no idea how)
//Then print the totals
cout << pair.first << " earned: " << tA << endl;
//Is this even possible in C++ alone?
Where "tA" is equal to total earned by A.
 
Awesome man. That's exactly what I was looking for. Thanks for helping me, I'm still learning:)
But one thing, is there a way to replace the "m['A]" etc. things where it tells the code A's, B's, C's, D's, and E's wage and hours all at one time, instead of in a table like it is?


And yeah aha, I was up for several hours and on sleep aids when I was writing this, so trying to do it flawlessly was rather difficult.
Code:
#include <bits/stdc++.h>

using namespace std;

struct Payment {
    double hours;
    double wage;

    double earned() {
        return hours*wage;
    }

};

class Money {
    public:
        vector<Payment*> data;

        Money(double *arr, int size) {
            for(int i = 0; i < size; i+= 2, arr+=2) {
                Payment* tmp = new Payment;
                tmp->hours = *arr;
                tmp->wage = *(arr + 1);
                data.emplace_back(tmp);
            }
        }

        void printData() {
            for(auto info: data) {
                cout << info->earned() << endl;
            }
        }
};

int main() {
    double data[100] = {43.6, 20.50, 38.4, 24.0, 46.9, 20.50, 44.2, 22.0, 39.5, 50.0};
    Money* obj = new Money(data, 10);
    obj->printData();
    return 0;
}


You mean this?
 
Code:
#include <bits/stdc++.h>

using namespace std;

struct Payment {
    double hours;
    double wage;

    double earned() {
        return hours*wage;
    }

};

class Money {
    public:
        vector<Payment*> data;

        Money(double *arr, int size) {
            for(int i = 0; i < size; i+= 2, arr+=2) {
                Payment* tmp = new Payment;
                tmp->hours = *arr;
                tmp->wage = *(arr + 1);
                data.emplace_back(tmp);
            }
        }

        void printData() {
            for(auto info: data) {
                cout << info->earned() << endl;
            }
        }
};

int main() {
    double data[100] = {43.6, 20.50, 38.4, 24.0, 46.9, 20.50, 44.2, 22.0, 39.5, 50.0};
    Money* obj = new Money(data, 10);
    obj->printData();
    return 0;
}


You mean this?
Ahh yes. Very very close. Although, is it possible to say like "A earned: x" etc? Without just printing the values. That's it tho!
 
Ahh yes. Very very close. Although, is it possible to say like "A earned: x" etc? Without just printing the values. That's it tho!
Code:
#include <bits/stdc++.h>

using namespace std;

struct Employee {
    string name;
    double hours;
    double wage;

    double earned() {
        return hours*wage;
    }

};

class Money {
    public:
        vector<Employee*> data;

        Money(double *arr, const char **names, int size) {
            for(int i = 0; i < size; i+= 2, arr+=2, names++) {
                Employee* tmp = new Employee;
                tmp->name = string(*names);
                tmp->hours = *arr;
                tmp->wage = *(arr + 1);
                data.emplace_back(tmp);
            }
        }

        void printData() {
            for(auto info: data) {
                //cout << info->name << " earned: " << info->earned() << endl;
                printf("%s earned: %.2f\n", info->name.c_str(), info->earned());
            }
        }

        void addHours(double hours) {
            for(auto info: data) {
                info->hours += hours;
            }
        }

        void addWage(double wage) {
            for(auto info: data) {
                info->wage += wage;
            }
        }

        void addEmployee(const string& name, double wage, double hours) {
            Employee* tmp = new Employee;
            tmp->name = string(name);
            tmp->wage = wage;
            tmp->hours = hours;
            data.emplace_back(tmp);
        }

        Employee* getEmployeeByName(const string& name) {
            vector<Employee*>::iterator it = find_if(data.begin(), data.end(), [&](Employee* tmp) {
                return name.compare(tmp->name) == 0;
            });

            if(it != data.end()) {
                return *it;
            }

            return nullptr;
        }
};

int main() {
    double data[100] = {43.6, 20.50, 38.4, 24.0, 46.9, 20.50, 44.2, 22.0, 39.5, 50.0};
    char const *names[] = {"A", "B", "C", "D", "SOMETHING"};
    Money* obj = new Money(data, names, 10);
    obj->printData();
    obj->addWage(10);
    obj->addEmployee("Mkalo", 100, 100);
    Employee* a = obj->getEmployeeByName("Mkalo");
    if(a != nullptr) {
        printf("%s is gettin mad moneys: %.2f\n", a->name.c_str(), a->earned());
    }
    return 0;
}
(changed payment to employee)

Throwed some functions for ya.
 
Last edited:
Solution
Code:
#include <bits/stdc++.h>

using namespace std;

struct Employee {
    string name;
    double hours;
    double wage;

    double earned() {
        return hours*wage;
    }

};

class Money {
    public:
        vector<Employee*> data;

        Money(double *arr, const char **names, int size) {
            for(int i = 0; i < size; i+= 2, arr+=2, names++) {
                Employee* tmp = new Employee;
                tmp->name = string(*names);
                tmp->hours = *arr;
                tmp->wage = *(arr + 1);
                data.emplace_back(tmp);
            }
        }

        void printData() {
            for(auto info: data) {
                //cout << info->name << " earned: " << info->earned() << endl;
                printf("%s earned: %.2f\n", info->name.c_str(), info->earned());
            }
        }

        void addHours(double hours) {
            for(auto info: data) {
                info->hours += hours;
            }
        }

        void addWage(double wage) {
            for(auto info: data) {
                info->wage += wage;
            }
        }

        void addEmployee(const string& name, double wage, double hours) {
            Employee* tmp = new Employee;
            tmp->name = string(name);
            tmp->wage = wage;
            tmp->hours = hours;
            data.emplace_back(tmp);
        }

        Employee* getEmployeeByName(const string& name) {
            vector<Employee*>::iterator it = find_if(data.begin(), data.end(), [&](Employee* tmp) {
                return name.compare(tmp->name) == 0;
            });

            if(it != data.end()) {
                return *it;
            }

            return nullptr;
        }
};

int main() {
    double data[100] = {43.6, 20.50, 38.4, 24.0, 46.9, 20.50, 44.2, 22.0, 39.5, 50.0};
    char const *names[] = {"A", "B", "C", "D", "SOMETHING"};
    Money* obj = new Money(data, names, 10);
    obj->printData();
    obj->addWage(10);
    obj->addEmployee("Mkalo", 100, 100);
    Employee* a = obj->getEmployeeByName("Mkalo");
    if(a != nullptr) {
        printf("%s is gettin mad moneys: %.2f\n", a->name.c_str(), a->earned());
    }
    return 0;
}
(changed payment to employee)

Throwed some functions for ya.
That's solid. Nice man! I'll break it down and figure out how you made that now! :)
 
Back
Top