basic c++ help


Please help!! My Program is outputting user's input twice on my table. Can't figure out why. It only output data last entered, it should output all input entered by user (e.g. Floor 1: 29,4,25, 86.20%) (Floor 2: 15,10,5, 33.33%)


It looks like this when its outputted:

Floor Room Available Occupied Occupancy Rate
------------------------------------------------------------------
1 15 10 5 33.33%

2 15 10 5 33.33%


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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    //Variable Definitions
    int Floors;
    double Rooms, occupied;
    char choice;
    int count = 1;

   //Retrieving Floors of Hotel from User
    cout << "\nFirst, tell us how many floors does the hotel have: " << endl;
    cin >> Floors;

            //Not accepting an answer floors less than 1
            if (Floors > 0) {

                //Using For loop to ask the user to enter the total number of rooms for each floor.
                for (; count <= Floors; count++) {

                    cout << "How many rooms does floor " <<count << " have: " << endl;
                    cin >> Rooms;


                    //User should enter Y or N on whether any rooms are occupied, if enter anything other than those
                    // letters, need to re-enter right choice.
                        cout << "Are any of the rooms occupied on this floor? (Enter Y for Yes or N for No): " << endl;
                    cin >> choice;

                        if (choice == 'Y') {
                            cout << "Enter amount of rooms occupied: " << endl;
                            cin >> occupied;
                        }

                            else if (choice != 'Y' && choice != 'N') {
                            cout << "Wrong Answer Choice, please answer with Y or N: " << endl;
                            cin >> choice;
                            cout << "Re-Enter amount of rooms occupied: " << endl;
                            cin >> occupied;
                            }

                        if (occupied > Rooms) {
                        cout << "Error: The amount occupied cant surpass total number of rooms on that floor. Please re-enter choice: ";
                        cin >> occupied;

                        }

                }

                //Formatting Table
                cout << setw(10) << "Floor" << setw(10) << "Room" << setw(10) << "\tAvailable"
                     << setw(10) << "Occupied" << setw(15)<< "\tOccupancy Rate " << setw(10)<< endl;
                cout << "    ------------------------------------------------------------------";

                //!!!Can't get this part to output data, repeating data.
                for (int x = 1; x <= Floors; x++) {

                    cout << "\n" << setw(10) << x << setw(10);
                    cout << Rooms << setw(10);
                    cout << Rooms - occupied << setw(10);
                    cout << occupied << setw(10);
                    cout << "\t" <<  (occupied / Rooms) * 100 << "%" << setw(10) << endl;
                }
            }


            //This is printed if user enter invalid floors from beginning
            else {
                    cout << "\nIncorrect: Please enter floor numbers more than 1: " << endl;
                    cin >> Floors;
            }


    return 0;

}




Every time the loop executes, you're overwriting what's currently stored in each variable. So let's say you're on floor one. You enter that there are 6 rooms(which goes to the variable "rooms"), 3 of which are occupied (which goes the the variable "occupied"). Now the loop starts over. Then you enter that for the second floor there are 9 rooms, 4 of which are occupied. Now rooms holds 9 and occupied holds 4 - the 6 and 3 that you entered are now gone. This happens every time until you reach the end of the loop, at which point the last data you entered is stored in those variables. That's why only the last input is being displayed. Have you learned about arrays yet? They would provide you with a simple solution to the problem.
Last edited on
Thank you!!
Topic archived. No new replies allowed.