Else error. Expected a statement

The else statement for "the customer does not have a discount coupon is underlined as incorrect. There is a missing or extra brace somewhere, but I can't find it. Also, keep in my that my program is unfinished.

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <locale>

using namespace std;

int main()

{
//declare variables

const double DISCOUNT = 0.25;
double discountQues = 0.0;
double extraTic = 0.0;
const double JUNIOR_TIC = 7.50;
double juniorTicTotal = 0.0;
const double MAX_SEAT_COUNT = 2200.0;
double numTic = 0.0;
const double SENIOR_TIC = 11.50;
double seniorTicTotal = 0.0;
double ticType = 0.0;
const double TODDLER_TIC = 0.0;
double toddlerTicTotal = 0.0;
double totalCost = 0.0;
double totalNumTic = 0.0;
double totalRevenue = 0.0;

//welcome the user and explain the purpose of the software
int consoleWidth = 50;
cout << setw(consoleWidth / 2) << " " << " PRIOLEAU PROGRAMMING, INC " << endl;
cout << setw(consoleWidth / 2) << " " << "SUMMERVILLE SEAWORLD TICKETING SOFTWARE 1.0" << endl << endl;
cout << "This program is designed to control, manage and track "
"ticket sales for Summerville Seaworld. Please input information when "
"you are prompted. Enjoy!" << endl << endl;

//choose a type of ticket
cout << "Please select from the following types of tickets: " << endl << endl;
cout << "TICKET TYPE AGE RANGE COST" << endl;
cout << "Toddler 0-5 years Free" << endl;
cout << "Junior 6-16 years $7.50" << endl;
cout << "Senior 17 and up $11.50" << endl;
cout << endl << endl;
cout << "Which type of ticket would you like to purchase? (1, 2, 3 or 4): " << endl;
cin >> ticType;
cout << endl;

//ticket purchase while loop
while (ticType == 1 || ticType == 2 || ticType == 3)
{//while the ticket type equals 1, 2 or 3

if (ticType == 1)
{//if purchase toddler ticket
cout << "You have selected to purchase a Toddler ticket. "
"How many Toddler tickets would you like to buy?" << endl << endl;
cout << "Please enter the number of tickets (1-10): " << endl << endl;
cin >> numTic;
}
if (numTic > 0 && numTic <= 10)
{//if number of toddler tickets is between 1-10
cout << "Do you have a discount coupon today?" << endl;
cout << "Enter 1 for YES or 2 for NO: " << endl;
cin >> discountQues;
}
if (discountQues == 1)
{//if customer has a discount coupon
totalCost = (TODDLER_TIC * numTic) - (TODDLER_TIC * numTic * DISCOUNT);
totalNumTic += numTic;
totalRevenue += totalCost;
}
if (totalNumTic >= MAX_SEAT_COUNT)
{//if the total number of tickets equals the maximum seat count
extraTic = totalNumTic - MAX_SEAT_COUNT;
cout << "SEAT CAPACITY EXTRAS TOTAL SOLD" << endl;
cout << MAX_SEAT_COUNT << extraTic << totalNumTic;
cout << "TODDLERS JUNIORS SENIORS";
cout << toddlerTicTotal << juniorTicTotal << seniorTicTotal;
cout << "GROSS FOR TODAY: $" << totalRevenue << endl << endl;
}
else
{//else the total number of tickets does not equal the maximum seat count
cout << "Type of ticket(s): Toddler Ticket(s)"<< endl;
cout << "Number of ticket(s):" << numTic << endl;
cout << "Total Cost: " <<totalCost << endl;
}
else
{//else the customer does not have a discount coupon
totalCost = TODDLER_TIC * numTic;
totalNumTic += numTic;
totalRevenue += totalCost;
}
else
{//else the number of tickets is not between 1-10
cout << "The maximum number of tickets that can be sold "
" is 10. Please enter a number between 1-10." << endl;
cin >> numTic;
}
else if (ticType == 2)
{
}

else if (ticType == 3)
{
}// end if
}//end while


system("pause");
return 0;
} //end of main function
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
if (totalNumTic >= MAX_SEAT_COUNT)
{//if the total number of tickets equals the maximum seat count
extraTic = totalNumTic - MAX_SEAT_COUNT;
cout << "SEAT CAPACITY EXTRAS TOTAL SOLD" << endl;
cout << MAX_SEAT_COUNT << extraTic << totalNumTic;
cout << "TODDLERS JUNIORS SENIORS";
cout << toddlerTicTotal << juniorTicTotal << seniorTicTotal;
cout << "GROSS FOR TODAY: $" << totalRevenue << endl << endl;
}
else
{//else the total number of tickets does not equal the maximum seat count
cout << "Type of ticket(s): Toddler Ticket(s)"<< endl;
cout << "Number of ticket(s):" << numTic << endl;
cout << "Total Cost: " <<totalCost << endl;
}
else
{//else the customer does not have a discount coupon
totalCost = TODDLER_TIC * numTic;
totalNumTic += numTic;
totalRevenue += totalCost;
}
else
{//else the number of tickets is not between 1-10
cout << "The maximum number of tickets that can be sold "
" is 10. Please enter a number between 1-10." << endl;
cin >> numTic;
}


You have else statements without the if statements to accompany them. Try else if statements.
I don't see a missing brace, but I do see a missing if. You have an else followed by an else, each else must have an if statement.

Where does the missing if need to be? I have several ifs to go into before I can begin else statements. Don't have have to go into all of the ifs first? I wish I could post my flowchart.
Every else must have a corresponding if in the block directly above the else.
1
2
3
4
5
6
7
8
if(condition)
{
   // Some statements.
}
else
{
   // Some statements.
}
To expand on jlb post:


1
2
3
4
5
6
7
8
9
if(condition) {
   // Some statements.
}
else if (condition) {
   // Some statements.
}
else {
   // Some statements.
}
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
if (ticType == 1)
{//if purchase toddler ticket
    ...
}
if (numTic > 0 && numTic <= 10)
{//if number of toddler tickets is between 1-10
    ...
}
if (discountQues == 1)
{//if customer has a discount coupon
    ...
}
if (totalNumTic >= MAX_SEAT_COUNT)
{//if the total number of tickets equals the maximum seat count
    ...
}
else
{//else the total number of tickets does not equal the maximum seat count
    ...
}
else
{//else the customer does not have a discount coupon
    ...
}
else
{//else the number of tickets is not between 1-10
    ...
}
else if (ticType == 2)
{
}


I think what you are trying to do is nest the if statements. You've got your closing braces at the end of your if statements in the wrong places. What you want is:

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
if (ticType == 1)
{//if purchase toddler ticket
    ...
    if (numTic > 0 && numTic <= 10)
    {//if number of toddler tickets is between 1-10
        ...
        if (discountQues == 1)
        {//if customer has a discount coupon
            ...
            if (totalNumTic >= MAX_SEAT_COUNT)
            {//if the total number of tickets equals the maximum seat count
                ...
            }
            else
            {//else the total number of tickets does not equal the
              //maximum seat count
                ...
            }
        }
        else
        {//else the customer does not have a discount coupon
            ...
        }
    }
    else
    {//else the number of tickets is not between 1-10
        ...
    }
}
else if (ticType == 2)
{
}


Notice each successive if statement is contained within the previous if statement code block.
Last edited on
Topic archived. No new replies allowed.