sunnysmile24
sunnysmile24

Reputation: 13

C++ expected primary-expression before ';' token

I get an error message that states "expected primary-expression ';' token" and it highlights my percentage formula. I have tried to rearrange my code, but it seems the problem is not in it.

const int TOTALSUITES = 120;

for (int floor = 10; floor <= 16; floor++) {
    if ( floor != 13) {
        do { 
            cout << "Please enter the number of suites occupied on the floor " << floor << ":";
            cin >> numOccupied; 

            if ((numOccupied <0) || (numOccupied >20 )) {  
                goodChoice = false;
                cout << "\n\t\t**ERROR" << numOccupied << " ***\n\n";
                cout << "*** Choice must be from [0-20]***\n";
            }
            else {
                goodChoice = true;
            }
            totalOccupied += numOccupied; 
        } while (!goodChoice);
    }
 }

percentage = (totalOccupied / TOTALSUITES) * 100% ;
cout << endl;
cout << "Hotel has " << TOTALSUITES << endl;
cout << "Number of occupied suites is " << totalOccupied << endl;
cout << "The percentage of occupied suites is " << percentage << endl;
system("pause");
return 0;

Upvotes: 0

Views: 1640

Answers (4)

Shreyan Mehta
Shreyan Mehta

Reputation: 550

% used here is the modulo operator which is a binary operator... so this is what you have to do...

percentage = (totalOccupied / TOTALSUITES)* 100;

//and then where you have cout percentage at that point...do this

cout<<"the percentage of occupied suites is"<<percentage<<"%";

Upvotes: 4

Bathsheba
Bathsheba

Reputation: 234875

% is actually the modulus operator in C++, requiring two arguments.

100% is therefore not syntactically valid.

Assuming that you want % to stand-in for a "divide by 100" operator, the simplest thing to do in your case is to drop the 100% from your code.

Note that totalOccupied / TOTALSUITES will be carried out in integer arithmetic if totalOccupied is also an int or unsigned. Fix that by promoting one of the arguments to a double, or pre-multiply the term with 1.0.

Upvotes: 1

user2209008
user2209008

Reputation:

percentage = (totalOccupied / TOTALSUITES) * 100% ;

This is not valid syntax. Change it to this.

percentage = (totalOccupied / TOTALSUITES);

Assuming your totalOccupied is not a float, you should probably do this as well:

percentage = (static_cast<float>(totalOccupied) / TOTALSUITES);

Upvotes: 1

NathanOliver
NathanOliver

Reputation: 181027

100% is not 100 percent. 100% is trying to use the % operator in an incorrect manner. To multiply by 100% you just use 1 which is not needed as anything times 1 is itself.

Upvotes: 1

Related Questions