I'm not looking for a direct answer or anything considering I'm doing this for homework, but I'm a little confused on why my batting average and slugging percentage do not work.

batAvg = singles + doubles + triples + homeRuns / atBats;
   slgPer = singles + 2* doubles + 3* triples + 4* homeRuns / atBats;

   cout << "Name                  AB  1B  2B  3B  HR    avg    slg" << endl;
   cout << "------------------   --- --- --- --- --- ------ ------" << endl;
   cout << setw(18) << left << plrName
        << setw(6) << right << atBats
        << setw(4) << singles
        << setw(4) << doubles
        << setw(4) << triples
        << setw(4) << homeRuns
        << setw(7) << batAvg
        << setw(7) << slgPer
        << endl;

   return 0;
}

It outputs it completely wrong...

Name                  AB  1B  2B  3B  HR    avg    slg
------------------   --- --- --- --- --- ------ ------
Jeremy               600 520  40  10  10    570      1

What can I do to get a real average or slugging percentage?

I have no idea about how baseball works, so I guess I can't really help you there, but you might want to use parentheses to ensure that the order of the operations is happening as expected. And make sure that any decimal outputs aren't being put into integer variables, as fractions will be chopped off in the conversion.

When explaining a problem, explain the problem. Simply saying "It outputs it completely wrong..." tells us nothing. Explain exactly why it's wrong and also add what the answer should be. Don't let us guess. batAvg = singles + doubles + triples + homeRuns / atBats; is wrong. Use parentheses because the compiler does not understand how you want to group your operations. What you have there is basically batAvg = (singles + doubles + triples) + (homeRuns / atBats); because of operator precedence. Always use parentheses so you and the compiler are never confused: batAvg = (singles + doubles + triples + homeRuns) / atBats);

Sorry, what I had meant is that it doesn't output into a decimal form. Using

batAvg = (singles + doubles + triples + homeRuns) / atBats);

does not compile:

main.cpp: In function `int main()':
main.cpp:57: error: expected `;' before ')' token

I entered

batAvg = (singles + doubles + triples + homeRuns) / (atBats);

and it output the same as before. I've searched and searched and just can't find a liable answer heh.
I think my best bet is to ask my professor tomorrow.

Here's the whole code to see if the mistake is elsewhere...

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

using namespace std;

// Program Description
// This program will input statistical data for one baseball player,
// calculate averages, and output the results

int main()
{

// Variables

   string plrName;  // the name of the player
   int atBats;      // the number of at-bats
   int singles;     // the number of singles hit
   int doubles;     // the number of doubles hit
   int triples;     // the number of triples hit
   int homeRuns;    // the number of homeruns hit
   float batAvg;    // batting average
   float slgPer;    // slugging percentage

// Describe the process to the user

   cout << "\n---------------" << endl;
   cout << "\nThis program will calculate statistical data for one ";
   cout << "baseball player," << endl;
   cout << "calculate averages, and output the results." << endl;

// Propt the reader and read the input

   cout << "\n---------------" << endl;
   cout << "Enter player name: ";
   getline(cin,plrName);
   cout << "Enter number of at bats:   ";
   cin >> atBats;
   cout << "Enter number of singles:   ";
   cin >> singles;
   cout << "Enter number of doubles:   ";
   cin >> doubles;
   cout << "Enter number of triples:   ";
   cin >> triples;
   cout << "Enter number of homeruns:  ";
   cin >> homeRuns;
   cout << "\n" << endl;

// Calculate averages and then output the results

   batAvg = (singles + doubles + triples + homeRuns) / (atBats);
   slgPer = (singles + 2* doubles + 3* triples + 4* homeRuns) / (atBats);

   cout << "Name                  AB  1B  2B  3B  HR    avg    slg" << endl;
   cout << "------------------   --- --- --- --- --- ------ ------" << endl;
   cout << setw(18) << left << plrName
        << setw(6) << right << atBats
        << setw(4) << singles
        << setw(4) << doubles
        << setw(4) << triples
        << setw(4) << homeRuns
        << setw(7) << batAvg
        << setw(7) << slgPer
        << endl;

   return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.