Should be very easy to fix.

I have a program created in Visual Studio 2010. (I am a 10th Grade student at a technical school)

The problem with my program is that when it displays average, it is displayed as 186.00 instead of the actual correct result 186.67 (at least the calculator says 186.666666667)

Can you look at my code to fix it?

Thanks.

//PROGRAMMER NAME: Edward Reyes-Draper
//PROGRAM NAME: Bowling
//DATE CREATED: 3/10/2014
//PROGRAM PURPOSE: This program calculates information taken from the uesr to produce a table of the statistics of a bowling game.

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

void main()
{
char bowling_league [7], bowler_name [7]; //These statements declare variables for the program to use.
int game_1, game_2, game_3, total_pins;
float average;

cout << "Enter the Bowling League: "; //These statements asks the user for input and gets the input from the user.
cin >> bowling_league;
cout << "Enter the Bowler's Name: ";
cin >> bowler_name;
cout << "Enter the score for Game-1: ";
cin >> game_1;
cout << "Enter the score for Game-2: ";
cin >> game_2;
cout << "Enter the score for Game-3: ";
cin >> game_3;

cout << endl; //These statements create blank lines to give program an organized look.
cout << endl;

total_pins = game_1+game_2+game_3; //These statements provdide the program with mathematical expressions to display output.
average = total_pins / 3;

cout << "------------------------------" << endl; //These statements display the table using the information inputted from the user.
cout << "Bowling League: " << bowling_league << endl;
cout << "Bowler's Name: " << bowler_name << endl;
cout << "------------------------------" << endl;
cout << "Game-1: " << game_1 << endl;
cout << "Game-2: " << game_2 << endl;
cout << "Game-3: " << game_3 << endl;
cout << "------------------------------" << endl;
cout << "Total Pins: " << total_pins << endl;
cout << "Average: " << fixed << setprecision(2) << average << endl;
cout << "------------------------------" << endl;
cout << endl;
}

total_pins is an integer.

When you divide an integer with an integer, like you did with this average = total_pins / 3;, you get an integer.

To solve this, do one of the following:

1.) Make total_pins a float or double (not recommended)
2.) Divide by 3.0 or 3.0f
3.) Cast either of the two to a float or double
Last edited on
Topic archived. No new replies allowed.