-
October 10th, 2013, 11:26 AM
#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;
}
-
October 10th, 2013, 03:27 PM
#2
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.
-
October 10th, 2013, 03:41 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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 11th, 2013, 07:04 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|