Output isn't working correctly.

I'm not sure what isn't going right, but no matter what numbers I input it out puts inf for my batting average, on base average, and slugging average. I cant figure out where the mistake is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
        
    float singles;
    float doubles;
    float triples;
    float homeRuns;

    float battingAvg = (singles+doubles+triples+homeRuns) / (atBats-walks);

    float onBase = (singles+doubles+triples+homeRuns+walks)/atBats;

    float slugging = (singles+(2* doubles)+(3*triples)+(4*homeRuns))/(atBats-walks);

    cout << endl << "Batter Stats" << endl << endl;

    cout << "How many at-bats did the batter have? ";
    cin >> atBats;
    cout << "How many walks? ";
    cin >> walks;
    cout << "How many singles? ";
    cin >> singles;
    cout << "How many doubles? ";
    cin >> doubles;
    cout << "How many triples? ";
    cin >> triples;
    cout << "How many home runs? ";
    cin >> homeRuns;
    cout << endl;

    cout << fixed << setprecision(3) << setw(26) << "Batting Average: "
    << battingAvg << endl;
    cout << fixed << setprecision(3) << setw(26) << "On-Base percentage: "
    << onBase << endl;
    cout << fixed << setprecision(3) << setw(26) << "Slugging percentage: "
    << slugging endl << endl;

    return 0;
}

You have the code out of sequence. You can't do any calculations such as finding the average until after the user has supplied the values.

Move lines 7 to 11 to around line 28 - that is after the end of the cin statements.

Topic archived. No new replies allowed.