Help with prices !!

ok this is my first time asking for help but if someone could help me i would appreciate this.
I have to make a program that calculate parking ticket prices
minimal price for parking is 2 $
maximum price is 10$
fixed price is 2$ for 1-3 hours if you stay longer price goes up for 0.50 each hour max stay time 24 h

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
  #include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    
    double h;
    double price;
    
    cout << "enter hours";
    cin >> h;
   


    if (h <= 3)
    { 
    price = 2;
    }
    else if  (h > 3 && price != 10 && h<24)
    {
    price = (h*0.50)+2;
    }
    else if (price = 10)
    {
    price = 10;
    }
    else 
    {
     cout<< "didnt follow the ryules";
     system("pause");
     return 0;
     }
    cout << price ;
 
    

    system("PAUSE");
    return EXIT_SUCCESS;
}


sorry for weak code ;)
Hey! First of all. Welcome to the forum :)

else if (price = 10) // You need == not =

Also. Here is the main problem I noticed.

1
2
3
4
5
6
If this if statement is triggered - 

else if  (h > 3 && price != 10 && h<24)
{
    price = (h*0.50)+2;
}


You're telling the program. price != 10 But what is this price? Becuase price is nothing, you just create it double price; but do nothing with it. So just initialize the price in the beginning.

double price = 0;

Edit: Also the math is a but off. It should be - price = (h*0.50) + 0.5;

Edit 2: Also, this will never happen

1
2
3
4
else if (price = 10)
{
    price = 10;
}


Because of this

else if (h > 3 && price != 10 && h<24)

You're telling it to enter this statement, if hour is between 3 and 24. Which is where 10 is. But it will always choose this one over the other one. So switch their places.

1
2
3
4
5
6
7
8
else if (h == 10)
	{
		price = 10;
	}
	else if (h > 3 && price != 10 && h<24)
	{
		price = (h*0.50) + 0.5;
	}
Last edited on
hmm.. now it kind a doesnt limit me on 10$ and when i enter 24 h it breaks down and says what it should say on 25 :)
@TarikNeaj Thanks for the help i think i figured out rest of code ;)
Glad you did :)
Topic archived. No new replies allowed.