Having some problems with a program that calculates airport parking values

closed account (28ApDjzh)
See title. The list of values is:
First 30 minutes FREE
31 to 60 minutes $2.00
Each additional 30 minutes $1.00
Up to seven hours $14.00
Thereafter, each hour $1.00
24-hour maximum $21.00
Below is a smaller test snippet of this program. I have a full one, and both of them have the same problem- if 60 or over is the entered value, it always prints 2 when it should print 3 or higher. Please help.

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
  #include <iostream>
using namespace std;
int main( )
{
    int parkingtime;
    cout << "Enter parking duration (in minutes): ";
    cin >> parkingtime;
    cout << "" << endl;
    if( parkingtime <= 30 )
    {
        cout << "Parking cost = $0";
    }
    else
    { 
    if( parkingtime >= 31 <= 60 )
    {
        cout << "Parking cost = $2";
    }
    else if(parkingtime >= 61 <= 90)
    {
         cout << "Parking cost = $3"; 
    }
}
}
Last edited on
I'm very new to programming, but I'm seeing a few issues with the code here.

1st thing I notice is your if/else if.

  #include <iostream>
using namespace std;
int main( )
{
    int parkingtime;
    cout << "Enter parking duration (in minutes): ";
    cin >> parkingtime;
    cout << "" << endl;
    if( parkingtime <= 30 )
    {
        cout << "Parking cost = $0";
    }
    else if((parkingtime>=31)&&(parkingtime <= 60 ))
    { 
        cout << "Parking cost = $2";
    }
        
    else if ((parkingtime >= 61)&&(parkingtime <= 90))
    {
         cout << "Parking cost = $3"; 
    }

}


So that fixes your problem. Remember, the "else" by itself should basically only come at the end of the 'main' if statement.

Also, you can't do the (parkingtime>=31<=60). It just doesn't work that way. You have to separate them. I'm new so I don't know how the compiler even reads that as correct, but apparently it does. lol. But, it gives you the wrong answer.

So there you have it. :)
Last edited on
closed account (28ApDjzh)
I'm still having trouble as I applied the fix to my main program and now it always prints 3. Not all of the values in the main program are done yet but it's still broken... Please help.

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
 #include <iostream>
using namespace std;
int main( )
{
    int parkingtime;
    cout << "Enter parking duration (in minutes): ";
    cin >> parkingtime;
    cout << "" << endl;
    if( parkingtime <= 30 )
    {
        cout << "Parking cost = $0";
    }
    else if((parkingtime>=31)&&(parkingtime <= 60 ))
    { 
        cout << "Parking cost = $2";
    }
        
    else if ((parkingtime >= 61)&&(parkingtime <= 90))
    {
         cout << "Parking cost = $3"; 
    }
    else if ((parkingtime >= 90)&&(parkingtime <= 120))
    {
         cout << "Parking cost = $4"; 
    }
    else if ((parkingtime >= 120)&&(parkingtime <= 150))
    {
         cout << "Parking cost = $5"; 
    }
    else if ((parkingtime >= 150)&&(parkingtime <= 180))
    {
         cout << "Parking cost = $6"; 
    }
    else if ((parkingtime >= 180)&&(parkingtime <= 210))
    {
         cout << "Parking cost = $7"; 
    }
    else if ((parkingtime >= 210)&&(parkingtime <= 240))
    {
         cout << "Parking cost = $8"; 
    }
    else if ((parkingtime >= 240)&&(parkingtime <= 270))
    {
         cout << "Parking cost = $9"; 
    }
    else if ((parkingtime >= 270)&&(parkingtime <= 300))
    {
         cout << "Parking cost = $10"; 
    }
    else if ((parkingtime >= 300)&&(parkingtime <= 330))
    {
         cout << "Parking cost = $11"; 
    }
    else if ((parkingtime >= 330)&&(parkingtime <= 360))
    {
         cout << "Parking cost = $12"; 
    }
    else if ((parkingtime >= 360)&&(parkingtime <= 390))
    {
         cout << "Parking cost = $13"; 
    }
    else if ((parkingtime >= 390)&&(parkingtime <= 420))
    {
         cout << "Parking cost = $14"; 
    }
   
}
Last edited on
It's because your numbers are 1 off.

Look at where it is between 61 and 90. . . .then look at your next "else if" -- it starts at >=90. . . but that's wrong because the previous one is <=90.

Add one to all the >= ifs, starting with "90" and your problem will be fixed.

Let me know when you fixed it. :)
Last edited on
closed account (28ApDjzh)
Thank you! I got it working.
Awesome! Glad to hear it. :)

Topic archived. No new replies allowed.