BAH! expected `;' before 'else'

This is a discussion on BAH! expected `;' before 'else' within the C++ Programming forums, part of the General Programming Boards category; Hi im new, and I bring you my woes for you to comfort me and brighten my day! The only ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    9

    BAH! expected `;' before 'else'

    Hi im new, and I bring you my woes for you to comfort me and brighten my day!

    The only errors I get when compiling my program are:
    pgm4b.cpp: In function 'int main()':
    pgm4b.cpp:43: error: expected primary-expression before 'else'
    pgm4b.cpp:43: error: expected `;' before 'else'

    And I know that the "expected ';' before 'else'" usually has to do with the ill-placement of an ';' after an if() condition before the commands, but nothing as such exists in my code, or atleast any I can find.

    The program is supposed to take in user input variables for a hotel

    What is the top floor?

    then a loop is started and repeats this questions in correlation to the # of floors

    how many rooms on that floor?
    how many are occupied

    Then the program should calculate
    How many rooms the hotel has
    how many are occupied
    how many are empty
    and the occupancy % of filled to empty rooms
    and the program should skip processing of floor six as the hotel doesn't a floor six or something,
    ALSO determine the heart Break floor which is the floor with the least amount of occupied rooms.

    Code:
    // pgm4b.cpp Kyle o'Brien cs201 1:30
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
                    { // opening block for main
    
            int tFloor, fRooms, oRooms;
            int tfRooms = 0;
            int toRooms = 0;
            int heartBreakF = 0;
            int heartBreakO = 0;
            int floor = 1;
            int empty;
            double occRate;
    
            cout << "What is the top floor? " << endl;
            cin >> tFloor;
    
            if (tFloor >= 1)
                            { // validation
                    while(floor <= tFloor)
                                    { // floor is acting counter
                            if (floor == 6)  // skipping floor 6
                                    floor++;
                            else // skipping floor 6
                    cout << "How many rooms on floor " << floor << " ?";
                    cout << endl;
                    cin >> fRooms;
                    cout << "How many of those are occupied? " << endl;
                    cin >> oRooms;
                            if(oRooms < oRooms)
                                            { // HeartBreak Checker
                            heartBreakO = oRooms;
                            heartBreakF = floor;
                          }
                    tfRooms = tfRooms + fRooms;
                    toRooms = toRooms + oRooms;
                    floor++;
                    } // loop closing
    
            else     // validation else option
    
            cout << "Enter a higher value for top Floor" << endl;
    
            } // validation closing
    
            occRate = toRooms / tfRooms;
            empty = tfRooms - toRooms;
            cout << fixed;
            cout << "The hotel has a total of " << tfRooms << " rooms";
            cout << endl;
            cout << toRooms << " are occupied" << endl;
            cout << empty << " are empty" << endl;
    cout << "The occupancy rate is " << setprecision(1) << occRate << "%" << endl;
    cout << "The heartbreak floor is: " << heartBreakF << " with only ";
    cout << heartBreakO << " occupied rooms." << endl;
    
    return 0;
    }
    I've tried just removing the else at line 43, and it compiles fine, but when I run it my occRate and heartBreakF and heartBreakO return as 0.

    Thanks again in advance!
    Last edited by Chef; 10-02-2008 at 03:03 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I think your braces don't match (they didn't count up correclty for me).

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    I counted four opening and four closing, which one do you see out of place?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    From the if(tFloor >=1) to the else, there are three opening and two closing (at least in what you posted).

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    Wow, thank you very much that took care of the compiling errors!
    But unfortunately the occRate and heartBreakF and heartBreakO still return 0...

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,170
    That's how good indentation helps . I can tell you are trying to indent, but your way is certainly not common, and even worse, it's inconsistent.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    Heh, I'm still relatively new to this "ORG-NIZA-TION" thing, its a wacky concept.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. When is oRooms < oRooms ever true?
    2. Integer division is not the same as floating-point division. If you need floating-point division, you can cast integers to float before dividing.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    I'm not looking for division, its to check and see if each loop the users enters a var for oRooms and to see if it is less than the previous oRooms entered. And if it is, store that oRooms literal in a separate variable called heartBreakO and take the floor# the loop is on and store that in the heartBreakF variable. Thats what it is supposed to do anyway.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    occRate = toRooms / tfRooms;
    So you don't think that's division, eh?

    So if you want to compare oRooms with heartBreakO, then why are you comparing oRooms with oRooms?

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    occRate = toRooms / tfRooms;
    Ok! That is meant to divide I was confused. So you want me to take the int variables and change them to float variables?

    And I'm comparing oRooms < oRooms to check and see if what the user entered in the previous loop is less than what they entered in this loop, and if true store the lowest oRooms value in heartBreakO and floor that the loop is on in heartBreakF.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Chef View Post
    Ok! That is meant to divide I was confused. So you want me to take the int variables and change them to float variables?
    No, you need to cast them to floating-point before the division.
    And I'm comparing oRooms < oRooms to check and see if what the user entered in the previous loop is less than what they entered in this loop, and if true store the lowest oRooms value in heartBreakO and floor that the loop is on in heartBreakF.
    1. If you want to store the lowest value, then comparing to the most recent value is just plain silly.
    2. It is not possible for you to think that oRooms < oRooms would "see if what the user entered in the previous loop is less than what they entered in this loop", for oRooms must obviously mean the same thing on both sides of the < sign.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    ...ok... your right.... and I fixed that, thank you once again for your grand knowledge.

    But I am still confused on floating-point, mainly because i've never dealt with it.
    type cast those varaibles above the occRate as floating-point varaibles through the type cast command?

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    Sorry for the frustration I caused in my glorious void of knowledge and logic. But I did get it to run successfully with your help. Thank you

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    This is what I get when I properly indent your code...
    Code:
    // pgm4b.cpp Kyle o'Brien cs201 1:30
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    { // opening block for main
    
    	int tFloor, fRooms, oRooms;
    	int tfRooms = 0;
    	int toRooms = 0;
    	int heartBreakF = 0;
    	int heartBreakO = 0;
    	int floor = 1;
    	int empty;
    	double occRate;
    
    	cout << "What is the top floor? " << endl;
    	cin >> tFloor;
    
    	if (tFloor >= 1)
    	{ // validation
    		while(floor <= tFloor)
    		{ // floor is acting counter
    			if (floor == 6)  // skipping floor 6
    				floor++;
    			else // skipping floor 6
    				cout << "How many rooms on floor " << floor << " ?";
    			cout << endl;
    			cin >> fRooms;
    			cout << "How many of those are occupied? " << endl;
    			cin >> oRooms;
    			if(oRooms < oRooms)
    			{ // HeartBreak Checker
    				heartBreakO = oRooms;
    				heartBreakF = floor;
    			}
    			tfRooms = tfRooms + fRooms;
    			toRooms = toRooms + oRooms;
    			floor++;
    		} // loop closing
    
    	else     // validation else option // Oops! Out of place!
    
    		cout << "Enter a higher value for top Floor" << endl;
    
    	} // validation closing
    
    	occRate = toRooms / tfRooms;
    	empty = tfRooms - toRooms;
    	cout << fixed;
    	cout << "The hotel has a total of " << tfRooms << " rooms";
    	cout << endl;
    	cout << toRooms << " are occupied" << endl;
    	cout << empty << " are empty" << endl;
    	cout << "The occupancy rate is " << setprecision(1) << occRate << "%" << endl;
    	cout << "The heartbreak floor is: " << heartBreakF << " with only ";
    	cout << heartBreakO << " occupied rooms." << endl;
    
    	return 0;
    }
    What IDE /editor do you use?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  2. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Windows using Dev-C++
    By Renegade in forum C++ Programming
    Replies: 15
    Last Post: 07-07-2005, 08:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21