Programming Ending without meeting both criteria

Jan 17, 2019 at 3:18am
So, I have this assignment and basically, I need to run the program until all seats are sold out of both halls. However, the program stops after one hall sells out, and doesn't move to the next hall. This is my code. (Everything else works the way it should)

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
  #include <iostream>
using namespace std;

int main(){
    
    int hallSeats = 300;
    int mezzSeats = 100;
    int childeren;
    int adults;
    int choice;
    double totalCost;
    double adultCost;
    double childCost;
    
    
    while(hallSeats!=0 && mezzSeats!=0){
        
        cout << "Enter 1 for Hall and 2 for Mezzanine" << endl;
        cin >> choice;
        
        if(choice == 1){
            
            cout << "Enter number of adults in the party: ";
            cin >> adults;
            
            cout << "Enter number of childeren in the party: ";
            cin >> childeren;
            
            if(adults + childeren > hallSeats)
                
                cout << "Sorry, there aren't enough seats available."<<endl;
            
            else {
                
                hallSeats-= childeren + adults;
                
                totalCost = (10 * adults ) + (7 * childeren);
                adultCost = 10 * adults;
                childCost = 7 * childeren;
                
                cout << "Great, you got " << adults << " tickets for $" << adultCost << " and " << childeren << " child tickets for $" << childCost << " so your total is $" << totalCost << endl;
            }
            
            cout << "Remaining Hall seats: " << hallSeats << endl;
            
            cout << "Remaining Mezzanine seats: " << mezzSeats <<endl;
            
        } else if(choice == 2){
            
            cout << "Enter number of adults in the party: ";
            cin >> adults;
            
            cout << "Enter number of childeren in the party: ";
            cin >> childeren;
            
            if(adults + childeren > hallSeats)
                
                cout << "Sorry, there aren't enough seats available."<<endl;
            
            else {
                
                mezzSeats-= childeren + adults;
                
                totalCost = (8 * adults ) + (5 * childeren);
                adultCost = 8 * adults;
                childCost = 5 * childeren;
                
                cout << "Great, you got " << adults << " tickets for $" << adultCost << " and " << childeren << " child tickets for $" << childCost << " so your total is $" << totalCost << endl;
            }
            
            cout << "Remaining seats: Hall " << hallSeats << " Mezzanine " << mezzSeats << endl;
            
            
        }
        
    }
    
}


Thanks in advance!
Jan 17, 2019 at 3:24am
On line 58 you're checking the wrong variable for seats.

I don't see how the problem you're seeing could happen. Post a sample input that reproduces it.
Last edited on Jan 17, 2019 at 4:31am
Jan 17, 2019 at 3:33am
Line 16 needs to be || instead of &&.
It currently says "keep going while both are not 0", so if either one becomes zero, it quits.
You want to say "keep going while either is not 0".
Jan 17, 2019 at 3:35am
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
#include <iostream>
using namespace std;

int main() {

    int hallSeats = 300;
    int mezzSeats = 100;

    while( hallSeats!=0 || mezzSeats!=0 ) { // note: || (at least one is non-zero)

        int choice;

        if( hallSeats!=0 && mezzSeats!=0 ) { // note: && (both are non-zero)

            // accept choice from user
            cout << "Enter 1 for Hall and 2 for Mezzanine" << endl;
            cin >> choice;
        }

        else if( hallSeats!=0 ) choice = 1 ; // only hall seats are available

        else choice = 2 ; // only mezzanine seats are available

        // rest of program
    }
    // ...
}
Jan 17, 2019 at 4:19pm
Thank you. I see what's happening, but I'm trying to make it so the program exits when all the seats are sold out. Right now, it exits when one hall sells out.
Jan 17, 2019 at 4:47pm
So... what's wrong with the advice dutch gave you?
Jan 17, 2019 at 4:55pm
Sorry I should've explained better I did that and what JL said. When I do just what dutch says it doesn't register as 0 seats it just keeps running, and doesn't display the no more seats message
Topic archived. No new replies allowed.