Getting speed to apply to a fee?

Hello. I cannot get the speed in line 27 to apply to the choices that follow. I thought that if the cin>>speed was included within the larger brackets, then it would automatically apply to any sub-brackets within...Help please?

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
#include <iostream>
using namespace std;
int main()

{
char code;
int fee;
int speed;
char type;

cout<<"Please enter 'V' for visitor, 'F' for faculty/staff, or 'S' for student.\n";
cin>>code;

if (code=='V'||code=='v')
   {
   fee=120;
   cout<<"Fee is $120.00\n";
   }
else if (code=='F'||code=='f')
   {
   fee=75;
   cout<<"Fee is $75.00\n";
   }
else if (code=='S'||code=='s')
   {
   cout<<"Please enter speed you were going at time of ticket.\n";
   cin>>speed;
   cout<<"Enter '1' if you are a senior, or '2' if not.\n";
   cin>>type;
   if (type=='1')
      {
      if (41<=speed<=55)
        {
        fee=212.5;
        cout<<"Fee is $212.50\n";
        }
      else if (speed>55)
        {
        fee=362.5;
        cout<<"Fee is $362.60\n";
        }
      else
        {
        fee=125;
        cout<<"Fee is $125.00\n";
        }
      }
   else
      {
      if (speed>40)
        {
        fee=162.50;
        cout<<"Fee is $162.50\n";
        }
      else
        {
        fee=75;
        cout<<"Fee is $75.00\n";
        }
      }
   }
return 0;
}
Last edited on
Is your program:
1) Not compiles
    * Is other code compiles correctly, i. e. is your compiler configured properly?
    * If it is, give us error message and corresponding code part.
2) Not running
    * Are you sure that it is not a problem with automatically closing console?
    * How do you run it?
    * Is there any error messages?
3) Gives an error when run
    * Is it system error message or error reported in console?
    * Give us error message and input which leads to error.
4) Not giving correct results
    * Tell what you entered, what you expected, and what you got.
    * Are you sure that results you expected are correct?
Thanks for responding,

it compiles and runs, but gives unexpected results. When I enter:
's'
'70'
'1'
it gives the result:
"Fee is $212.50"

but I was expecting:
"Fee is $362.50"

So, no matter what speed you input, it ignores it and always gives the output of 212.50 when you say your 's' and then '1'.
if (41<=speed<=55) is always true:
41<=speed can evaluate either to true or false, which can be converted to 1 or 0 respectively.
(0 or 1) <= 55 is always true.

So no matter what speed you enter, you will always get fine of 212.5.

fix: (41 <= speed && speed <= 55)
I just ran the program inputting:
's'
'70'
'2'
and it gives the correct result. Also,
's'
'36'
'2'
gives the correct result.

So maybe the problem is
if (41<=speed<=55)
on line 32?
↑ Answered already
@noahfrere posted literally 1 minute after you, could have even been 1 second after you, it doesnt show specifically. He probably pressed reply before your answer even existed.

Edit: Whoops thought noah was just other dude and not the OP :D
Last edited on
Wow! Thanks!
Topic archived. No new replies allowed.