Short term parking

I am having trouble with my code. The error message that I get is no match for operator =.

#include<iostream>

using namespace std;

int main()
{
float minute;
double cost;
int h;
cout<< "*****************************************************";
cout<< endl;
cout<< endl;
cout<< " Short Term Parking ";
cout<< endl;
cout<< endl;
cout<< "******************************************************";
cout<< endl;
cout<< "Enter in the number of hours (from 0 to 24) you have in parking garage. :";
cin>> h;
cout<< endl;
if (h >= 0 h <= 3)
{
cout<< "Your fee for short term parking is : " << cost = 5;
}
else if (h >= 3 h <= 9)
{
cout<< "Your fee for short term parking is : " << cost = 6 * h + 1;
}
else if (h > 9 h <= 24)
{
cout<< "Your fee for short term parking is : " <<cost = 60;
}
else
{
cout<< "Have a nice day!!! ";
}
return 0;
}
1
2
3
if (h >= 0 h <= 3)
else if (h >= 3 h <= 9)
else if (h > 9 h <= 24)
You forgot your && operator

1
2
3
<< cost = 5;
<< cost = 6 * h + 1;
<<cost = 60;

You can't output an assignment.
Hello james58,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Consider these changes for your program:
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
#include <iostream>
#include <iomanip>  // <--- Added.

using namespace std;

int main()
{
    double minute;  // <--- Changed. Never used.
    double cost;
    double hour;  // <--- Changed.
    
    std::cout << std::fixed << std::setprecision(2);

    cout <<
        "*****************************************************\n\n"
        << std::setw((53 - 18) / 2) << ' ' << "Short Term Parking\n\n"
        "******************************************************\n\n"
        "Enter in the number of hours (from 0 to 24) you have in parking garage.: ";  // <--- Space was on the wrong side of the (:).
    //std::cout
    //    << std::setfill('*') << std::setw(53) << '*' << std::setfill(' ') << '\n'
    //    << std::setw((53 - 18) / 2) << ' ' << "Short Term Parking\n\n"
    //    << std::setfill('*') << std::setw(53) << '*' << std::setfill(' ') << '\n';
    cin >> hour;

    cout << '\n';

    if (hour >= 0.0 && hour <= 3.0)
    {
        cost = 5.0;
    }
    else if (hour >= 3.0 && hour <= 9.0)
    {
        cost = 6.0 * hour + 1.0;
    }
    else if (hour > 9.0 && hour <= 24.0)
    {
        cost = 60.0;
    }
    else
    {
        cout << "Have a nice day!!! ";
    } 

    cout << "Your fee for short term parking is : " << cost;

    return 0;  // <--- Not required, but makes a good break point for testing.
}

These days prefer to use "double" over "float", then again "minute" is never use, and would most likely be promoted to a"double" if used in a calculation with a "double".

Andy

Edit:
Last edited on
Thank you .... I am new at this and the if and if else statement confuse me at times.
if and else should not be too confusing. you can chain statements and make very complex conditions,
but they just do one thing, they make a decision whether to execute a block of code or not.
It comes in only 2 flavors, with and without the else

if something that can be true //if without an else, you do something 'extra' when a condition is true.
{
do these things when the condition is true.
}
this stuff always happens;
-------------------------------------------------
or with an else:
if(something that can be true)
{
do these things
}
else
{
do other things //this can ONLY happen when the 'something that can be true' was actually false.
}
these things always happen;

that is all there is to it, and all you can 'really' do.
you can repeat (chain) them:
if(1)
code1
else
if(2)
code2
else
if(3)
etc

but the only thing new that is happening here is that the second if and third if are *inside* the else blocks from the previous if statement. It can be tricky to follow it in code, but the previous form is still true,
it just happens that inside the code that lives under an if statement you can have another if statement.

and you can nest them to do more complicate things.
if (1)
{
//only 1 is known to be true here. 2 could be anything.
do something no matter what 2 is;
if(2)
//both 1 and 2 are true here, do whatever special thing when 2 is true.
}

and you can have complex if conditions
if(this && that || (other && aforuth)) //it still boils down to true or false, do it or do not do it.

--edit, not sure what is going on with the code tag today. Taking it out for now.
Last edited on
Topic archived. No new replies allowed.