• Advertisement
Sign in to follow this  

Problem with functions

This topic is 4345 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all. If one of you can help me with this I'll be indebted for life. I really want to become a programmer but am having some trouble understanding functions and getting them to work. For whatever reason the following program I've written isn't calculating correctly, and I can't figure out why for the life of me. Can someone help? The program is supposed to calculate a car salesman's paycheck based on his # of sales of a particular model plus commission. I've declared both the model of car and commission rate as global variables at the top of the program, as you can see. Everything compiles okay but the paycheck amount is way off for every input that is given. Can someone tell me or at least clue me in on what could be causing the calculation problem? Thanks!
#include <iostream>
#include <cstdlib>

using namespace std;

float experience();
float pay(float);
float commission();         
            
//Declare global variables for program
const float Autorust = 17000.00, Belchfire = 30000.00, Turbosmog = 49000.00;
const float AutorustComm = .03, BelchfireComm = .05, TurbosmogComm = .08;


int main()
{         
    //Declare variables for program input & output
    int AutorustNo, BelchfireNo, TurbosmogNo;
    float basePay, yearsExp, totalComm, paycheck;

    
    //Prompt user for input and perform calculations through functions
    experience();       
    commission();
    
    paycheck = basePay + totalComm;
    cout << fixed << showpoint << setprecision(2);
    cout << endl << "Paycheck amount is: " << "$" << paycheck << endl;
    system("pause");
    return 0;
}





float experience()
{
      float yearsExp;

      cout << "Enter the number of years of experience: ";
      cin >> yearsExp;
      
      pay(yearsExp);
}


float pay(float worked)
{
      float basePay;      
    
      if (worked >= 2)
         basePay = 25000.00;
      else
          basePay = 18000.00;

      return basePay;
}


float commission()
{
      int AutorustNo, BelchfireNo, TurbosmogNo;
      float totalComm;
      
      cout << endl << "Enter the number of Autorust models sold: ";
      cin >> AutorustNo;
      cout << endl << "Enter the number of Belchfire models sold: ";
      cin >> BelchfireNo;
      cout << endl << "Enter the number of Turbosmog models sold: ";
      cin >> TurbosmogNo;
       

      totalComm = (AutorustComm * AutorustNo * Autorust) + 
                  (BelchfireComm * BelchfireNo * Belchfire) +
                  (TurbosmogComm * TurbosmogNo * Turbosmog);
      
      return totalComm;
}

Share this post


Link to post
Share on other sites
Advertisement
Your basePay and totalComm variables inside the pay and commission functions respectively are "local" to those functions, and hence when you update them you only update the local copies. The variables of the same name in main() are completely separate, and hence never get updated.

You need to return a value from your experience function, and assign this to the variable in main, something like this:


#include <iostream>
#include <cstdlib>

using namespace std;

float experience();
float pay(float);
float commission();

//Declare global variables for program
const float Autorust = 17000.00, Belchfire = 30000.00, Turbosmog = 49000.00;
const float AutorustComm = .03, BelchfireComm = .05, TurbosmogComm = .08;


int main()
{
//Declare variables for program input & output
int AutorustNo, BelchfireNo, TurbosmogNo;
float basePay, yearsExp, totalComm, paycheck;


//Prompt user for input and perform calculations through functions
// --- Added assignments here
basePay = experience();
totalComm = commission();

paycheck = basePay + totalComm;
cout << fixed << showpoint << setprecision(2);
cout << endl << "Paycheck amount is: " << "$" << paycheck << endl;
system("pause");
return 0;
}





float experience()
{
float yearsExp;

cout << "Enter the number of years of experience: ";
cin >> yearsExp;

// --- Added return value here
return pay(yearsExp);
}


float pay(float worked)
{
float basePay;

if (worked >= 2)
basePay = 25000.00;
else
basePay = 18000.00;

return basePay;
}


float commission()
{
int AutorustNo, BelchfireNo, TurbosmogNo;
float totalComm;

cout << endl << "Enter the number of Autorust models sold: ";
cin >> AutorustNo;
cout << endl << "Enter the number of Belchfire models sold: ";
cin >> BelchfireNo;
cout << endl << "Enter the number of Turbosmog models sold: ";
cin >> TurbosmogNo;


totalComm = (AutorustComm * AutorustNo * Autorust) +
(BelchfireComm * BelchfireNo * Belchfire) +
(TurbosmogComm * TurbosmogNo * Turbosmog);

return totalComm;
}

Share this post


Link to post
Share on other sites
bakery2k1, you nailed it friend. I can't thank you enough; you'll get an excellent rating from me! It's such a great resource to have all of you on here when I need help.

Did anyone else have a little trouble with functions as a beginning programmer? I'm trying to gauge whether I can ever be productive in this field or not. My plan is to get into graphics eventually, and then games.

Share this post


Link to post
Share on other sites
When I was a beginner, I had trouble with everything. If you're a beginner, I'd advise you to stubbornly stick with it for at least a year or two, before trying to decide if you'd be good at it. It does get easier with constant practice, like most things.

Share this post


Link to post
Share on other sites
Thanks for that feedback, LastUnicron; it helps me feel a bit better. Sometimes I've wondered if I should be catching onto this stuff easier than I am.

Share this post


Link to post
Share on other sites
Well it wans't possible for me to run into that error, when using pascal all variables are global (Except those defined under a function/procedure but a beginner wouldn't know he could declare it in that local scope) and thus techinically this misunderstanding could not have occured!

Share this post


Link to post
Share on other sites
When i was first starting C++, everything was frustrating. Everything. It's like hiking uphill through a dense forest. You don't think you are making any progress until you reach a clear spot at the top and think "wow, you can see everything from up here!"

Share this post


Link to post
Share on other sites
It took me one year to be able to compile K&R Hello world example using MS QuickC 5.1 back then, on my dad's 8088 XT.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement