I am having trouble getting my if and else statement to work. The program is a simple payroll calculator but when I try to put an else statement after my if statement the compiler tells me that it cant find an if statement to accompany the else statement. So I tried making 2 if statements with the appropriate conditions (or so I think) and the program prints out the information from both if statements. I could really use some help figuring this out. (FYI I use Visual Studio for my compiler if that makes any difference)
#include "stdafx.h"
#include <iostream>
using std::cout;using std::endl;
using std::cin;
int main()
{
int n, etc, hw, hp, rp = 0, ot, otp, tp;
cout<<"Enter the Employees Hourly Pay"<<endl;
cin>>hp;
while (hp<7.15 || hp>31)
{
cout<<"Amount entered exceeds company pay scale."<<endl;
cout<<"Enter the Employees Hourly Pay"<<endl;
cin>>hp;
} //end while
cout<<"Enter the number of hours worked for the week."<<endl;
cin>>hw;
while (hw<.01 || hw>80)
{
cout<<"Amount entered exceeds company work hour standards"<<endl;
cout<<"Enter the number of hours worked for the week"<<endl;
cin>>hw;
} //end while
if (hw > 40)
rp= 40 * hp; // regular pay = 40 hours x hourly pay
ot= hw - 40; //overtime = hours worked - 40
otp= ot * (hp * 1.5); //overtime pay = overtime x (hourly pay x 1.5)
tp= otp + rp; //total pay = overtime pay + regular pay
cout<<"Hours Worked"<<endl;
cout<<hw<<endl;
cout<<endl;
cout<<"Regular Pay"<<endl;
cout<<rp<<endl;
cout<<endl;
cout<<"Overtime Pay"<<endl;
cout<<otp<<endl;
cout<<endl;
cout<<"Total Pay"<<endl;
cout<<tp<<endl;
if (hw <= 40)
rp= hw * hp; // regular pay = 40 hours x hourly pay
tp=rp; //total pay = regular pay
cout<<"Hours Worked"<<endl;
cout<<hw<<endl;
cout<<endl;
cout<<"Regular Pay"<<endl;
cout<<rp<<endl;
cout<<"Total Pay"<<endl;
cout<<tp<<endl;
cout<<endl;
cout<< "To calculate another payroll enter the Type Code"<<endl;
cout<<"Otherwise type -1 press enter and then type any character to exit program"<< endl;
cin>> etc;
cin >> n;
}
You need to put braces { } around the block of statements to be controlled by the if/else. Without them, only the very first statement after the if are dependent upon it.