Reputation: 31
I'm trying to start my new program. The program will ask for n
employees from a company, then the name
, salary
and sale of the month
. The output will show the previous information plus the comission
, net salary
and a total
of each one of them.
For better understanding, this would be the output:
Name Salary Sale of the month Comission Net salary
Elena Gomez 2000.00 1000.00 35.00 2035.00
Elle Johns 4000.00 1345.00 60.53 4060.53
Stefan Cox 3200.00 4000.00 216.00 3416.00
Total 9200.00 6345.00 311.53 9511.53
My problem it's that my code doesn't align the same as the previous output. I'm using int
arrays and a char
multidimensional array for now. This is my code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define NUMBER_OF_STRING 6
#define MAX_STRING_SIZE 50
void print_array(const char arr[][MAX_STRING_SIZE]);
int main ()
{
int numemployees, cont=0;
float salary[10], salnet[5], sale[5], comission[5][1], ind=0, total[1][50];
char header[0], nom[5][40];
char arr[NUMBER_OF_STRING][MAX_STRING_SIZE] =
{ "Name",
"salary",
"Sale of the month",
"Comission",
"Net salary"
};
printf("Enter # of employees to consult: \n");
scanf("%d",&numemployees);
for(int i=0; i<=numemployees; i++)
{
printf("Enter employee's %d name : ",cont); // I'd like to show it from #1 not zero.
scanf("%s", nom[i]);
printf("Enter employee's %d salary : ",cont);
scanf("%f", &salary[i]);
printf("Enter sale of the month of the employee %d : ",cont);
scanf("%f", &sale[i]);
cont++;
system("cls");
}
print_array(arr); // function that shows the headers
for(int i=0; i<=numemployees; i++)
{
printf("%s %-25f %-25f\n",nom[i], salary[i], sale[i]);
}
return 0;
}
And the print_array
function:
void print_array(const char arr[NUMBER_OF_STRING][MAX_STRING_SIZE])
{
for (int i = 0; i < NUMBER_OF_STRING; i++)
{
printf("%-16s ", arr[i]);
if (i == 5)
{
printf("%-16s \n", arr[i]);
}
}
}
My output shows something like this:
Name salary Sale of the month Comission Net salary
pedro 60.000000 45.000000
I tried using printf
with %-25f
but it doesn't fixed it. Please check it out if there's something missing or another idea to get the correct output. Thanks in advance.
Upvotes: 1
Views: 44
Reputation: 359
First of all:
for(int i=0; i<=numemployees; i++)
{
printf("%s %-25f %-25f\n",nom[i], salary[i], sale[i]);
}
You should indicate all columns width like this:
for(int i=0; i<=numemployees; i++)
{
printf("%25s %25f %25f\n",nom[i], salary[i], sale[i]);
}
If you put "-" before the number of characters x, you use x (in your case 25) chars to represent the info but you will get the left-aligned text.
Upvotes: 1