SRL Archive - C/C++ Help and Tutorials

C Data type help


Runescapian321

2008-03-01 14:01:47

I'm not sure if what I'm doing is not working because of my data types or because of something else, but either way I can't figure out how to get it to work :p

I have to write a program that gets 4 teams' names, games played, and games won. And the output has to be in a table that also includes the percentage of games won. That's where I'm having the problem - the percentage. Here is what I have so far

#include "stdafx.h"
#include "genlib.h"
#include "simpio.h"
#include <math.h>
#include "strlib.h"

int main()
{
string L1, L2, L3, L4;
int gp1,gp2,gp3,gp4,gw1,gw2,gw3,gw4;
float pw1,pw2,pw3,pw4;
printf("Enter first name: ");
L1=GetLine();
printf("Enter games played for %s: ",L1);
gp1=GetInteger();
printf("Enter games won for %s: ",L1);
gw1=GetInteger();
if (gw1>gp1)
{
printf("There cannot be more games won than played\n");
exit(0);
}
if (gp1<0)
{
printf("You cannot play or win less than zero games");
}
if (gw1<0)
{
printf("You cannot play or win less than zero games");
}
printf("\n");
printf("Enter second name: ");
L2=GetLine();
printf("Enter games played for %s: ",L2);
gp2=GetInteger();
printf("Enter games won for %s: ",L2);
gw2=GetInteger();
if (gw2>gp2)
{
printf("There cannot be more games won than played\n");
exit(0);
}
if (gp2<0)
{
printf("You cannot play or win less than zero games");
}
if (gw2<0)
{
printf("You cannot play or win less than zero games");
}
printf("\n");
printf("Enter third name: ");
L3=GetLine();
printf("Enter games played for %s: ",L3);
gp3=GetInteger();
printf("Enter games won for %s: ",L3);
gw3=GetInteger();
if (gw3>gp3)
{
printf("There cannot be more games won than played\n");
exit(0);
}
if (gp3<0)
{
printf("You cannot play or win less than zero games");
}
if (gw3<0)
{
printf("You cannot play or win less than zero games");
}
printf("\n");
printf("Enter fourth name: ");
L4=GetLine();
printf("Enter games played for %s: ",L4);
gp4=GetInteger();
printf("Enter games won for %s: ",L4);
gw4=GetInteger();
if (gw4>gp4)
{
printf("There cannot be more games won than played\n");
exit(0);
}
if (gp4<0)
{
printf("You cannot play or win less than zero games");
}
if (gw4<0)
{
printf("You cannot play or win less than zero games");
}
printf("\n");
printf("\n");
pw1=gw1/gp1*100;
pw2=gw2/gp2*100;
pw3=gw3/gp3*100;
pw4=gw4/gp4*100;
printf("%-13s %-15s %-11s %s%%\n", "Name", "Games Played", "Games Won", "Winning ");
printf("%-13s %-15s %-11s %s\n", "----", "------------", "---------", "---------");
printf("%-13s %-15d %-11d %f%%\n",L1,gp1,gw1,pw1);
printf("%-13s %-15d %-11d %f%%\n",L2,gp2,gw2,pw2);
printf("%-13s %-15d %-11d %f%%\n",L3,gp3,gw3,pw3);
printf("%-13s %-15d %-11d %f%%\n",L4,gp4,gw4,pw4);

}

It returns the right name, games played, and games won, but not the percentage of games won. It always returns 0% for that. Is it my data types? My calculations? Something else?

Thanks in advance :)

ShowerThoughts

2008-03-02 15:33:03

this is why i don't like this langauage its such a choas, but it's the best prog. language…

Runescapian321

2008-03-02 16:58:33

From: hermpie
this is why i don't like this langauage its such a choas, but it's the best prog. language…



IMO it seems easier than others (like C++, Java, PHP, etc), but maybe that's just because I've never even tried to learn them :p

mpd

2008-03-05 11:23:30

you're trying to do a calculation with integers and then assigning it to a float.
This wont work, since the types dont match

try the following, it'll work


pw1=(float)gw1/(float)gp1*100;
pw2=(float)gw2/(float)gp2*100;
pw3=(float)gw3/(float)gp3*100;
pw4=(float)gw4/(float)gp4*100;