Hello, I'm having issues with my final total for charges. When I run my program, it either comes up with "0" or if I don't declare my variable, it comes up with a long string of numbers. My total hours works great, but not my charges. Can someone take a look at my code and see if they see something I need to look at? I thought it was my setprecision or fixed, but it's not. Thanks

#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::fixed;

#include <iomanip>

using std::setw;
using std::setiosflags;
using std::setprecision;

#include <cmath>


//function prototype
double calculateCharge(double);
double Totalhours(double);

int count;
int total_hours;
int total_charge;

int main()

{
    int num;
    int charge;
    int count = 1;   
    int total_hours = 0 ;
    int total_charge = 0 ;

    for( int i = 0; i < 3; ++i )

    {
        
        cout << "enter hours parked ";
        cin >> num ;
        
        cout<< setw(4)<< "CAR " << setw(20)<< "HOURS" << setw(18)<< "CHARGE\n";
        cout<< count << setw(20)<< num << setw(18)<< fixed << setprecision( 2 ) << calculateCharge (charge) << endl;
        
        charge = charge;        
        count = count + 1;
        total_hours = total_hours + num;
        total_charge = total_charge + charge;
                
    }
        cout<< setw(05)<< "TOTAL HOURS" << setw(10)<< total_hours << setw(16)<< fixed << setprecision( 2 ) << endl;
        cout << fixed << setprecision( 2 ); 
        cout<< setw(10)<< "TOTAL CHARGES"<< setw(10)<< total_charge << setw(16)<< endl;
        cout<< setw(10)<< "TOTAL CHARGES"<< setw(10)<< charge << setw(16)<< endl;

        return 0;
}//end main



double calculateCharge( double x)
{

double charge;       

    if (x <= 3)
          charge = 2;
    
    else if (x >19)
           charge = 10;

    else if (x > 3)
            charge = 2 + (x - 3) * (.5);

return charge;

}

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.

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 do before 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;

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...

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.

Just so I understand your input above, I need to add the global variable "charge" and remove the one in main(), correct?

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.

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.

That did it, and that's what I kept trying to do: charge = calculateCharge(num); Thank you very much, I'm happy again and happy that I was not that far off. Thanks!

This question has already been answered. Start a new discussion instead.