User Defined Function help! c++

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

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

#1 Houston573   User is offline

  • New D.I.C Head

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

User Defined Function help! c++

Posted 25 April 2012 - 12:54 PM

So i'm trying to make a c++ program to receive user input and to take that data and calculate all the taxes and finally the net pay with user defined functions. However, this is my firs time with user defined functions and my screen keeps showing errors and i'm not sure why or how to fix them. I'm only in the beginning,but if anyone can help me i'd greatly appreciate it.

#include <iostream>
#include <string>

using namespace std;

//Function Prototypes

void GetName(string);
void GetPay(float);
void GetHrs(float);
void GetRate(float);
void PrintStub();
void CalcFICA();
void CalcFedTax();
void CalcStateTax();



/*
 * 
 */
int main() 
{
    //declare variables
    
    string name;
    float earnings;
    float hours;
    float rate;
    float fica;
    float state;
    float fed;
    float net;
    
    
    //Get input data
     GetName(string name)
    {
        cout << "Employee name? " << endl;
        cin >> name;
    }
     
     GetRate(float rate)
     {
         cout << "Hourly Rate? " << endl;
         cin >> rate;
     }
    
    
    
    

    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: User Defined Function help! c++

#2 jimblumberg   User is offline

  • member icon

Reputation: 5810
  • View blog
  • Posts: 17,740
  • Joined: 25-December 09

Re: User Defined Function help! c++

Posted 25 April 2012 - 01:02 PM

Post your complete error messages exactly as they appear in your development environment.

But you can't define a function within another function. Move your function implemtation code to outside any other functions. You may want to study the links in my signature, they should help you understand functions.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 Houston573   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 25 April 2012 - 01:08 PM

View Postjimblumberg, on 25 April 2012 - 01:02 PM, said:

Post your complete error messages exactly as they appear in your development environment.

But you can't define a function within another function. Move your function implemtation code to outside any other functions. You may want to study the links in my signature, they should help you understand functions.

Jim



Ok i will take a look at the links here in a little bit when i get a chance. But what do you mean "define a function within another function"? I had no idea i was doing this. This is how my book had it set up except the book kept the "void" part on the outside of the function name after the function prototypes,but when i kept it on, the program ran errors as well. I might have to take a screen shot to show the errors, but the errors popped up on the first curly brace "{" after the function where i get the user input.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5810
  • View blog
  • Posts: 17,740
  • Joined: 25-December 09

Re: User Defined Function help! c++

Posted 25 April 2012 - 01:14 PM

Look at your code that you posted, starting at line 37 you start implementing a function. But this code is inside the main() function. The main function ends on line 54 with it's closing brace. You can not implement these functions inside another function. Move lines 37 - 47 to below the closing brace of main.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 Houston573   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 25 April 2012 - 02:14 PM

Now my code is set up like that but i had to put the void back behind the user defined functions or it wouldn't run. Should i set it up like that? It runs with the void's behind the user-defined functions, but the program prints nothing to the screen.

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

using namespace std;

//Function Prototypes

void GetName(string);
void GetPay(float);
void GetHrs(float);
void GetRate(float);
void PrintStub();
void CalcFICA();
void CalcFedTax();
void CalcStateTax();



/********
 * 
 */
int main() 
{
    //declare variables
    
    string name;
    float earnings;
    float hours;
    float rate;
    float fica;
    float state;
    float fed;
    float net;
    
    
      
    

    return 0;
}

/****************************************************************/
void GetName(string name)
    {
        cout << "Employee name? " << endl;
        cin >> name;
    }
     
/****************************************************************/
void GetRate(float rate)
     {
         cout << "Hourly Rate? " << endl;
         cin >> rate;
     }



Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is offline

  • member icon

Reputation: 5810
  • View blog
  • Posts: 17,740
  • Joined: 25-December 09

Re: User Defined Function help! c++

Posted 25 April 2012 - 02:25 PM

I don't really understand what you mean about void back behind the functions. But your function definition must match your function implementation with reguards to both the type and number of parameters, as well as the return type. If you want a function that does not return any value then you use void. Example:
#include <iostream>
using namespace std;
void yourFunction(); //  This defines a function that does not return a value, taking no parameters.
int yourFunction2(); // This defines a function that returns an int, taking no parameters.

int main()
{
   yourFunction(); // Call the function.
   int value = yourFunction2(); // Call the second function.
   cout << "The value returned from the function is: " << value << endl;
   return 0;
}
void yourFunction()
{
   // This is the function implementation.
}

int yourFunction2()
{
   // The other function that returns a value.
   return 100;
}



Jim
Was This Post Helpful? 0
  • +
  • -

#7 Houston573   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 25 April 2012 - 02:37 PM

View Postjimblumberg, on 25 April 2012 - 02:25 PM, said:

I don't really understand what you mean about void back behind the functions. But your function definition must match your function implementation with reguards to both the type and number of parameters, as well as the return type. If you want a function that does not return any value then you use void. Example:
#include <iostream>
using namespace std;
void yourFunction(); //  This defines a function that does not return a value, taking no parameters.
int yourFunction2(); // This defines a function that returns an int, taking no parameters.

int main()
{
   yourFunction(); // Call the function.
   int value = yourFunction2(); // Call the second function.
   cout << "The value returned from the function is: " << value << endl;
   return 0;
}
void yourFunction()
{
   // This is the function implementation.
}

int yourFunction2()
{
   // The other function that returns a value.
   return 100;
}



Jim


Right here all i want the program to do is print to the screen "employee name?" and then store whatever the user inputs into the name variable
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//Function Prototypes

void GetName(string);
void GetPay(float);
void GetHrs(float);
void GetRate(float);
void PrintStub();
void CalcFICA();
void CalcFedTax();
void CalcStateTax();



/********
 * 
 */
int main() 
{
    //declare variables
    
    string name;
    float earnings;
    float hours;
    float rate;
    float fica;
    float state;
    float fed;
    float net;
    
    
      
    

    return 0;
}

/****************************************************************/
 GetName(string name)
    {
        cout << "Employee name? " << endl;
        cin >> name;
    }


Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is offline

  • member icon

Reputation: 5810
  • View blog
  • Posts: 17,740
  • Joined: 25-December 09

Re: User Defined Function help! c++

Posted 25 April 2012 - 02:46 PM

Look at your function prototype for GetName()? In that prototype what did you tell the compiler this function will return. This is your prototype/definition: void GetName(string); Does this prototype match the function implementation? Answer: No. But they must match. So the proper function implementation would be:
void GetName(string name)
    {
        cout << "Employee name? " << endl;
        cin >> name;
    }



The function prototype, function implementation, and function call must all agree as to the number and type of parameters and the return type of the prototype must match the function implementation.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 Houston573   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 25 April 2012 - 02:55 PM

Ok i see what you're saying and that makes sense, but i want the program to print to the screen and (correct me if i'm wrong) i think void tells the function to not do anything, right? I want my program to print to the screen so should i do this instead?

string GetName(string)



string GetName(string name)
{
  cout << "Employee name? " << endl;
  cin >> name;
}


Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg   User is offline

  • member icon

Reputation: 5810
  • View blog
  • Posts: 17,740
  • Joined: 25-December 09

Re: User Defined Function help! c++

Posted 25 April 2012 - 03:01 PM

No void is saying that this function will not return anything. It is not saying it isn't doing anything. Also if you tell the compiler you are going to return something, you must return something that matches what you told the compiler you would return. So if you only want to display the name you should use a void function.

Here is another tutorial about functions you should study thoroughly. Functions I
Jim

This post has been edited by jimblumberg: 25 April 2012 - 03:03 PM

Was This Post Helpful? 1
  • +
  • -

#11 Houston573   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 25 April 2012 - 06:34 PM

View Postjimblumberg, on 25 April 2012 - 03:01 PM, said:

No void is saying that this function will not return anything. It is not saying it isn't doing anything. Also if you tell the compiler you are going to return something, you must return something that matches what you told the compiler you would return. So if you only want to display the name you should use a void function.

Here is another tutorial about functions you should study thoroughly. Functions I
Jim



Ok so i've looked over the link you gave me which helped me out a little bit on understanding this user-defined function concept, but i'm still not comfortable with it.
I changed up my code and i'm only trying to get the program to ask "employee name" and then have the user input a valid name, but the part i'm stuck on is the local variables and not using the same variables in my main as in my user-defined functions. How do i put the data entered from the user as the employee name and print it next to "Employee name?"

void GetName()
    {
      cout << "Employee name? ";
       
    }

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

/********
 * 
 */
int main() 
{
    GetName();
  

    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#12 Houston573   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 25 April 2012 - 06:54 PM

Ok so i've gotten my program to print "Employee name?" and then have the user enter a name, but how do i keep going if i want to print all the information that i'll be getting from each of my user defined functions?
I tried this approach, but my program won't run and it doesn't tell me why.

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

using namespace std;

//Function Prototypes
void GetName(string& name);
void GetRate(float& rate);
void PrintStub(string& name, float& rate);



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

/********
 * 
 */
int main() 
{
    string name;
    float rate;
    
    
    PrintStub(name, rate);
    

    return 0;
}

/****************************************************************/
  void GetName(string& name)
    {
      cout << "Employee name? ";
      cin >> name;
    }
 
     
/****************************************************************/
  void GetRate(float& rate)
  {
      cout << "Employee rate? ";
      cin >> rate;
  }


Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg   User is offline

  • member icon

Reputation: 5810
  • View blog
  • Posts: 17,740
  • Joined: 25-December 09

Re: User Defined Function help! c++

Posted 25 April 2012 - 06:58 PM

You need to carefully study the links in my signature and these two links Functions I and Functions II. Just don't read the material, type the programs into your compiler, compile them and experiment with them. If you have a question about these tutorials, post the relevant code and ask specific questions about that code. These links should have everything you need to understand how to use basic functions.

Jim

This post has been edited by jimblumberg: 25 April 2012 - 06:58 PM

Was This Post Helpful? 1
  • +
  • -

#14 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: User Defined Function help! c++

Posted 25 April 2012 - 07:19 PM

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?)
Was This Post Helpful? 1
  • +
  • -

#15 Houston573   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:25 AM

I realized what i've been doing wrong for instance i wasn't supposed to write a user function to get the user input name and i studied those tutorials that you gave me up above and they're helping.

Right now i'm trying to create a function to get the total amount earned if i have the hours and the hourly rate inputted by the user. I'm using those tutorials as templates and trying to set my function similar to a subtraction function, but instead it would be multiplication the variables being multiplied would be what ever the user inputs.

here's what i have, but my program runs errors

#include <iostream>
#include <string>

using namespace std;

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

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

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

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

void 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 total, x=5, y=4;
    total = GetPay(x * y);
    cout << name << " earned " << total;
    cin >> pay;
    
    
            

    return 0;
}




One error here says "too few arguments to function 'void GetPay' "
void GetPay (float r, float h)
{
    float p;
    p = r * h;
    
    return(p);
}

And the other error says "error at this point in file"
[code]
total = GetPay(x * y);


[/code]
Was This Post Helpful? 0
  • +
  • -

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