Function division problem. Need HELP!!
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Function division problem. Need HELP!!

1. Junior Member
Join Date
Oct 2013
Posts
1

## Function division problem. Need HELP!!

hello im hoping i can receive some insight on my current problem i was required to write a program that takes a baseball players statistics and displays there averages. i was required to make 3 function in the file to perform this tasks. my problem i am having a division problem in the SLG function and for the life of me cannot figure out what i have done wrong the two functions above it are working flawlessly. i hope one of you can help me out thank you.My compiler does not require the system ("PAUSE"); command.
OUTPUT
The player's batting average is: 0.347
The player's on-base percentage is: 0.375
The player's slugging percentage is:
(test)AB = 101
(test)Tot Base = 58
0.000

Code:
```/* Batting Average Program
file:  batavg1CPP.cpp

Glossary of abbreviations:

BA = batting average
PA = plate appearances
H = hits
BB = bases on balls (walks)
HBP = times hit by pitch
S = sacrifices
AB = at bats
_1B = singles
_2B = doubles
_3B = triples
HR = home runs

*/

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

//function prototypes
double BA(int PA, int _1B, int _2B, int _3B, int HR, int S, int BB, int HBP);
double OBP(int PA, int _1B, int _2B, int _3B, int HR, int BB, int HBP);
double SLG(int PA, int _1B, int _2B, int _3B, int HR, int S, int BB, int HBP);

using namespace std;

int main ()
{
int _1B, _2B, _3B, HR, PA, BB, HBP, S;

/*In this example the actual parameter names match the formal parameter names.
This doesn't have to be the case */

cout<<"Enter a player's plate appearances: ";
cin>>PA;

cout<<endl;

cout<<"Enter the number of singles: ";
cin>>_1B;

cout<<endl;

cout<<"Enter the number of doubles: ";
cin>>_2B;

cout<<endl;

cout<<"Enter the number of triples: ";
cin>>_3B;

cout<<endl;

cout<<"Enter the number of home runs: ";
cin>>HR;

cout<<endl;

cout<<"Enter the number of sacrifices: ";
cin>>S;

cout<<endl;

cout<<"Enter the number of walks: ";
cin>>BB;

cout<<endl;

cout<<"Enter the number of times hit by pitch: ";
cin>>HBP;

cout<<endl;

cout<<fixed<<setprecision(3)<<"The player's batting average is: "
<<BA(PA, _1B, _2B, _3B, HR, S, BB, HBP)<<endl
<<"The player's on-base percentage is: "
<<OBP( PA, _1B, _2B, _3B, HR, BB, HBP)<<endl
<<"The player's slugging percentage is: "
<<SLG( PA, _1B, _2B, _3B, HR, S, BB, HBP)<<endl;

cout<<endl<<endl;

return 0;
}

/*----------------------------------------------------------------------------*/

double BA(int PA, int _1B, int _2B, int _3B, int HR, int S,
int BB, int HBP)
{
int AB, H;
double avg;
H = _1B + _2B + _3B + HR;
AB = PA - (BB + HBP + S);
avg = static_cast<double>(H)/AB;
return avg;
}

double OBP(int PA, int _1B, int _2B, int _3B, int HR, int BB, int HBP)
{
int H;
double OBP;
H = _1B + _2B + _3B + HR;
OBP = (H + BB + HBP) / static_cast<double>(PA);
return OBP;
}

double SLG(int PA, int _1B, int _2B, int _3B, int HR, int S, int BB, int HBP)
{
int Tot_base, AB, SLG, x;
AB = PA - (BB + HBP + S);
cout<<endl<<"AB = "<< AB <<endl;     // error testing
Tot_base = (HR*4)+(_3B*3)+(_2B*2)+_1B;
cout<<"Tot Base = "<<Tot_base <<endl;// error testing
SLG = static_cast<double>(Tot_base) / AB;
return SLG;
}```

2. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: Function division problem. Need HELP!!

Originally Posted by StrayKiller
my problem i am having a division problem in the SLG function and for the life of me cannot figure out what i have done wrong
Use the debugger that comes with your compiler.
the two functions above it are working flawlessly.
Well, learning how to fix problems is part of learning how to program. You wrote the function and it doesn't give you the correct results, so which one of those variables in that SLG function is incorrect? That is what the debugger is used for.

Also:

1) Drop the usage of the variables with leading underscores. In C++, identifiers with leading underscores are reserved for the compiler library usage. Use variable and function names that are both descriptive (not these two or three letter names), and do not start these names with underscores. For example, call your function "BattingAverage", not "BA". Doing that alone can reveal mistakes.

2) You don't check whether you are dividing by 0 in any of the calculations.

Regards,

Paul McKenzie
Last edited by Paul McKenzie; October 10th, 2013 at 03:30 PM.

3. ## Re: Function division problem. Need HELP!!

Code:
```double SLG(int PA, int _1B, int _2B, int _3B, int HR, int S, int BB, int HBP)
{
int Tot_base, AB, SLG, x;
AB = PA - (BB + HBP + S);
cout<<endl<<"AB = "<< AB <<endl;     // error testing
Tot_base = (HR*4)+(_3B*3)+(_2B*2)+_1B;
cout<<"Tot Base = "<<Tot_base <<endl;// error testing
SLG = static_cast<double>(Tot_base) / AB;
return SLG;
}```
It is also not good practice to have a variable with the same name as a function.

4. Elite Member Power Poster
Join Date
Apr 2000
Location
Belgium (Europe)
Posts
4,626

## Re: Function division problem. Need HELP!!

it's also not a good idea to abbreviate variable and function names (even if you document them at the top of the source).

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•