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.

Dani AI

Generated

already solved the percentage calculation (integer division). The remaining alignment problem is purely formatting: the - flag only controls left-justification when paired with a field width. Also note that printf expects a C string for %s (use a.c_str()), while C++ streams work directly with std::string.

A compact printf approach that left-justifies fixed-width columns:

printf("%-15s %-14d %-12d %5.0f%%\n",
       name.c_str(), gamesPlayed, gamesWon, winPct);

Field widths (15, 14, 12, 5) control column alignment; - makes the text left-justified. The final %% prints a literal percent sign.

Using C++ iostreams with manipulators is often clearer and safer with std::string:

#include <iostream>
#include <iomanip>

std::cout << std::left
          << std::setw(15) << name
          << std::setw(14) << gamesPlayed
          << std::setw(12) << gamesWon
          << std::setw(6)  << std::fixed << std::setprecision(0) << winPct
          << '%' << '\n';

For variable-length names, compute a dynamic width from the longest name and use it (with printf the * width or with std::setw for streams). Example idea: determine nameWidth = max_length + 2 and use printf("%-*s", nameWidth, name.c_str()) or std::setw(nameWidth).

Additional notes: keep consistent column widths for the header (print headers with the same widths), guard against divide-by-zero when computing percentages, and avoid mixing printf with C++ strings unless .c_str() is used.

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.