else without a previous if?

Pages: 12
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.

Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  #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 << "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;

}
Last edited on
Your problem is here -

1
2
3
4
5
else
{
 (SALARY = AGE * SMWO) 
cout << "Salary for this position is $ "<<SALARY<<".\n";
}	


You want to use else if instead of "else"

1
2
3
4
else if (SALARY = AGE * SMWO) 
{
      cout << "Salary for this position is $ "<<SALARY<<".\n";
}	
Last edited on
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
}
Here's how the compiler parses the code:

1
2
3
4
5
6
7
8
9
10
11
12
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
else if // 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 
}
Last edited on
I see 2 problems here.


1
2
3
4
5
if(ANS == 'Y' || ANS 'y') SALARY = AGE * SMWD; setprecision(2) ;fixed;
{
cout << "Salary for this position is $ "<<SALARY<<".\n";

}


You say ANS == 'Y' || ANS 'y'

But what does <ANS 'y'> Mean? You dont tell it anything logical. Perhaps you mean

 
if(ANS == 'Y' || ANS == 'y')


Second problem. The parameter should be directly after "else if" and not inside the brackets.

 
else if (SALARY = AGE * SMWO) 



Please watch Bucky's (Youtube channel : thenewboston) Tutorials. They are great for the basics.

Here is a link - https://www.youtube.com/playlist?list=PLAE85DE8440AA6B83

If you want to learn about if, else if and else statements. watch video 8, 16, and 17.
Last edited on
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
@grammec70
Segment of code from your post
1
2
3
4
5
6
7
8
9
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";
}



return 0;

}
Could you please post it in code mode?
what does code mode mean?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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";
}



return 0;

}
The way you put your code the first time, between 2 code blocks like this

 
    // code goes here 


its to the right under format.
I'm sorry I don't understand?
Just specifiy the function like this

1
2
3
4
5
else if (ANS == 'N' || ANS == 'n')
	{
		SALARY = AGE * SMWO;
		cout << "Salary for this position is $ " << SALARY << ".\n";
	}


And that will fix the problem.
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?
If you change

1
2
3
4
5
6
else
{
SALARY = AGE * SMWO;
cout << "Salary for this position is $ "<<SALARY<<".\n";
}
 


to

1
2
3
4
5
else if (ANS == 'N' || ANS == 'n')
	{
		SALARY = AGE * SMWO;
		cout << "Salary for this position is $ " << SALARY << ".\n";
	}


Then anyone below 18 will not get a salary. Ive tried it.
This is with the change you suggested and it still comes up with the same thing!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#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
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"; 
}

 else if (ANS == 'N' || ANS =='n')
{
  
    SALARY = AGE * SMWO;
    cout << setprecision(2) << fixed;
    cout << endl;
    cout << "Salary for this position is $ "<<SALARY<<".\n";
}
 
   

		 

return 0;

}
Ok I got that gyazo and look at what I see;
I don't know if I did this link thing right or not.
Pages: 12