Am I over thinking this?

Ok so, I have this task and it seems simple enough, and I may be over looking something or over thinking it, but I would like some outside feed back. The problem is this: I'm creating a parking cost calculator and the cost cannot be more than $5 if the Arrival time is after 5pm or the departure time is before 8am. this is what I have so far:

// test_computeCost.cpp
//
// computeCost and a test driver

#include <iostream>
using namespace std;

struct Time
{
int h, m;
};

void computeCost( const Time & in, const Time & out,
float & cost )
{

if ( (in.h*60 + in.m) > (out.h*60 + out.m) )
cout << "Arrival can't be later than departure time"<<endl;

//if ((in.h*60 + in.m) < (out.h*60 + out.m)) //I'm not sure if I should comment out or not

cost = ( (out.h*60 + out.m) - (in.h*60 + in.m) )/60.0*4;

if (( out.h <= 8) || (in.h >= 17))
cost <= 5; //this is where the problem occurs no errors, just incorrect result

else if ( cost < .5 )
cost = .5;
else if ( cost > 10 )
cost = 10;
}

int main()
{

Time in, out;
float cost;

in.h=1; in.m=5;
out.h=8; out.m=0;
//the above values are just test values that make the time statement true

cout << cost <<endl;

return 0;
}

-----------

ok so here's another question is there a way for something like cost <= 5; to actually work to make cost never exceed 5 while the time conditions are met? or do I have something in the wrong order?? please help!! thanks!
You can't assign a range to a variable. The ONLY assignment operator in C/++ is =.
What you need is to first check that cost>5, and if it is, set it to 5.
Unfortunately I've done that in attempts in a way to fix it but it didn't result in the correct outcomes... I think I may just have to go with that if I can't find another solution though. thanks for confirming the range thing though :)
Think logically and translate your thinking directly to code. Like this:

Compute the cost irrespective of the special case;
IF departure time is before 8am OR arrival time is after 5pm AND cost > $5 THEN
Set cost to $5

This is the algorithm you want. You have to figure out the algorithm before you can write the code.
Topic archived. No new replies allowed.