May 2021 2 129 Report
Weird number division in C?

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.


Please enter comments
Please enter your name.
Please enter the correct email address.
You must agree before submitting.

Answers & Comments




Helpful Social

Copyright © 2024 Q2A.MX - All rights reserved.