Question:
At the beginning of every year, Khalid receives a raise on his previous year's salary. He wants a program that calculates and displays the amount of his annual raises for the next three years, using rates of 3%, 4%, 5%, and 6%. The program should end with Khalid enters a sentinel value as the salary.
Code: (I Made this)
#include<iostream>
using namespace std;
int main()
{
//Declare variables
double salary;
double rate;
while(1)
//Read salary
cout<<"\n\nEnter salary (-999 to stop) : ";
cin>>salary;
//If sentinal is entered break loop
if(salary==-999)
{
cout<<"Bye!\n";
break;
}
else
{
cout<<"\nAnual Raises for next three years : "<<endl;
//Intializing rate to 0.03 i.e 3% of salary
rate = 0.03;
while(rate<0.07)
{
//display annual raise in amount
cout<<rate*100<<" % raise : "<<salary*rate<<endl;
//increment salary by raise amount
salary = salary + (salary*rate);
//Increment rate by 1%
rate = rate + 0.01;
}
}
return 0;
} //end of main function
The break statement can only be used inside a loop or a switch statement. Your break statement is not within either. Remember that when you don't use braces only one statement will be inside the loop.
You need to use the braces to surround the body of the loop.
By the way your while loop really should have the termination condition instead of creating an "endless" loop then you wouldn't need the break statement.
1 2 3 4 5 6 7 8
cout << "\n\nEnter salary (-999 to stop) : ";
while(cin >> salary && salary != 999)
{
// The rest of the code you want in your loop.
// At the end of the loop body.
cout << "\n\nEnter salary (-999 to stop) : ";
}