0

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;

4 Answers4

4

% 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<<"%";
Shreyan Mehta
  • 550
  • 5
  • 17
  • "%" symbol i have used in cout statement is just to display the percentage sign and has no logical effect on code – Shreyan Mehta Mar 09 '16 at 17:34
  • @SunnySmile24 if my answer or any else answer u found appropriate then mark as most appropriate i.e. the tick below the upvote and downvote buttons...so that other people knew that its answer has been already answered – Shreyan Mehta Mar 12 '16 at 07:12
1

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.

Community
  • 1
  • 1
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
1
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);
Colin Basnett
  • 4,052
  • 2
  • 30
  • 49
1

% 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.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483