illegal else without matching if

# include <iostream>
using namespace std;
int main()
{
int hc=50; // price for hair cut
int cp=500; // price for cellophane
int ho=150; // price for hot oil
int hs=250; // price for hair spa
int hr=1500;
int hd=250;
int ham=500;
int fs=80;
int mc=60;
int pc=80;
char Y,mem;
int tot=0;
cout<<"Did the customer avail the following services:(Y/N):"<< endl;cout << "total: " << tot<<endl;

cout<<"HairCut P 50.00: ";cin>>Y;
if (hc=='Y'){
tot=hc+tot;cin>>tot;}

cout<<"HotOil P 150.00: ";cin>>Y;
else if(ho=='Y'){
tot=ho+tot;cin>>ho;}


cout<<"HairSpa P 250.00: ";cin>>Y;
else if(hs=='Y'){
cin>>hs;
tot=hs+tot;}

cout<<"Cellophane P 500: ";cin>>Y;
else if(cp=='Y'){
tot=cp+tot;cin>>cp;}

cout<<"HairRebond P 1500.00: ";cin>>Y;
else if(hr=='Y'){
cin>>hr;
tot=hr+tot;}

cout<<"HairDye P 400.00: ";cin>>Y;
else if(hd=='Y'){
tot=hd+tot;cin>>hd;}

cout<<"Hair and Makeup P 500.00: ";cin>>Y;
else if(ham=='Y'){
tot=ham+tot;cin>>ham;}

cout<<"FootSpa P 80.00: ";cin>>Y;
else if(fs=='Y'){
tot=fs+tot;cin>>fs;}

cout<<"Manicure P 50.00: ";cin>>Y;
else if(mc=='Y'){
tot=mc+tot;cin>>mc;}

cout<<"Pedicure P 60.00: ";cin>>Y;
else if(pc=='Y'){
tot=pc+tot;cin>>pc;}

else {
cout<<"Aborted";
}
return 0;
}

what seems to be wrong???
You are using else in the wrong context, I've re-arranged your code slightly to explain....

1
2
3
4
5
6
7
8
9
10
11
12
cout<<"HairCut P 50.00: ";cin>>Y;
if (hc=='Y')
{
   tot=hc+tot;cin>>tot;
}

cout<<"HotOil P 150.00: ";cin>>Y;

else if(ho=='Y')
{
   tot=ho+tot;cin>>ho;
}


The else on line 9 is illegal because you have already closed the if code block and the code on line 7 separates the if from the else. It has to be something like this

1
2
3
4
5
6
7
8
9
10
11
12
cout<<"HairCut P 50.00: ";cin>>Y;
if (hc=='Y')
{
   tot=hc+tot;cin>>tot;
//}

   cout<<"HotOil P 150.00: ";cin>>Y;
}
else if(ho=='Y')
{
   tot=ho+tot;cin>>ho;
}


with no code between the if closing brace and the else

EDIT: This has already been answered by Grey Wolf. Try not to post the same question multiple times. If you do it accidentally then delete the duplicate(s)
Last edited on
sorry, how do i delete a post and if i moved or enclosed the last statement with brackets then my service will end with only one statement / services offered...ty!
Last edited on
You can't delete once the post has been replied to but otherwise click the delete button when you're logged in.

Remove the 'else' keyword, is doesn't look like you need them.
closed account (z05DSL3A)
I have added a link to this thread, from the other one.
Topic archived. No new replies allowed.