Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have two codes that are linked. qdriver:

#include <stdio.h>  // printf

void temperature_convertor(int 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;
}


and q.c

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


I need Celsius and Kelvin to be floats since I need to display two decimal places but Fahrenheit to display no decimals, and I can't edit qdriver so changing Fahrenheit to anything but an int is out of the question? How can I accomplish this?

What I have tried:

Honestly I'm very new to programming so I'm completely lost.
Posted
Updated 27-Sep-21 9:09am
v2

There are lots of ways. Probably the simplest is to tell the compiler to cast Fahrenheit to int e.g.
C
printf("Fahrenheit = %d\n", (int)Fahrenheit);
The (int) is known as a cast and tells the compiler to convert the given type to the requested types. Alternatively you could assign the float value to an int e.g.
C
int Fahrenheit_int = Farenheit;
printf("Farenheit = %d\n", Fahrenheit_int);
Be aware that the conversion from floating point to int truncates the value so that (int)32.2 = 32, and (int)32.99 = 32. You might want to think about how you could get the int value of the value to be the closes int rather than the int closest to zero.

There are other possibilities too, you could look into the round() function in the C library, but that might cause linking issues, since it may be part of the math library.
 
Share this answer
 
Comments
CPallini 28-Sep-21 3:04am    
5.
It's important to note exactly where you want the integer value. It appears to me to be solely for display purposes so you don't need to convert the data. You just need to display it appropriately. I refer you to the documentation : printf - C++ Reference[^]. For this, you do not want any decimal places so use a .0 in the format specifier. If what you want the numbers to all line up in nice columns then you can specify the width of each field. For integer Fahrenheit values the largest value is going to have four digits. To display four digits of a floating point value with no decimal places the format specifier will be "%4.0f". To show two decimal places with the four digit value then the format specifier will be "%7.2f". The total width is seven because you have to account for the decimal point.
 
Share this answer
 
Comments
CPallini 28-Sep-21 3:05am    
5.
We have explained this already at How do I make it so that the output is an integer or with less decimal places?[^] and How do I make the numbers and text align?[^]. You need to look more closely at the solutions you have been given.
 
Share this answer
 
Comments
CPallini 28-Sep-21 3:05am    
Indeed.
5.

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