If / else if loop

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{
//calculates a person's regular pay as well as overtime (with user's input)

double regularPay, regularHours, overtimeHours, overtimePay, weekOvertime, weekPay, overallWeek;
bool answer;

cout<<"How much do you make per hours?"<<endl;
cin>>regularPay;

cout<<"How many regular hours did you work this week?"<<endl;
cin>>regularHours;

weekPay = regularPay * regularHours;

cout<<"You've earned $ "<<weekPay<<" this week. Do you have any overtime?"<<endl;
cin>>answer;

if (answer = "yes")
{
cout<<"How much do you get per overtime hour?"<<endl;
cin>>overtimePay;

cout<<"How many overtime hours did you work?"<<endl;
cin>>overtimeHours;

weekOvertime = overtimePay * overtimeHours;
overallWeek = weekPay + weekOvertime;

cout<<"You have $"<<weekOvertime<<" in overtime pay. So in total, you've made $"<<overallWeek<<" . Good work!"<<endl;
}
else if
{
cout<<"That's ok. Good job on making $"<<weekPay<<endl;
}
system ("pause");
return 0;
}



I'm not sure why my loop isn't running right. I've also used if/if and if/else . Nothing worked
on 1st hand, i noticed the following:
1. You are declaring answer as bool and then you are comparing value of answer with "yes" that is a string.
2. change if (answer = ...) to if (answer == ...). = is a statement, == is the comparison operator.
3. your else if does not have any condition..??
Please use code tags next time (use the <> button).

if (answer = "yes")
Use the == operator for comparison.

1
2
3
}
else if
{

Leave out the "if" when there is no condition to check.
Topic archived. No new replies allowed.