I'm new at C++ and I am getting the error: 'else' without a previous 'if' I have been reading other things on here about this and using this example:
if(boolean_expression)
{
// statement(s) will execute if the boolean expression is true
}
else
{
// statement(s) will execute if the boolean expression is false
}
I have checked everything but must be missing something can you help?
Thank you sure appreciate anything anyone can do to help me with this. I have been about 4 days working on this reading and making attempts.
#include <iostream>
usingnamespace std;
#include <iomanip>
#include <cstdlib>
#define ADULT 18 //AGE (integer)
#define SMWD 1000.0 //SALARY (float) Multiplier with degree
#define SMWO 600.0 //SALARY (float Multiplier without degree
int AGE; //Testing for SALARY
int YRS; //Years to wait before re-applying
char ANS; //Testing for Y or y
double SALARY; //SALARY in dollars & cent to offer qualified applicants
int main()
{
system("cls");
/* Intro. & Instructions */
cout << "Screening Process for Position\n";
cout << "Written by Donna Ehler - 17 February 2015\n\n";
cout << "This program will request information in order to evaluate qualifications\n";
cout << "for the position being offered. Then display the results.\n";
cout << "How old are you? ";
cin >> (AGE);
cout << endl;
if ( AGE>=ADULT )
{
cout << "Do you have a college degree? Y/N"<< endl;
cin >> ANS;
}
else
{
cout << "Please come back in " << ADULT - AGE << " to re-apply. \n";
}
if(ANS == 'Y' || ANS 'y') SALARY = AGE * SMWD; setprecision(2) ;fixed;
{
cout << "Salary for this position is $ "<<SALARY<<".\n"; // statement(s) will execute if the boolean expression is true
}
else
{
(SALARY = AGE * SMWO)
cout << "Salary for this position is $ "<<SALARY<<".\n";// statement(s) will execute if the boolean expression is false
}
return 0;
}
Thank you for your fast reply I did what you said and still get and error:
45 1 J:\College\Cop2000\PortableApps\HW assignments\A2\A2.cpp [Error] 'else' without a previous 'if'
46 1 J:\College\Cop2000\PortableApps\HW assignments\A2\A2.cpp [Error] expected '(' before '{' token
Here is where I changed the code:
if(ANS == 'Y' || ANS 'y') SALARY = AGE * SMWD; setprecision(2) ;fixed;
{
cout << "Salary for this position is $ "<<SALARY<<".\n"; // statement(s) will execute if the boolean expression is true
}
else if
{
(SALARY = AGE * SMWO)
cout << "Salary for this position is $ "<<SALARY<<".\n";// statement(s) will execute if the boolean expression is false
}
if(ANS == 'Y' || ANS 'y') SALARY = AGE * SMWD; //end of if statements
setprecision(2) ; //new statement
fixed; //next statement
{
cout << "Salary for this position is $ "<<SALARY<<".\n"; // statement(s) will execute if the boolean expression is true
}
// above code block is unrelated to the if(. . .) condition
elseif // not associated due to intervening code, don't need the if (no condition anyway)
{
(SALARY = AGE * SMWO) // what's missing here?
cout << "Salary for this position is $ "<<SALARY<<".\n";// statement(s) will execute if the boolean expression is false
}
Thank you so much. I am going right now and watch these videos Yes, I have problems in there and want to LEARN how NOT to continue to make them. Appreciate your input. Have a great day.
Donna
if(ANS == 'Y' || ANS 'y') SALARY = AGE * SMWD; setprecision(2) ;fixed;
{
cout << "Salary for this position is $ "<<SALARY<<".\n"; // statement(s) will execute if the boolean expression is true
}
else
{
(SALARY = AGE * SMWO)
cout << "Salary for this position is $ "<<SALARY<<".\n";// statement(s) will execute if the boolean expression is false
}
Code with fixes explained
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//two problems fixed on the following line...
//1. ANS 'y' changed to ANS == 'y'
//2. Two extra statements moved to the inside of the if block (your main problem)
if(ANS == 'Y' || ANS 'y')
{
SALARY = AGE * SMWD;
//this is the proper way to tell cout to setprecision and use fixed
cout << setprecision(2) << fixed;
cout << "Salary for this position is $ "<<SALARY<<".\n";
}
else
{
//removed parentheses, added semicolon
SALARY = AGE * SMWO;
cout << "Salary for this position is $ "<<SALARY<<".\n";
}
Thank you so much for all your help. I don't see my last message here but I still had a few issues. But with all your help I GOT IT! Can't belive it. I am a stroke survivor and my brain doesn't seem to like BRACES but I think I can understand them better now.
Appreciate all your help. God Bless have a great night.
Donna
Well, guess I got excited too quickly...need another question answered:
When the age of 16 is put in there should be NO SALARY because it needs to be 18 to qualify for the position.
When I run the code it says to come back in 2 yrs which is correct but below that it says salary is $9,800 which is the multiper for age * salary but I don't want any salary. Where do I stop that from happening?
Thanks code below:
#include <iostream>
using namespace std;
#include <iomanip>
#include <cstdlib>
#define ADULT 18 //AGE (integer)
#define SMWD 1000.0 //SALARY (float) Multiplier with degree
#define SMWO 600.0 //SALARY (float Multiplier without degree
int AGE; //Testing for SALARY
int YRS; //Years to wait before re-applying
char ANS; //Testing for Y or y
double SALARY; //SALARY in dollars & cent to offer qualified applicants
int main()
{
system("cls");
/* Intro. & Instructions */
cout << endl;
cout << "Screening Process for Position\n";
cout << endl;
cout << "Written by Donna Ehler - 17 February 2015\n\n";
cout << "This program will request information in order to evaluate qualifications\n";
cout << "for the position being offered. Then display the results.\n\n";
cout << "How old are you? ";
cin >> (AGE);
cout << endl;
if ( AGE>=ADULT )
{
cout << "Do you have a college degree? Y/N"<< endl;
cin >> ANS;
}
else
{
cout << "Please come back in " << ADULT - AGE << " to re-apply. \n\n";
}
if(ANS == 'Y' || ANS == 'y')
{
SALARY = AGE * SMWD;
cout <<setprecision(2) <<fixed;
cout << "Salary for this position is $ "<<SALARY<<".\n";
}
else
{
SALARY = AGE * SMWO;
cout << "Salary for this position is $ "<<SALARY<<".\n";
}
#include <iostream>
usingnamespace std;
#include <iomanip>
#include <cstdlib>
#define ADULT 18 //AGE (integer)
#define SMWD 1000.0 //SALARY (float) Multiplier with degree
#define SMWO 600.0 //SALARY (float Multiplier without degree
int AGE; //Testing for SALARY
int YRS; //Years to wait before re-applying
char ANS; //Testing for Y or y
double SALARY; //SALARY in dollars & cent to offer qualified applicants
int main()
{
system("cls");
/* Intro. & Instructions */
cout << endl;
cout << "Screening Process for Position\n";
cout << endl;
cout << "Written by Donna Ehler - 17 February 2015\n\n";
cout << "This program will request information in order to evaluate qualifications\n";
cout << "for the position being offered. Then display the results.\n\n";
cout << "How old are you? ";
cin >> (AGE);
cout << endl;
if ( AGE>=ADULT )
{
cout << "Do you have a college degree? Y/N"<< endl;
cin >> ANS;
}
else
{
cout << "Please come back in " << ADULT - AGE << " to re-apply. \n\n";
}
if(ANS == 'Y' || ANS == 'y')
{
SALARY = AGE * SMWD;
cout <<setprecision(2) <<fixed;
cout << "Salary for this position is $ "<<SALARY<<".\n";
}
else
{
SALARY = AGE * SMWO;
cout << "Salary for this position is $ "<<SALARY<<".\n";
}
return 0;
}
I don't want any salary for a 16 yrs old. When I run the code it says Please come back in 2 years to re-apply.
Then below that is says " Salary for this position is $9,600. I don't want any salary. How can I do that?
#include <iostream>
usingnamespace std;
#include <iomanip>
#include <cstdlib>
#define ADULT 18 //AGE (integer)
#define SMWD 1000.0 //SALARY (float) Multiplier with degree
#define SMWO 600.0 //SALARY (float Multiplier without degree
int AGE; //Testing for SALARY
char ANS; //Testing for Y or y
double SALARY; //SALARY in dollars & cent to offer qualified applicants
int main()
{
system("cls");
/* Intro. & Instructions */
cout << endl;
cout << "Screening Process for Position\n";
cout << endl;
cout << "Written by Donna Ehler - 17 February 2015\n\n";
cout << "This program will request information in order to evaluate qualifications\n";
cout << "for the position being offered. Then display the results.\n\n";
cout << "How old are you? ";
cin >> (AGE);
cout << endl;
if ( AGE>=ADULT )
{
cout << "Do you have a college degree? Y/N " << endl;
cout << endl;
cin >> ANS;
}
else
{
cout << "Please come back in " << ADULT - AGE << " to re-apply. \n\n";
}
if(ANS == 'Y' || ANS == 'y')
{
SALARY = AGE * SMWD;
cout << setprecision(2) << fixed;
cout << endl;
cout << "Salary for this position is $ "<<SALARY<<".\n";
}
elseif (ANS == 'N' || ANS =='n')
{
SALARY = AGE * SMWO;
cout << setprecision(2) << fixed;
cout << endl;
cout << "Salary for this position is $ "<<SALARY<<".\n";
}
return 0;
}