Expected Primary Expression Before "Else" Error Please Help

This is my code so far and in my else if statements its sayin "Expected Primary Expression Before "Else"

What do i do?

//Assignment 10 program 3
#include <iostream>
using namespace std;
int main ()
{
//declare variable
double n,a,b,c,d,total;
a=3.50;
b=4.00;
c=4.50;
d=5.00;
total=20;
//get users input
cout << "Enter the Amount of Money You Have (Dollars)"<<endl<<endl;
cin >> n;
cout <<""<<endl;
cout <<"You Have "<<n; cout << " Dollars To Spend"<<endl;
cout <<""<<endl;
cout <<"You Have Four Choices of Candy"<<endl<<endl;
cout <<"1 Skittles $3.50"<<endl<<endl;
cout <<"2 M&Ms $4.00 "<<endl<<endl;
cout <<"3 Life Savour $4.50"<<endl<<endl;
cout <<"4 Smarties $5.00"<<endl<<endl;
//Items in the vending machine and the codes
cout <<"Please Select An Item From The Vending Machine By Insert The Code."<<endl<<endl;
cout <<"1 Skittles"<<endl<<endl;
cout <<"2 M&Ms"<<endl<<endl;
cout <<"3 Life Savour"<<endl<<endl;
cout <<"4 Smarties" <<endl<<endl;
//Get the user's option again
(n,cin);
cin >> n;
cout <<endl;
//If statements
if (n=a) {
cout <<"You have selected: Skittles"<<endl<<endl;
total=total-a;
cout <<"Your New Total Is: "<<total<<endl<<endl;
}
//Get the user's option again
(n,cin);
cin >> n;
cout <<endl;
//Else If Statements
else if(n==b) {
cout <<"You have selected: M&Ms"<<endl<<endl;
total=total-b;
cout <<"Your New Total is:"<<total<<endl<<endl;
}
//Get the user's option again
(n,cin);
cin >> n;
cout <<endl;
else if (n=c); {
cout <<"You have selected Life Savour"<<endl<<endl;
total1=total-c;
cout <<"Your New Total Is:"<<total<<endl<<endl;
}
//Get the user's option again
(n,cin);
cin >> n;
cout <<endl;
else if (n=d); {
cout <<"You have selected Smarties"<<endl<<endl;
total=total-d;
cout <<"Your New Total Is: "<<total<<endl<<endl;
}
system ("pause");
return 0;
}
An else clause must follow an un-terminated if statement.

1
2
3
4
5
6
7
8
if (abc == xyz)
{
  do_stuff();
}
else
{
  do_something_else();
}

(You've got something like:

1
2
3
4
5
if (abc == xyz)
  do_stuff();

else
  do_something_else();

<-- end of statement

<-- new statement expected here, but 'else' found


BTW, use [code] tags. People are unlikely to read/help when there is unformatted code dumped on the screen.

Hope this helps.
Last edited on
Post code in the code tag so that it is clear for us to understand.
If you already know the error post the line number for which the error is thrown.

Quickly looking at the code though what is (n,cin);? Doesnt look like proper C++

Also, if (n=c); and if (n=d); should be if (n==c); and if (n==d);?
The $ amount the user enters in n is getting overwritten by whatever choice they make for candy, which is also stored in variable n. Should the total variable come into play here? You initialize that to 20, but it looks like it's keeping track of how much money is left after a candy selection is made.

Also, the user is probably expecting to enter a 1,2,3 or 4 to choose from the candy menu, but the code is checking if they entered a,b,c or d.
Topic archived. No new replies allowed.