User Defined Function help! c++

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 1485 Views - Last Post: 29 April 2012 - 06:38 PM Rate Topic: -----

#16 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Re: User Defined Function help! c++

Posted 26 April 2012 - 07:30 AM

I just figured out what i did wrong up above.
I changed my code to this which now makes sense
#include <iostream>
#include <string>

using namespace std;

//user defined functions
void GetRate()
{
    cout << "Hourly Rate? ";
}

/************************************************/

void GetHrs()
{
  cout << "Hours Worked? ";  
}

/*************************************************/

float GetPay (float r, float h)
{
    float p;
    p = r * h;
    
    return(p);
}


/*
 * 
 */
int main() 
{
    //declare variables
    string name;
    float rate;
    float hours;
    float pay;
    
    
    //get Employee's name
    cout << "Employee name? ";
    cin >> name;
    
    //Get employee hourly rate
    GetRate();
    cin >> rate;
    
    //Get hours worked
    GetHrs();
    cin >> hours;
    
    //Calculate pay and print to screen
    float x=hours, y=rate, total;
    total = GetPay(x, y);
    cout << name << " earned " << total;
    cin >> pay;
    
    
    
    
            

    return 0;
}



Is the program storing the value of "total" into the variable pay?
Was This Post Helpful? 0
  • +
  • -

#17 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Re: User Defined Function help! c++

Posted 26 April 2012 - 07:59 AM

I'm trying to calculate a FICA tax from the total amount earned, but for some reason it's not calculating it right and keeps displaying a negative number that's way off. Does anyone know why? It would be with my user function "CalcFICA" or down below inside of the main somewhere

#include <iostream>
#include <string>

using namespace std;

//user defined functions
void GetRate()
{
    cout << "Hourly Rate? ";
}

/************************************************/

void GetHrs()
{
  cout << "Hours Worked? "; 
}

/*************************************************/

float GetPay (float r, float h)
{
    float p;
    p = r * h;
    
    return(p);
}

/*************************************************/

float CalcFICA(float a, float B)/>
{
    float f;
    f = a * b;
    
    return (f);
}


/*
 * 
 */
int main() 
{
    //declare variables
    string name;
    float rate;
    float hours;
    float pay;
    
    
    
    
    //get Employee's name
    cout << "Employee name? ";
    cin >> name;
    
    //Get employee hourly rate
    GetRate();
    cin >> rate;
    
    //Get hours worked
    GetHrs();
    cin >> hours;
      
    
    //Calculate pay and print to screen
    float x=hours, y=rate, total;
    total = GetPay(x, y);
    cout << name << " earned " << " $" << total << endl;
    
    
    //calculate FICA and print to screen
    float ficaRate=.0765, ficaAmt;
    ficaAmt = CalcFICA(ficaRate, pay);
    cout << "FICA " << " $" << ficaAmt;
    
    
    
    
            

    return 0;
}



View Postr.stiltskin, on 25 April 2012 - 07:19 PM, said:

View PostHouston573, on 25 April 2012 - 09:54 PM, said:

I tried this approach, but my program won't run and it doesn't tell me why.

Are you saying that you built this program and you didn't get any error messages? That's not possible. Try compiling that code again and see what message you get, & then we'll talk about why.

PS: what is your development environment? (Which IDE/compiler are you using, and what is your operating system?)


Oh i'm using NetBeans for my MacBook Pro
Was This Post Helpful? 0
  • +
  • -

#18 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: User Defined Function help! c++

Posted 26 April 2012 - 08:13 AM

You never assigned any value to pay. You stored the result of getPay in the new variable called total which you declared on line 68.

Why did you declare those new variables on line 68? Why don't you use hours, rate and pay on line 69?

It also seems odd to use the functions GetRate and GetHours just to print a message. You ought to perform the input operations inside those functions and make them return the value to main.

PS: doesn't your compiler give you a warning message about "pay", something like
"warning: 'pay' may be used unitialized in this function"? If not, you should try to increase the compiler warning level. I don't know specifically how to do that in NetBeans, but there should be some sort of global compiler settings or build options where you can do that.

This post has been edited by r.stiltskin: 26 April 2012 - 08:17 AM

Was This Post Helpful? 1
  • +
  • -

#19 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Re: User Defined Function help! c++

Posted 26 April 2012 - 04:36 PM

View Postr.stiltskin, on 26 April 2012 - 08:13 AM, said:

You never assigned any value to pay. You stored the result of getPay in the new variable called total which you declared on line 68.

Why did you declare those new variables on line 68? Why don't you use hours, rate and pay on line 69?

It also seems odd to use the functions GetRate and GetHours just to print a message. You ought to perform the input operations inside those functions and make them return the value to main.

PS: doesn't your compiler give you a warning message about "pay", something like
"warning: 'pay' may be used unitialized in this function"? If not, you should try to increase the compiler warning level. I don't know specifically how to do that in NetBeans, but there should be some sort of global compiler settings or build options where you can do that.


My teacher wanted us to create user defined functions to print these messages i believe. What he told us was to print this output and create this many functions with the name of Gethrs, GetRate, etc... but this is the only way i know how to get the main to print these messages while calling to the user defined functions.

I also don't know why i declared new variables so thank you for pointing that out and i just used hours, rate, and pay. I also stored the gross income to the variable pay and everything works good so far till i reach the FICA when the program's running. It gives me a number for instance "-$.0117509" if the user earns $100. that doesn't make sense because if the user earns 100 then the FICA value should be 7.65.
Do you know why it's doing that?
#include <iostream>
#include <string>

using namespace std;

//user defined functions
void GetRate()
{
    cout << "Hourly Rate? ";
}

/************************************************/

void GetHrs()
{
  cout << "Hours Worked? "; 
}

/*************************************************/

float GetPay (float r, float h)
{
    float p;
    p = r * h;
    
    return(p);
}

/*************************************************/

float CalcFICA(float a, float B)/>
{
    float f;
    f = a * b;
    
    return (f);
}


/*
 * 
 */
int main() 
{
    //declare variables
    string name;
    float rate;
    float hours;
    float pay;
    
    
    
    
    //get Employee's name
    cout << "Employee name? ";
    cin >> name;
    
    //Get employee hourly rate
    GetRate();
    cin >> rate;
    
    //Get hours worked
    GetHrs();
    cin >> hours;
      
    
    //Calculate pay and print to screen
    float total;
    total = GetPay(hours, rate);
    cout << name << " earned " << " $" << total << endl;
    
    
    
    //calculate FICA and print to screen
    float ficaRate=0.0765, ficaAmt;
    ficaAmt = CalcFICA(ficaRate, pay);
    cout << "FICA " << " $" << ficaAmt;
    
    
    
    
            

    return 0;
}



Oh and i added a "cin>>pay;" on line 71 to store that value to the variable but when i run the program it stops after that instead of calculating FICA next.

Ok i know i'm going to look stupid after this post, but oh well. I finally figured it out haha and it was so simple.
Here's the new and improved code so far.
int main() 
{
    //declare variables
    string name;
    float rate;
    float hours;
    float pay;
    
    
    
    
    //get Employee's name
    cout << "Employee name? ";
    cin >> name;
    
    //Get employee hourly rate
    GetRate();
    cin >> rate;
    
    //Get hours worked
    GetHrs();
    cin >> hours;
      
    
    //Calculate pay and print to screen
    
    pay = GetPay(hours, rate);
    cout << name << " earned " << " $" << pay << endl;
    
    
    
    
    //calculate FICA and print to screen
    float ficaRate=0.0765, ficaAmt;
    ficaAmt = CalcFICA(ficaRate, pay);
    cout << "FICA " << " $" << ficaAmt;
    
    
    
    
            

    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#20 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Re: User Defined Function help! c++

Posted 29 April 2012 - 12:59 PM

I've got my program running, but now i have to add conditions to it. For example if somebody enters a pay rate of less than 5.50 or more than 200.00 the program should print a message saying "please only enter values between 5.50 and 200.00". I know that i need an if statement in there and i was going to put it in my user-defined function, but i can't figure out how to incorporate it in there. Does anyone have any tips on doing that? I don't have a variable in my payrate user function so i'm not sure how to do my if statement.

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//user defined functions
void GetRate()
{
    cout << "Hourly Rate? ";
}

/************************************************/

void GetHrs()
{
  cout << "Hours Worked? "; 
}

/*************************************************/

float GetPay (float r, float h)
{
    float p;
    p = r * h;
    
    return(p);
}

/*************************************************/

float CalcFICA(float a, float B)/>
{
    float f;
    f = a * b;
    
    return (f);
}

/*************************************************/

float CalcFedTax(float c, float d)
{
    float e;
    e = c * d;
    
    return (e);
}

/*************************************************/

float CalcStateTax(float y, float z)
{
    float h;
    h = y * z;
    
    return (h);
}

/*
 * 
 */
int main() 
{
    //declare variables
    string name;
    float rate;
    float hours;
    float pay;
    
    
    
    
    //get Employee's name
    cout << "Employee name? ";
    cin >> name;
    
    //Get employee hourly rate
    GetRate();
    cin >> rate;
    
    //Get hours worked
    GetHrs();
    cin >> hours;
      
    
    //Calculate pay and print to screen
    
    pay = GetPay(hours, rate);
    cout << name << " earned " << " $" << pay << endl;
    
    
    
    
    //calculate FICA and print to screen
    float ficaRate=0.0765, ficaAmt;
    ficaAmt = CalcFICA(ficaRate, pay);
    cout << "FICA" << " $" << ficaAmt << endl;
    
    
    //calculate Federal Tax and print to screen
    float fedRate=0.22, fedAmt;
    fedAmt = CalcFedTax(fedRate, pay);
    cout << "Fed"<<" $"<<fedAmt<<endl;
    
    
    //calculate State Tax and print to screen
    float taxRate=0.12, stateAmt;
    stateAmt = CalcStateTax(taxRate, pay);
    cout << "State" << " $"<<stateAmt;
    
            

    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#21 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: User Defined Function help! c++

Posted 29 April 2012 - 01:10 PM

Problem is that you still haven't implemented the functions correctly. You're supposed to be doing the input in the function. But your GetRate function just prints a message and you're still doing the input in your main.

The GetRate function and the GetHours function should be receiving the user's input, and then returning those values to main. If you do that, you will of course have variables to work with in the functions, so you will be able to verify the inputs supplied by the user.
Was This Post Helpful? 0
  • +
  • -

#22 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Re: User Defined Function help! c++

Posted 29 April 2012 - 03:06 PM

View Postr.stiltskin, on 29 April 2012 - 01:10 PM, said:

Problem is that you still haven't implemented the functions correctly. You're supposed to be doing the input in the function. But your GetRate function just prints a message and you're still doing the input in your main.

The GetRate function and the GetHours function should be receiving the user's input, and then returning those values to main. If you do that, you will of course have variables to work with in the functions, so you will be able to verify the inputs supplied by the user.


If i do what you're saying then i'd have to also figure out another way to implement the GetPay function as well. I've been storing those values from GetRate and GetHrs into my main so how would i do the rest of my calculations in my main? Would i have to change all the calculations from the main because if i take out the cin>>hours and cin>>rate then i have no way of calculating the total pay.

I also don't know how to put a variable into my GetRate function...Can you show me an example maybe? Not give me the answer, but help guide for reference. I'm worried that now i'll have to take out all the calculations in my main and figure out a new way to do this program.
Was This Post Helpful? 0
  • +
  • -

#23 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: User Defined Function help! c++

Posted 29 April 2012 - 03:17 PM

According to your own description of the assignment, you're supposed to be doing everything -- user input and calculations -- in separate functions, not in main. You can define the variables in main, but then main should call other functions to get values for those variables and to do any calculations with those variables. main's "main job" is to call other functions.

Here it is -- everything you need to know about functions in 24 lines:

An other function can return a value to main:
int my_fun() {
    int temp_var;
    // do stuff
    temp_var = 5;
    return temp_var;
}

int main() {
    int real_var;
    real_var = my_fun();
    cout << "real_var = " << real_var << endl;
    return 0;
}



or main can pass a variable by reference to another function so that the other function can modify its value:
void my_fun2(int &tmp) {
   // do stuff
   tmp = 5;
}

int main() {
    real_var;
    my_fun2(real_var);
    cout << "real_var = " << real_var << endl;
    return 0;
}


This post has been edited by r.stiltskin: 29 April 2012 - 03:18 PM

Was This Post Helpful? 1
  • +
  • -

#24 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: User Defined Function help! c++

Posted 29 April 2012 - 03:31 PM

If there's anything that you don't understand about what I wrote in Post #23, ask.

Ask about my code, not yours, because its simpler and easier to explain. Once you thoroughly understand what I wrote it should be easy to apply these principles to your program.
Was This Post Helpful? 0
  • +
  • -

#25 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Re: User Defined Function help! c++

Posted 29 April 2012 - 04:45 PM

View Postr.stiltskin, on 29 April 2012 - 03:31 PM, said:

If there's anything that you don't understand about what I wrote in Post #23, ask.

Ask about my code, not yours, because its simpler and easier to explain. Once you thoroughly understand what I wrote it should be easy to apply these principles to your program.


I understand what you did and it makes sense, but because mine is more complex including conditions I'm still confused how to do that. I'm trying to do what you're saying by having the user-defined function do everything and all the main has to do is call the function and store the returning value in the "rate" variable declared in my main. I can get my if statement to work in the user function to say for example if user inputs pay rate > 200 then print to the screen "invalid number", but then nothing else in my program will run after that.

I don't know i might just have to start over and do my if statements while i write the user defined functions. If i want a rate to be between 5.50 and 200.00 should i set it up like this...
if (5.50 < rate > 200.00)
{
  cout << "Invalid number";
}
else


Was This Post Helpful? 0
  • +
  • -

#26 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Re: User Defined Function help! c++

Posted 29 April 2012 - 05:46 PM

Ok i've been working on it some more and i've gotten my GetRate and GetHrs function to work, but i want to get my GetPay function to work as well and was wondering if i should use reference variables my GetRate and GetHrs functions so i can use those values in my GetPay function.
Was This Post Helpful? 0
  • +
  • -

#27 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: User Defined Function help! c++

Posted 29 April 2012 - 06:05 PM

If we're talking about the variables hours and rate, I assume they're declared in main, yes? Then main can pass them to GetPay by value - just ordinary double or float. They don't have to be passed to GetPay as references (e.g., double&) because GetPay only uses those values, it shouldn't modify them. pay, on the other hand, should either be passed to GetPay by reference, or GetPay can send it to main as a return value which you can assign to a variable in main, e.g.,
pay = GetPay(rate, hours);

Was This Post Helpful? 0
  • +
  • -

#28 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Re: User Defined Function help! c++

Posted 29 April 2012 - 06:07 PM

I changed up my program and it runs fine, but if one of my conditions are broken the program won't stop. I'm not sure why or how else to change the condition. If anyone knows please tell me why the program continues to do this and how to fix this problem. Thanks

#include <iostream>
#include <iomanip>

using namespace std;

//user defined functions
float GetRate()
{
    float rate;
    cout << "Hourly Rate? ";
    cin>> rate;
    
    if (rate > 200 || rate < 5.50)
    {
        cout << "invalid";
    }
    else
    {
        
    }
    
    return (rate);
}
/****************************************************/

float GetHrs()
{
    float hours;
    cout << "Hours worked? ";
    cin >> hours;
    
    if (hours < 0 || hours > 60)
    {
        cout << "invalid hours" <<endl;
    }
    else
    {
        
    }
}

/****************************************************/
float GetPay (float r, float h)
{
    float p;
    p = r * h;
    
    if (p > 10000)
    {
        cout << "out of normal range";
    }
    else
    {
        
    }
    
    return(p);
}

/*************************************************/

float CalcFICA(float a, float B)/>
{
    float f;
    f = a * b;
    
    return (f);
}

/*************************************************/

float CalcFedTax(float c, float d)
{
    float e;
    e = c * d;
    
    return (e);
}

/*************************************************/

float CalcStateTax(float y, float z)
{
    float h;
    h = y * z;
    
    return (h);
}



/*
 * 
 */
int main() 
{
    float real_rate;
    float real_hours;
    float pay;
    string name;
    
    //get user name
    cout << "Employee name? ";
    cin>>name;
    
    
    //call rate function and print to screen
    real_rate = GetRate();
    
    //call hours function and print to screen
    real_hours = GetHrs();
    
    //call pay function and print to screen
    pay = GetPay(real_hours, real_rate);
    cout << name << " earned " << " $" << pay << endl;
    
    //calculate FICA and print to screen
    float ficaRate=0.0765, ficaAmt;
    ficaAmt = CalcFICA(ficaRate, pay);
    cout << "FICA" << " $" << ficaAmt << endl;
    
    
    //calculate Federal Tax and print to screen
    float fedRate=0.22, fedAmt;
    fedAmt = CalcFedTax(fedRate, pay);
    cout << "Fed"<<" $"<<fedAmt<<endl;
    
    
    //calculate State Tax and print to screen
    float taxRate=0.12, stateAmt;
    stateAmt = CalcStateTax(taxRate, pay);
    cout << "State" << " $" <<stateAmt;

    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#29 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: User Defined Function help! c++

Posted 29 April 2012 - 06:12 PM

Do you know where the error is? Can you determine what the last thing was that it did before going "wild"? You should be able to figure that out based on its output.

Does your compiler give you any warning messages when you compile? If not, which compiler are you using?
Was This Post Helpful? 0
  • +
  • -

#30 Houston573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 14-April 12

Re: User Defined Function help! c++

Posted 29 April 2012 - 06:21 PM

View Postr.stiltskin, on 29 April 2012 - 06:12 PM, said:

Do you know where the error is? Can you determine what the last thing was that it did before going "wild"? You should be able to figure that out based on its output.

Does your compiler give you any warning messages when you compile? If not, which compiler are you using?


I don't know where the error is exactly...I was thinking it could be in my if statement where the GetRate and GetHrs functions are. I set it up to say if the rate entered is too much or too little then print to screen saying "invalid",but it does that except the program keeps running.

Is my if statement ok the way it's set up?

I'm using netbeans and no there is no warning indicators
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3