Oct 16, 2016 at 5:22pm Oct 16, 2016 at 5:22pm UTC
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
Error:
1>------ Build started: Project: Intermediate18 Project, Configuration: Debug Win32 ------
1> Intermediate18.cpp
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(19): error C2043: illegal break
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Can someone help me fix this error.
Oct 16, 2016 at 5:26pm Oct 16, 2016 at 5:26pm UTC
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.
Oct 16, 2016 at 5:28pm Oct 16, 2016 at 5:28pm UTC
Where should I should put the braces at. I do not understand would I need to remove it or add something
Oct 16, 2016 at 6:18pm Oct 16, 2016 at 6:18pm UTC
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) : " ;
}
Also please use code tags when posting code.
Last edited on Oct 16, 2016 at 6:19pm Oct 16, 2016 at 6:19pm UTC
Oct 18, 2016 at 2:09am Oct 18, 2016 at 2:09am UTC
#include<iostream>
using namespace std;
int main()
{
//Declare variables
double salary;
double rate;
while(1)
{
//Read salary
cout<<"\n\nEnter salary (-999 to stop) : ";
while(cin >> salary && salary != 999)
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;
}
}
cout << "\n\nEnter salary (-999 to stop) : ";
}
return 0;
}
This is my new code and I keep getting errors
1>------ Build started: Project: Intermediate18 Project, Configuration: Debug Win32 ------
1> Intermediate18.cpp
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(5): error C3872: '0xa0': this character is not allowed in an identifier
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(6): error C3872: '0xa0': this character is not allowed in an identifier
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(6): error C2065: ' ' : undeclared identifier
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(6): error C2146: syntax error : missing ';' before identifier ' '
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(6): error C2144: syntax error : 'double' should be preceded by ';'
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(7): error C3872: '0xa0': this character is not allowed in an identifier
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(7): error C2065: ' ' : undeclared identifier
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(7): error C2144: syntax error : 'double' should be preceded by ';'
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(31): error C3872: '0xa0': this character is not allowed in an identifier
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(31): error C2065: ' ' : undeclared identifier
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(31): error C2146: syntax error : missing ';' before identifier ' '
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(31): error C2065: ' ' : undeclared identifier
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(33): error C2065: ' ' : undeclared identifier
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(33): error C2146: syntax error : missing ';' before identifier ' '
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(33): error C2146: syntax error : missing ';' before identifier 'salary'
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(34): error C3872: '0xa0': this character is not allowed in an identifier
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(34): error C2065: ' ' : undeclared identifier
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(34): error C2146: syntax error : missing ';' before identifier ' '
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(34): error C2065: ' ' : undeclared identifier
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(36): error C2065: ' ' : undeclared identifier
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(36): error C2146: syntax error : missing ';' before identifier ' '
1>c:\cpp8\chap 08\intermediate18 project\intermediate18 project\intermediate18.cpp(36): error C2146: syntax error : missing ';' before identifier 'rate'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========