Below is my code:

#include "stdafx.h"
#include "strlib.h"
#include "simpio.h"
#include "genlib.h"

int _tmain(int argc, _TCHAR* argv[])
{
   int num1,nom1,num2,nom2,num3,nom3,num4,nom4;
   double nem1,nem2,nem3,nem4;
   string a,b,c,d;

   printf("Enter first name: ");
   a=GetLine();
   printf("Enter games played for %s: ",a);
   num1=GetInteger();
   printf("Games won for %s: ",a);
   nom1=GetInteger();
   nem1=(nom1/num1)*100;
   printf("\n");

   printf("Enter second name: ");
   b=GetLine();
   printf("Enter games played for %s: ",b);
   num2=GetInteger();
   printf("Games won for %s: ",b);
   nom2=GetInteger();
   nem2=(nom2/num2)*100;
   printf("\n");

   printf("Enter third name: ");
   c=GetLine();
   printf("Enter games played for %s: ",c);
   num3=GetInteger();
   printf("Games won for %s: ",c);
   nom3=GetInteger();
   nem3=(nom3/num3)*100;
   printf("\n");

   printf("Enter fourth name: ");
   d=GetLine();
   printf("Enter games played for %s: ",d);
   num4=GetInteger();
   printf("Games won for %s: ",d);
   nom4=GetInteger();
   nem4=(nom4/num4)*100;
   printf("\n");

   printf("Name         Games Played      Games Won      Winning %\n");
   printf("----         ------------      ---------      ---------\n");
   printf("%s           -%d               -%d            -%0.f%%      \n",a,num1,nom1,nem1);
   printf("%s           -%d               -%d            -%0.f%%      \n",b,num2,nom2,nem2);
   printf("%s           -%d               -%d            -%0.f%%      \n",c,num3,nom3,nem3);
   printf("%s           -%d               -%d            -%0.f%%      \n",d,num4,nom4,nem4);
}

-------------------------------------------------------------------------------------

The Winning column does not display the numbers properly. What's my mistake?
thank you.

Recommended Answers

All 3 Replies

Can you please send the output also ?

In line 18 for example when you have nem1 = (nom1/num1)*100; (more descriptive variable names might serve you better in the future) the nom1/num1 is an integer division so that something like 2/3 will yield 0, 3/2 would yield 1 etc (the decimal portion is tossed).

To alleviate this, cast one of the members of the ratio to a double. nem1 = ((double)nom1/num1)*100; OR nem1 = (nom1/(double)num1)*100;

How do I get all the columns to all align on the left side?
---------------------------------------------------------------------------------

I casted the ratio to a double, and see below:

#include "stdafx.h"
#include "strlib.h"
#include "simpio.h"
#include "genlib.h"

int _tmain(int argc, _TCHAR* argv[])
{
   int num1,nom1,num2,nom2,num3,nom3,num4,nom4;
   double nem1,nem2,nem3,nem4;
   string a,b,c,d;

   printf("Enter first name: ");
   a=GetLine();
   printf("Enter games played for %s: ",a);
   num1=GetInteger();
   printf("Games won for %s: ",a);
   nom1=GetInteger();
   nem1=((double)nom1/num1)*100;
   printf("\n");

   printf("Enter second name: ");
   b=GetLine();
   printf("Enter games played for %s: ",b);
   num2=GetInteger();
   printf("Games won for %s: ",b);
   nom2=GetInteger();
   nem2=((double)nom2/num2)*100;
   printf("\n");

   printf("Enter third name: ");
   c=GetLine();
   printf("Enter games played for %s: ",c);
   num3=GetInteger();
   printf("Games won for %s: ",c);
   nom3=GetInteger();
   nem3=((double)nom3/num3)*100;
   printf("\n");

   printf("Enter fourth name: ");
   d=GetLine();
   printf("Enter games played for %s: ",d);
   num4=GetInteger();
   printf("Games won for %s: ",d);
   nom4=GetInteger();
   nem4=((double)nom4/num4)*100;
   printf("\n");

   printf("Name         Games Played      Games Won      Winning %\n");
   printf("----         ------------      ---------      ---------\n");
   printf("%-s          %-d               %-d            %-.0f%%      \n",a,num1,nom1,nem1);
   printf("%-s          %-d               %-d            %-.0f%%      \n",b,num2,nom2,nem2);
   printf("%-s          %-d               %-d             %-.0f%%      \n",c,num3,nom3,nem3);
   printf("%-s          %-d               %-d             %-.0f%%      \n",d,num4,nom4,nem4);
}

---------------------------------------------------------------------------------
How do I get all the columns to all align on the left side in the output?

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.