I'm trying to print the percentage of the games a team one using floating point values, but i keep getting odd outputs like -0.0000 and some really bizarre huge numbers....
Code:
#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#include <string.h>
main()
{
printf("This program will show you the scores of the basketball games for 1 season.\n");
printf("What is the name of the basketball league? ");
string league = GetLine();
printf("How may games were played by the group? ");
int gamesplayed = GetInteger();
string teams[4];
float wonGames[4];
int a, b, c;
for (a = 0; a < 4; a++)
{
printf("What is team %d's name? ", a+1);
teams[a] = GetLine();
}
for (b = 0; b < 4; b++)
{
printf("How many times did team %s win? ", teams[b]);
wonGames[b] = GetInteger();
}
printf("\n\n ----===[%s]===----\n", league);
printf("Team Name | Games Played | Games Won | Percentage\n");
for(c = 0; c < 4; c++)
{
float percent = 100 * (wonGames[c] / gamesplayed);
printf("| %s | %d | %d | %f |\n", teams[c], gamesplayed, wonGames[c], percent);
}
}
Update:EDIT:
Simpio.h is just a library used to get input. The two functions im using in this program are GetLine() and GetInteger(). Getline gets a string and getinteger gets an int from user input.
Copyright © 2024 Q2A.MX - All rights reserved.
Answers & Comments
I obviously don't have your files "genlib.h" and "simpio.h"
but you'll get things like that if you'd inputs are bad... I don't have the definitions of the functions you're using to take in values but you should print the values after you input them to check if it's doing what it's supposed to.
your problem is here:
printf("| %s | %d | %d | %f |\n", teams[c], gamesplayed, wonGames[c], percent);
wonGames[] is a float you are using %d to print it
You should use %f to print a float..