Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,136 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,821 people online right now. Registration is fast and FREE... Join Now!




pass user inputs into functions ?

 
Reply to this topicStart new topic

pass user inputs into functions ?, Is there a way to pass 3 user inputs into a function that calculates a

grimes
12 May, 2007 - 07:24 PM
Post #1

New D.I.C Head
*

Joined: 28 Apr, 2007
Posts: 19


My Contributions
Ok, my homework works fine, this is just for kicks but I keep getting compiler errors. I want to pass in 3 values that the user enters, into the function (code below) and then calculate the total of the values, to then output to the screen.

this is the function I'm playing with
CODE

double calculateHoursParked(double hoursParkedCar1,double hoursParkedCar2, double hoursParkedCar3)
{
    double totalHoursParked = 0;

    totalHoursParked = hoursParkedCar1 + hoursParkedCar2 + hoursParkedCar3;

    return totalHoursParked;
}


This is the whole programs, with stuff commented out to get the function calculateHoursParked to run

CODE

#include<iostream>
#include<iomanip>
#include<cmath>

using namespace std;

//function prototype defined after end main
double calculateCharge(double);
void totalHoursParked(double, double, double);

int main()
{    
    double hoursParkedCar1 = 0;        //store hours for 1st parked car
    double hoursParkedCar2 = 0;        //store hours for 2nd parked car
    double hoursParkedCar3 = 0;        //store hours for 3rd parked car
    /*double totalHoursParked = 0;*/    //store value for the aggregate hours parked
    double totalParkingFees = 0;    //store value for the total aggregate fees
    char response = 'x';            //store junk value to quit the program or continue

    //perform the exercise as long as the quit value is not entered
    do
    {    
        //Ask the user to input the hours parked for three cars
        cout << "\nEnter hours parked for 3 customers (hrs hrs hrs): ";
        cin >> hoursParkedCar1 >> hoursParkedCar2 >> hoursParkedCar3;

        /*
        Not sure if it was required to ask the question repeatedly ?
        */

        //calculate the aggregate hours parked
        /*totalHoursParked = hoursParkedCar1 + hoursParkedCar2 + hoursParkedCar3;*/

        //calculate the aggregate fees charged
        totalParkingFees = (calculateCharge(hoursParkedCar1)) + (calculateCharge(hoursParkedCar2)) + (calculateCharge(hoursParkedCar3));
    
        //output to the headings to the screen
        cout << "\n" << "Car" << setw(10) << "Hours" << setw(15) << "Charge\n";

        //output the values for each car to the screen
        cout << "1" << setw(12) << fixed << setprecision(2) << hoursParkedCar1 << setw(14) << fixed << setprecision(2) << calculateCharge(hoursParkedCar1) << endl;
        cout << "2" << setw(12) << fixed << setprecision(2) << hoursParkedCar2 << setw(14) << fixed << setprecision(2) << calculateCharge(hoursParkedCar2) << endl;
        cout << "3" << setw(12) << fixed << setprecision(2) << hoursParkedCar3 << setw(14) << fixed << setprecision(2) << calculateCharge(hoursParkedCar3);

        //output the aggregate values to the screen
        cout << "\nTotal" << setw(8) << fixed << setprecision(2) << totalHoursParked << setw(14) << totalParkingFees <<endl <<endl;
    
        //ask the user to quit & read in their response
        cout << "Would you like to quit y or n ?";
        cin >> response;

    //contine the loop if the response is not y
    }while(response != 'y');

    system("pause");

    return 0;

}//end of main

//define the function calculateCharge
double calculateCharge(double hours)
{
    double charge = 0;    //initialize the variable charge

    //calculate the value of charge depending on hours to calculate fees
    if(hours <= 3)
    {
        charge = 2;
    }
    else if(hours > 3 && hours < 19)
    {
        charge = (2 + .50 * (hours - 3));
    }
    else if(hours >= 19)
    {
        charge = 10;
    }
    return charge;
}//end of function calculateCharge

double calculateHoursParked(double hoursParkedCar1,double hoursParkedCar2, double hoursParkedCar3)
{
    double totalHoursParked = 0;

    totalHoursParked = hoursParkedCar1 + hoursParkedCar2 + hoursParkedCar3;

    return totalHoursParked;
}


Like I say, trying to get this function to work is just bonus and trying to get better, the assignment part is done.

Kindly,

-g
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Pass User Inputs Into Functions ?
13 May, 2007 - 05:46 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
What ate the errors you are receiving?

The problem I see is where you call the function here:
CODE

cout << "\nTotal" << setw(8) << fixed << setprecision(2) << totalHoursParked << setw(14) << totalParkingFees <<endl <<endl;

First,you are treating the function call like a variable. It is not. As well, you are not actually passing any parameters to the function. It likely needs to resemble the following:
CODE

cout << "\nTotal" << setw(8) << fixed << setprecision(2) << totalHoursParked(hoursParkedCar1,hoursParkedCar2,hoursParkedCar3) << setw(14) << totalParkingFees <<endl <<endl;

Finally, your function prototype declares the function as return type void, whereas you clearly return a double. The prototype will need to be modified.
User is offlineProfile CardPM
+Quote Post

grimes
RE: Pass User Inputs Into Functions ?
13 May, 2007 - 10:30 AM
Post #3

New D.I.C Head
*

Joined: 28 Apr, 2007
Posts: 19


My Contributions
QUOTE(Amadeus @ 13 May, 2007 - 06:46 AM) *

What ate the errors you are receiving?

The problem I see is where you call the function here:
CODE

cout << "\nTotal" << setw(8) << fixed << setprecision(2) << totalHoursParked << setw(14) << totalParkingFees <<endl <<endl;

First,you are treating the function call like a variable. It is not. As well, you are not actually passing any parameters to the function. It likely needs to resemble the following:
CODE

cout << "\nTotal" << setw(8) << fixed << setprecision(2) << totalHoursParked(hoursParkedCar1,hoursParkedCar2,hoursParkedCar3) << setw(14) << totalParkingFees <<endl <<endl;

Finally, your function prototype declares the function as return type void, whereas you clearly return a double. The prototype will need to be modified.


----------------

Ok,

I made the changes suggested and I still get the following 2 errors at compile time:

I appreciate the help.

- G

1 - Error 1 error LNK2019: unresolved external symbol "double __cdecl totalHoursParked(double,double,double)" (?totalHoursParked@@YANNNN@Z) referenced in function _main main.obj

2- Error 2 fatal error LNK1120: 1 unresolved externals

Here is entire source

CODE

#include<iostream>
#include<iomanip>

using namespace std;

//function prototype defined after end main
double calculateCharge(double);
double totalHoursParked(double, double, double);

int main()
{    
    double hoursParkedCar1 = 0;        //store hours for 1st parked car
    double hoursParkedCar2 = 0;        //store hours for 2nd parked car
    double hoursParkedCar3 = 0;        //store hours for 3rd parked car
    /*double totalHoursParked = 0;*/    //store value for the aggregate hours parked
    double totalParkingFees = 0;    //store value for the total aggregate fees
    char response = 'x';            //store junk value to quit the program or continue

    //perform the exercise as long as the quit value is not entered
    do
    {    
        //Ask the user to input the hours parked for three cars
        cout << "\nEnter hours parked for 3 customers (hrs hrs hrs): ";
        cin >> hoursParkedCar1 >> hoursParkedCar2 >> hoursParkedCar3;

        /*
        Not sure if it was required to ask the question repeatedly ?
        */

        //calculate the aggregate hours parked
        //totalHoursParked = hoursParkedCar1 + hoursParkedCar2 + hoursParkedCar3;

        //calculate the aggregate fees charged
        totalParkingFees = (calculateCharge(hoursParkedCar1)) + (calculateCharge(hoursParkedCar2)) + (calculateCharge(hoursParkedCar3));
    
        //output to the headings to the screen
        cout << "\n" << "Car" << setw(10) << "Hours" << setw(15) << "Charge\n";

        //output the values for each car to the screen
        cout << "1" << setw(12) << fixed << setprecision(2) << hoursParkedCar1 << setw(14) << fixed << setprecision(2) << calculateCharge(hoursParkedCar1) << endl;
        cout << "2" << setw(12) << fixed << setprecision(2) << hoursParkedCar2 << setw(14) << fixed << setprecision(2) << calculateCharge(hoursParkedCar2) << endl;
        cout << "3" << setw(12) << fixed << setprecision(2) << hoursParkedCar3 << setw(14) << fixed << setprecision(2) << calculateCharge(hoursParkedCar3);

        //output the aggregate values to the screen
        cout << "\nTotal" << setw(8) << fixed << setprecision(2) << totalHoursParked(hoursParkedCar1,hoursParkedCar2,hoursParkedCar3) << setw(14) << totalParkingFees <<endl <<endl;
    
        //ask the user to quit & read in their response
        cout << "Would you like to quit y or n ?";
        cin >> response;

    //contine the loop if the response is not y
    }while(response != 'y');

    system("pause");

    return 0;

}//end of main

//define the function calculateCharge
double calculateCharge(double hours)
{
    double charge = 0;    //initialize the variable charge

    //calculate the value of charge depending on hours to calculate fees
    if(hours <= 3)
    {
        charge = 2;
    }
    else if(hours > 3 && hours < 19)
    {
        charge = (2 + .50 * (hours - 3));
    }
    else if(hours >= 19)
    {
        charge = 10;
    }
    return charge;
}//end of function calculateCharge

double calculateHoursParked(double hoursParkedCar1, double hoursParkedCar2, double hoursParkedCar3)
{
    double totalHoursParked = 0;

    totalHoursParked = hoursParkedCar1 + hoursParkedCar2 + hoursParkedCar3;

    return totalHoursParked;
}


User is offlineProfile CardPM
+Quote Post

vasdueva
RE: Pass User Inputs Into Functions ?
13 May, 2007 - 04:58 PM
Post #4

D.I.C Head
**

Joined: 3 Apr, 2007
Posts: 69


My Contributions
CODE

//output the aggregate values to the screen
        cout << "\nTotal" << setw(8) << fixed << setprecision(2) << totalHoursParked(hoursParkedCar1,hoursParkedCar2,hoursParkedCar3) << setw(14) << totalParkingFees <<endl <<endl;


I believe you have a naming disagreement. Here you call the function "totalHoursParked. In the actual method header it is "calculateHoursParked".

I believe that might be causing the first error. as totalHoursParked(double,double,double) is not defined.

Unless you are calling that method from elsewhere... then my mistake.

Also I don't think you ever call the function "calculateHoursParked"

This post has been edited by vasdueva: 13 May, 2007 - 05:00 PM
User is offlineProfile CardPM
+Quote Post

grimes
RE: Pass User Inputs Into Functions ?
13 May, 2007 - 09:36 PM
Post #5

New D.I.C Head
*

Joined: 28 Apr, 2007
Posts: 19


My Contributions
QUOTE(vasdueva @ 13 May, 2007 - 05:58 PM) *

CODE

//output the aggregate values to the screen
        cout << "\nTotal" << setw(8) << fixed << setprecision(2) << totalHoursParked(hoursParkedCar1,hoursParkedCar2,hoursParkedCar3) << setw(14) << totalParkingFees <<endl <<endl;


I believe you have a naming disagreement. Here you call the function "totalHoursParked. In the actual method header it is "calculateHoursParked".

I believe that might be causing the first error. as totalHoursParked(double,double,double) is not defined.

Unless you are calling that method from elsewhere... then my mistake.

Also I don't think you ever call the function "calculateHoursParked"


------------------

You are Right On, something so easy, damn it. Thanks for the help and sorry to waste everyone's time with it.

- Grimes
User is offlineProfile CardPM
+Quote Post

vasdueva
RE: Pass User Inputs Into Functions ?
14 May, 2007 - 09:02 AM
Post #6

D.I.C Head
**

Joined: 3 Apr, 2007
Posts: 69


My Contributions
No problem. Simple things get to be a pain when the compiler isn't crystal clear on what the error is.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:32PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month