can't work out float???

This is a discussion on can't work out float??? within the C++ Programming forums, part of the General Programming Boards category; Hi everyone! /* A program that calculates and print the parking charges for each of 3 customers who parked their ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    27

    can't work out float???

    Hi everyone!


    /* A program that calculates and print the parking charges for each of 3 customers who parked their car in the garage yesterday
    */



    #include <iostream.h>
    #include <iomanip.h>
    #include <conio.h>

    int main()
    {
    float hours,
    totalHour = 0,
    totalCharge = 0;

    float calculateCharges(float);
    void head(void);

    head();
    for(int a = 1; a < 4; a++) {
    cout << setw(9) << ":";
    cin >> hours;
    cout << a << setw(10) << hours << "h" << setw(8) << "$"
    << calculateCharges(hours)
    << endl << endl;
    totalHour += hours;
    totalCharge += charges; //[B]THIS IS WHAT I TRIED. NO GOOD[B]

    //[B] I GET --UNDERFINED SYMBOL IN FUNCTION MAIN--- ERROR[B]
    }

    cout << "TOTAL" << setw(7) << totalHours;
    return 0;
    }


    float calculateCharges(float h) {

    float charges;

    if (h <= 3) {
    charges = 2;
    return charges;
    }
    else
    if (h >= 19) {
    charges = 10;
    return charges;
    }
    else
    return charges = (h-3)*.50 + 2;
    }


    void head (void) {

    cout << "Cars" << setw(9) << "Hours" << setw(12)
    << "Charges" << endl;
    cout << "----" << setw(9) << "-----" << setw(12) << "-------"
    << endl;
    }


    I have managed to do what it has asked me to do. The only that is wrong with it is that I CAN'T GET THE TOTAL FOR THE CHARGES.

    Can someone help me, what should I do?
    I tried doing the same for what I did with [B]totalHours[B] but it doesn't work. I get an error message ---Underfined symbol in function main---


    Pleaseeeeeeeee help Pleassseeeeeeeeeeeeee...
    Many thanks in advance

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    > totalCharge += charges; //[B]THIS IS WHAT I TRIED. NO GOOD[B]

    That's because you haven't declared charges in main. charges is only declared in the calculateCharges function, so it's out of scope in main.

    Here's how I'd do it:
    Code:
    int main() 
    { 
    float hours, 
    totalHour = 0, 
    totalCharge = 0,
    charges; 
    
    float calculateCharges(float); 
    void head(void); 
    
    head(); 
    for(int a = 1; a < 4; a++) { 
    cout << setw(9) << ":"; 
    cin >> hours; 
    charges = calculateCharges(hours);  //Might as well use the return value, since you've declared it.
    cout << a << setw(10) << hours << "h" << setw(8) << "$" 
    << charges<< endl << endl; 
    totalHour += hours; 
    totalCharge += charges; 
    } 
    
    cout << "TOTAL" << setw(7) << totalHours; 
    return 0; 
    }

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    27

    Many thanks :)

    Thanks Govtcheez!!!

    I hate it when I can't figure out simple problems.


    and thanks to everyone else too



    I just wanna take this opportunity to say thanks to everybody who help out. I just love this site. I mean if you have a sincere problem and u are completely stuck with nowhere to go to ask for help you can always 100% rely on the very nice people that is willing to help out if they can.
    There are some really great people out there who has gotten me out of jams that I would still be stuck in if it won't for them

    Many thanks again!!!

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Code:
    float calculateCharges(float);
    void head(void);
    You should avoid declaring the function declarations here....... put them outside main()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 05:36 PM
  2. Float Function Issues
    By GCNDoug in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2007, 03:25 PM
  3. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 04:57 PM
  4. Repetition in do{}while
    By Beast() in forum C Programming
    Replies: 25
    Last Post: 06-16-2004, 10:47 PM
  5. error declaration terminated incorrectly help
    By belfour in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2002, 08:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21