How do I align columns in C Also How do I print a colon with
How do I align columns in C?
Also, How do I print a colon with a string ?
For example:
My output comes out like this:
Protein Donations: 0 Requests: 0
Dairy Donations: 0 Requests: 0
Grains Donations: 0 Requests: 0
Vegetables Donations: Requests: 0
Fruits Donations: 0 Requests: 0
When it should look like:
Protein: Donations: 0 Requests: 0
Dairy: Donations: 0 Requests: 0
Grains: Donations: 0 Requests: 0
Vegetables: Donations: 0 Requests: 0
Fruits: Donations: 0 Requests: 0
My code is:
printf(\"\\t%s Donations:%d Requests:%d\ \", TYPES[i],donation_amount[i], request_amount[i]);
It runs fine except it doesnt print the string if I use \"%s:\"
also there doesnt need to be a space in between each line. Chegg did that automatically
Solution
Dear Student,
You are printing values as-
printf(\"\\t%s Donations:%d Requests:%d\ \", TYPES[i],donation_amount[i], request_amount[i]);
instead of using the above format use format as below-do not use tab sign because tab gives system defined spaces after the character length that is why you are getting error.
printf(\"%-15s: Donations:%d Requests:%d\ \", TYPES[i], donation_amount[i],request_amount[i]);
what the above line does is described as follows-
%-15s ----- The hypen sign do the left alignment and 15 before s is the width of the column.
so first you have to decide the maximum width of the column as in your question the maximum width of the left elements is not exeeciding above 15 character. So i put 15 before s.
Now try this approach in your program you might get the right result.
####Kindly provide your valubale feedback if you are satisfied with the answer.####
