Converting from Fahrenheit to Celsius and viceversa in C [duplicate] - c

This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 2 years ago.
I'm working on a program to convert F to C and vice-versa. The thing is, I've seen similar programs where you first enter if the conversion is from degree F to degree C or vice-versa, and then you Enter the data. I just want kind of a more user friendly version, where you Enter the data as "35F" and it gives you output.
So far I have basically %70 of the program done but I think I'm having problems when reading the Temperature and the character together in the same scanf(). I was wondering if it is possible to do that in just 1 line and if I'm aiming in the right directions.
Currently the program is running fine but the Output is always 0.000000 ):
I would love your help and thank your in advance!
float temperature, Farenheit, Celsius;
char unit0;
char unit1 = 'C';
char unit2 = 'F';
printf("Enter the temperature (Ex. 35F): ");
scanf("%f%c", &temperature, &unit0);
if (unit0 == unit2)
{
Celsius = (temperature-32) * (5/9) ;
printf("The temperature %f%c is equal to %f in Celsius", temperature, unit0, Celsius);
}
else
if (unit0 == unit1)
{
Farenheit = (9/5)*temperature+32;
printf("The temperature %f%c is equal to %f in Fahrenheit", temperature, unit0, Fahrenheit);
}

change (5/9) to 5.0/9.0. If you use integer division, this gives you 0 after rounding.
You should do the same for 9/5

Related

Why can't I define float before scanf (in this case)?

I am a beginner to coding trying to learn C. I tried to make a program which would convert celsius to fahrenheit. When I tried to run this code:
#include <stdio.h>
int main()
{
// Define celsius and fahrenheit.
float celsius;
float far = (celsius * 9 / 5) + 32;
// Ask for Celsius temperature
printf("What is the temperature in celsius?\n");
// Get the input of the temperature
scanf("%f", &celsius);
// Convert to Fahrenheit
//Print the temperature in Farenheit.
printf("The temperature converted to Farenheit is %f\n", far);
return 0;
}
It gives the output (no error / warning):
What is the temperature in celsius?
5
The temperature converted to Farenheit is -1172181139767515960282508306484822016.000000
Which is wrong as it should be 41. Also, every time I run this, I get a different temperature.
However, running this code (defining far after scanf), there are no issues:
#include <stdio.h>
int main()
{
// Define celsius
float celsius;
// Ask for Celsius temperature
printf("What is the temperature in celsius?\n");
// Get the input of the temperature
scanf("%f", &celsius);
// Convert to Fahrenheit
float far = (celsius * 9.0 / 5.0) + 32.0;
//Print the temperature in Farenheit.
printf("The temperature converted to Farenheit is %f\n", far);
return 0;
}
This is the output:
What is the temperature in celsius?
5
The temperature converted to Farenheit is 41.000000
Why is it so?
PS - Did I take a right decision by choosing C first? I chose C because I'd heard it makes a good foundation for other languages and also because I like Linux.
In the case
float celsius;
float far = (celsius * 9 / 5) + 32;
you're using celsius uninitialized. It's an automatic storage local scope variable, and unless initialized explicitly, contains indeterminate value.
If not anything, the result will be indeterminate.
However, once you store some value in celsius, and then use it, the proper value will be used and result will be as per expectation.
What else do you expect by the first code.
It's obvious that it will show you some or the other garbage value since the value of celcius is not initialized.
After scanf(), it is initialized so then it works fine.
The other code is absolutely fine.
Kindly use that one

Trouble going from Celsius to Fahrenheit

I just started learning C today so this might seem like a simple question but I'm having trouble running my program. I'm trying to implement a program that converts from Celsius to Fahrenheit. The formula for converting from Celsius to Fahrenheit is T(F)=T(C)*9/5+32. The degrees In Celsius is given as 30.
int main()
{
int DegreesCelsius;
printf("30 Degrees Celsius:");
scanf("%d",&30 );
printf("The temperature in F is %d\n", 30 * 9/5 + 32);
return 0;
}
My work is shown above. The problem is on the line containing scanf where I'm told "1value required as unary & operand"
Am I using the ampersand incorrectly?
Thanks
You need to scan the entered value into DegreesCelsius, and then use that variable to perform the conversion.
int DegreesCelsius;
printf("Degrees Celsius: ");
scanf("%d", &DegreesCelsius);
printf("The temperature in F is %d\n", DegreesCelsius * 9/5 + 32);
&30 is invalid syntax, as 30 is an integer literal and cannot be addressed by &; this is why you have DegreesCelsius, to hold the value the user enters.

5/9 is not taking by the C compiler to Calculate fc [duplicate]

This question already has answers here:
Division result is always zero [duplicate]
(4 answers)
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 5 years ago.
I'm stuck in a Code where I have to convert a temperature given by the user (in Fahrenheit) to degree Celsius. But unfortunately it's formula is not working with C Compiler.
#include<stdio.h>
#include<stdlib.h>
void c_f();
//void f_c();
float c,f,fc,fc1;
int main()
{
c_f();
// f_c();
return 0;
getch();
}
void c_f()
{
printf("\n Enter the temperature (in *F) to covert it into Celsius: ");
scanf("%d",&f);
fc=((5/9)*(f-32));
printf("\n %f*C",fc);
}
fc=((5/9)*(f-32));
Because 5 and 9 are integers, the arithmetic calculation is done at integer level, thus 5/9 == 0.
You should change it to floating point:
fc = ((5.0f / 9.0f) * (f - 32.0f));
Besides, format specifier %d is used for integers. If you want to read a floating point number, use %f:
scanf("%f", &f);
The result should be right now.
You have to use the correct format specifier which will be scanf("%f",&f) in this case.
Also fc=((5.0/9.0)*(f-32)), otherwise integer division yields 0.(Integer division truncates).5/9.0 will also work.
It is useless to put getch() after return statement. It will never reach upto that line.
What happens in your version?
Actually when two integers are divided the result is the quotient. If there any fractional part it is discarded. If you divide 5 by 9 the result will be 0.555.... When fractional part discarded it will be 0. So you always get 0.
Whenever you need an outcome of a division not t truncate you must make one of them floating point. That ensures that it(result) will not truncate.
So the program would be
#include<stdio.h>
#include<stdlib.h>
void celciusToFahreinheit();
float fahr_temp,celc_temp;
int main()
{
celciusToFahreinheit();
return EXIT_SUCCESS;
}
void celciusToFahreinheit()
{
printf("\n Enter the temperature (in *F) to covert it into Celsius: ");
if( scanf("%f",&fahr_temp) != 1 ){
fprintf(stderr,"Error in input\n");
exit(EXIT_FAILURE);
}
celc_temp=((5.0/9.0)*(fahr_temp-32));
printf("\n %f*C",celc_temp);
}
Apart from the problem few things would be - Using readable function name and checking the return value of scanf.

error: called object '0' is not a function [duplicate]

This question already has answers here:
Why am I getting a 'called object 0 is not a function error'?
(4 answers)
Closed 9 years ago.
I'm running into the following error in both functions in the following code:
In function 'Celsius_Converter':
Line 64: error: called object '0' is not a function
In function 'Fahrenheit_Converter':
Line 90: error: called object '1' is not a function
Here is my code:
/*
*Program Name: COP 2220-10018 Project 3
*
* Author: Nathan Gamble
*
* Description: Convert either Celsius to Fahrenheit, or Fahrenheit to Celsius.
*
* Input: Celsius or Fahrenheit Temperature
*
* Output: Fahrenheit or Celsius, depending on which one the User requested.
*/
#include <stdio.h>
#include <math.h>
int main (void)
{
//Local Declarations
char choice;
int fahrenheit;
int celsius;
//Statements
printf("This program converts Celsius temperature to Fahrenheit degree and Fahrenheit temperature to Celsius degree. \n");
printf("If you want to convert Celsius to Fahrenheit, please enter C.");
printf("If you want to convert Fahrenheit to Celsius, please enter F.");
scanf("%s", &choice);
//Process
if (choice == 'C') {
fahrenheit = Celsius_Converter();
printf("The temperature in Fahrenheit degree is %d", &fahrenheit);
}
else {
celsius = Fahreinheit_Converter();
printf("The temperature in Celsius degree is %d", &celsius);
}
return 0;
} // End main
/*
*Program Name: COP 2220-10018 Project 3
*
* Author: Nathan Gamble
*
* Description: Convert Celsius to Fahrenheit
*
* Input: Celsius Temperature
*
* Output: Fahrenheit
*/
int Celsius_Converter ()
{
//Local Declarations
double fahrenheit;
double celsius;
printf("Enter a temperature in Celsius.");
scanf("%d", &fahrenheit);
celsius = (5/9) (&fahrenheit - 32);
return celsius;
} //end Celsius_Converter
/*
*Program Name: COP 2220-10018 Project 3
*
* Author: Nathan Gamble
*
* Description: Convert Fahrenheit to Celsius
*
* Input: Fahrenheit Temperature
*
* Output: Celsius
*/
int Fahrenheit_Converter ()
{
//Local Declarations
double fahrenheit;
double celsius;
printf("Enter a temperature in Fahrenheit.");
scanf("%d", &celsius);
fahrenheit = (9/5) (&celsius + 32);
return fahrenheit;
} //end Fahrenheit_Converter
You have a few problems here:
You're using the address of some values instead of the values theselves. You use the address of celsius when you call scanf (which is right, because scanf needs to know where to store the value):
double celsius;
scanf("%d", &celsius);
but you need to celsius itself when you do arithmetic, not its address:
fahrenheit = (9/5) (celsius + 32); // instead of &celsius
There's a similar error in the other conversion routine, where you use &fahrenheit rather than fahrenheit. This is also an issue in your output. When you're printing, you need to use the value of the variable, not its address. So
printf("The temperature in Celsius degree is %d", &celsius);
should also use celsius, not &celsius.
Once you've got those worked out, though, you can address your main issue.
something (...)
is function call syntax in C. This means that when you do, e.g., (9/5)(celsius + 32), you're trying to call a function (9/5), which is, by integer arithmetic, 0. So, you need to make the multiplication explicit, using *, so you'd have
(9/5)*(celsius+32)
but then you'll still run into the problem that 9/5 is 1, because it's integer arithmetic. You can fix this by making at least one of those numbers a non-integer, e.g., by using (9/5.0):
fahrenheit = (9/5.0) * (celsius + 32);
A similar explanation holds for the other case, except that 5/9 is 0.
The return types of some of your methods aren't right. E.g., since you're trying to return a double, the function needs to be declared to return a double, not an int.
int Celsius_Converter () // needs to be double
{
double celsius;
...
celsius = (5/9) (&fahrenheit - 32);
return celsius;
} //end Celsius_Converter
First, the answer to your question:
(9/5) (&celsius + 32) is a function invocation, more precisely, you are trying to invoke the function 9/5, which is 1, passing &celsius + 32 (which is an error) as its parameter.
What you want to do is probably (9/5) * (&celsius + 32). There is no such thing as implicit operators in C. Actually, it should be (9/5) * (celsius + 32): the & is wrong here.
The same goes for the other function.
Then, some other considerations on your code:
scanf("%s", &choice); is a hazard, because you are asking for buffer overruns. You'd better do scanf("%c", &choice); or
char choice[2];
...
scanf("%1s", choice);
printf("The temperature in Fahrenheit degree is %d", &fahrenheit); is wrong. It should be printf("The temperature in Fahrenheit degree is %d", fahrenheit);. The same goes for Celsius. The & is needed for scanfs, when you're reading into a primitive type (you'll learn the reason behind this as you go further with your studies, or you can post another question, or check a online C guide).
Moreover, I have some doubts on the rationale behind your decomposition in functions, but, again, this is something you don't learn now.

Program to Convert Fahrenheit to Celsius

I'm trying to convert temperature given in Fahrenheit to Celsius. But for some reason its not working properly. I know that a similar question has been asked but my problem is different because I can't even printout what I have scanned (using scanf) from the user.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
float Fahrenheit, Celsius;
clrscr();
printf("Enter Temperature in Fahrenheit \n");
scanf("%f",&Fahrenheit);
Celsius = 5.0/9.0 * (Fahrenheit-32);
printf("\n Temperature in Fahrenheit = %f", Fahrenheit);
printf("\n Temperature in Celsius = %f", Celsius);
getch();
}
Output :
I'm using Windows 7 - 64 bit. IDE = Emulated C++ 3.0
I'm not sure (because your compiler might behave differently) but perhaps it's caused by this:
You're using 5.0 and 9.0 which are double values as well as 32 which is an int.
Try changing them to 5.0f, 9.0f and 32.0f.
It seems to be a compiler issue. My compiler (Emulated Turbo C++ 3.0) was not able to save my edits properly. So I went to C:\TC\Bin\ filename.c and opened the file in Notepad. Corrected the errors and compiled it again.
Now it works :)

Resources