variable charge is declared both as a global and as a local variable inside main(). Delete the variable in main() because it hides the global.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
First off:
Use code tags! It makes it hard to read code otherwise, and usage of them here is highly emphasized.
Now for your code:
cout<< count << setw(20)<< num << setw(18)<< fixed << setprecision( 2 ) << calculateCharge (charge) << endl;
You never initalized charge when you call calculateCharge() . Try putting in calculateCharge(num) instead (at least I think that's what you want). And you need to grab the value returned, if you want to calculate total charges.
charge = charge;
There's absoloutely no point in this line. You obviously need charge to equal the result of calculateCharge , which you should probably dobefore calling cout .
A better way to write the 3 lines that follow would be (also slightly more efficient):
count++;
total_hours += num;
total_charge += charge;
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
variable charge is declared both as a global and as a local variable inside main().
Where's the global instance of charge ? I can't seem to find it...
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
Where's the global instance of charge ? I can't seem to find it...
I thought it was just above main(), but I don't see it now. So maybe I was seeing things?
Sorry Joe, I accidentally edited your post instead of creating a new one. But fixed it now.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Just so I understand your input above, I need to add the global variable "charge" and remove the one in main(), correct?
You should avoid the use of globals in regular C++ programming. I know it's unavoidable sometimes, but I don't think there's a need to add a global charge variable in this situation.For the calculateCharge(charge), in the function, I say return "charge", so shouldn't that return that when I call it in the main()? I tried using charge and num and it still wouldn't work either way. I do get an warning saying "charge" has not been used but not initiated, but I tried to.
Here's how I think you should do it:
// ...
int charge;
// ...
cout << "enter hours parked ";
cin >> num ;
charge = calculateCharge(num);
cout<< setw(4)<< "CAR " << setw(20)<< "HOURS" << setw(18)<< "CHARGE\n";
cout<< count << setw(20)<< num << setw(18)<< fixed << setprecision( 2 ) << charge << endl;
count++;
total_hours += num;
total_charge += charge;
// etc.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339