Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
This is my code

#include <stdio.h>  
float fahrenheit;
void temperature_convertor(float Fahrenheit)
{
	float C = Fahrenheit - 32;
    float Ce = C * 5;
    float Celsius =  Ce/9;
    float Kelvin =  Celsius + 273.15f;
    printf("Fahrenheit     Celsius     Kelvin \n---------------------------------------------\n%0.0f          %0.2f           %0.2f\n", Fahrenheit, Celsius, Kelvin);
}


linked to this other one

#include <stdio.h>  // printf

void temperature_convertor(float Fahrenheit);

int main(void)
{
	// test case 1
	temperature_convertor(0);
	printf("\n");

	// test case 2
	temperature_convertor(20);
	printf("\n");

	// test case 3
	temperature_convertor(300);
	printf("\n");

	// test case 4
	temperature_convertor(-20);
	printf("\n");

	// test case 5
	temperature_convertor(137);
	printf("\n");

	// test case 6
	temperature_convertor(-300);
	printf("\n");

	return 0;
}


I'm trying to get the numbers and letters to align in the output, ie I want all celsius values to form a neat line before the word "celsius", the problem is that all the numerical results are slightly different and I end up with a janky line. Any help?

What I have tried:

I tried leaving more spacing, but other than that I'm kinda lost.
Posted
Updated 27-Sep-21 4:47am

You didn't specify enough room for the numbers (see, for instance: printf - C++ Reference[^]).

Replace
Quote:
printf("Fahrenheit Celsius Kelvin \n---------------------------------------------\n%0.0f %0.2f %0.2f\n", Fahrenheit, Celsius, Kelvin);
with
printf("Fahrenheit     Celsius     Kelvin \n---------------------------------------------\n      %4.0f     %7.2f    %7.2f\n", Fahrenheit, Celsius, Kelvin);
 
Share this answer
 
Quote:
I tried leaving more spacing, but other than that I'm kinda lost.

What about reading the printf formatting documentation ?
printf - C++ Reference[^]
As OG already suggested in your previous question.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900