We i compile this program is shows that percents as 0.... any suggestions.

#include <stdio.h>
#include "simpio.h"
#include "strlib.h"
#include "random.h"
#include <math.h>

int main()
{
	int g1, g2, g3, g4, w1, w2, w3, w4, p1, p2, p3, p4;

	string n1;
	string n2;
	string n3;
	string n4;

//-------------------------------------------//
	printf("Enter first name: ");
	n1 = GetLine();

	printf("Enter games played for %s: ", n1);
	g1 = GetInteger();

	printf("Enter games won for %s: ", n1);
	w1 = GetInteger();
//-------------------------------------------//
	printf("\n");
//-------------------------------------------//
	printf("Enter second name: ");
	n2 = GetLine();

	printf("Enter games played for %s: ", n2);
	g2 = GetInteger();

	printf("Enter games won for %s: ", n2);
	w2 = GetInteger();
//-------------------------------------------//
	printf("\n");
//-------------------------------------------//
	printf("Enter thrid name: ");
	n3 = GetLine();

	printf("Enter games played for %s: ", n3);
	g3 = GetInteger();

	printf("Enter games won for %s: ", n3);
	w3 = GetInteger();
//-------------------------------------------//
	printf("\n");
//-------------------------------------------//
	printf("Enter fourth name: ");
	n4 = GetLine();

	printf("Enter games played for %s: ", n4);
	g4 = GetInteger();

	printf("Enter games won for %s: ", n4);
	w4 = GetInteger();
//-------------------------------------------//
	printf("\n");
	printf("\n");
//-------------------------------------------//

//---------------Calculations----------------//
	p1 = w1/g2*100;
	p2 = w2/g2*100;
	p3 = w3/g3*100;
	p4 = w4/g4*100;
//-------------------------------------------//


	printf("Name		Games Played	Games Won	Winning %\n");
	printf("----		------------	---------	---------\n");
	printf("%s		%d		%d		%d\n", n1, g1, w1, p1);
	printf("%s		%d		%d		%d\n", n2, g2, w2, p2);
	printf("%s		%d		%d		%d\n", n3, g3, w3, p3);
	printf("%s		%d		%d		%d\n", n4, g4, w4, p4);
	system("pause");
}

Recommended Answers

All 4 Replies

You are loosing precision. For percentages you need to use floating points.

> p1 = w1/g2*100;
Should this be g1 ?

Also, try p1 = w1 * 100 / g1; to preserve the precision.

1. Follow Salem's advice (better use p1 = (w1 * 100) / g1 ).
2. Change the last single % in the header string literal to double %% (see printf format string specifications).
3. Better use tabs \t to adjust columns in all trailing printfs.

>>3. Better use tabs \t to adjust columns in all trailing printfs.
That can easily screw up the spacing. Better to use printf() correctly, such as printf("%-10.10s%15d%15d%15d\n", n1, g1, w1, p1); The above will insure consistent spacing

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.