Hi, @New Programmer
There are several errors in the snippet that need to be corrected:
- Do not define another function in one function definition.
- There is no need to use const in istream& operator>>. For your reference: Overloading the >> and Overloading the <<
- Income in
ostream& operator<<(ostream& out, const IncomeCalc&income)
is a class, not a member. - Do not use uninitialized members, you should initialize them in the constructor before using them.
Below is the modified snippet for your reference:
#include <string>
#include<iostream>
using namespace std;
class IncomeCalc {
const float rate = 180.25;
public:
float income=0; // modified =0
float hours_per_week=0;//hours you worked in a week
friend
ostream& operator<<(ostream& out, const IncomeCalc& incomeCalc);
friend
istream& operator>>(istream& in, IncomeCalc& incomeCalc);
};
void weekly() {
cout << "Hello User!" << "Here you can calculate your weekly income" << endl;
cout << "To do so, please insert your weekly hours worked." << endl;
} //modified { }
istream& operator>>(istream & in, IncomeCalc & incomeCalc) { // removed const
in >> incomeCalc.hours_per_week; //modified incomeCalc.hours_per_week
return in;
}
ostream& operator<<(ostream & out, const IncomeCalc & incomeCalc) {
out << "Wow!" << " You have earned " << incomeCalc.hours_per_week << " * " << incomeCalc.rate << "Which is equal to :" << endl; //modified incomeCalc.
out << incomeCalc.income << endl;
return out;
}
int main() {
IncomeCalc calculate;
cout << "Bye" << endl;
return 0;
}
Best regards,
Minxin Yu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our [documentation][3] to enable e-mail notifications if you want to receive the related email notification for this thread.