'Printing many variables causes strange formatting
In the code below, when trying to print many variables at the same time, it causes strange formatting. Please check the what it print now at the end of the post and how it is supposed to look:
#include <stdio.h>
#include <stdlib.h>
#define NAME_LEN 20
#define N 2
struct submission{
int ID;
char title[NAME_LEN];
int rev1;
int rev2;
float total_score;
};
float final_score(int a, int b);
float final_score(int a, int b)
{
float z = (float)(a+b)/2;
return z;
}
int main(){
struct submission example[N];
int i;
for (i=0; i<N; i++)
{
printf("insert title\n",i+1);
fflush(stdin);
fgets(example[i].title, NAME_LEN, stdin);
printf("give number for rev1\n");
scanf("%d",&example[i].rev1);
printf("give number for rev2\n");
scanf("%d",&example[i].rev2);
example[i].ID = i+1;
example[i].total_score = final_score(example[i].rev1,example[i].rev2);
printf("total is: %0.3f\n\n",example[i].total_score);
}
for (i=0; i<N; i++)
{
printf("%d %s %0.2f\n",example[i].ID, example[i].title, example[i].total_score);
}
}
Example of the formatting, as it appears currently:
insert title
A cold morning //input
give number for rev1
2 //input
give number for rev2
3 //input
total is: 2.500
insert title
The locals //input
give number for rev1
3 //input
give number for rev2
3 //input
total is: 3.000
1 A cold morning
2.50
2 The locals
3.00
As you can see the formatting in the table above does not appear properly. It should look more like that:
1 A cold morning 2.50
2 The locals 3.00
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
