Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem with functions

Started by F E A R Mar 29, 2006 at 10:00 AM 6 replies 1.4k views
Original Post
F E A R
F E A R
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;
}
bakery2k1
bakery2k1
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 programconst 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;}
F E A R
F E A R
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.
LastUnicron
LastUnicron
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.
F E A R
F E A R
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.
dawidjoubert
dawidjoubert
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!
----------------------------http://djoubert.co.uk
leiavoia
leiavoia
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!"
DerekSaw
DerekSaw
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.

"after many years of singularity, i'm still searching n the event horizon" o

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.