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.
Related
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
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
This program is supposed to convert degrees Fahrenheit into degrees Celsius:
#include <stdio.h>
int main() {
float fahrenheit, celsius;
int max, min, step;
max = 100;
min = 0;
step = 5;
fahrenheit = 0.0;
//celsius = (fahrenheit - 32.0) * 5.0/9.0; DOESN'T WORK HERE
printf("\n");
printf("This program converts fahrenheit into celsius \n");
while(fahrenheit <= max) {
celsius = (fahrenheit - 32.0) * 5.0/9.0; /* Works here */
printf("%3.0f %6.2f\n", fahrenheit, celsius);
fahrenheit = fahrenheit + step;
}
}
As I noted in my source comments, when I try put the formula for celsius into the body of the main() function, I get -17.8 for every single fahrenheit value. The output looks like this -
0 -17.78
5 -17.78
10 -17.78
15 -17.78
20 -17.78
25 -17.78
and so on and so on. However, when I put the celsius formula into the while() function, I get the correct celsius values for each fahrenheit value. It looks like this:
0 -17.78
5 -15.00
10 -12.22
15 -9.44
20 -6.67
Why does that happen?
Here's the code that doesn't work. It's identical to the code above, except for the location of the celsius formula. (At least, I think it is.)
#include <stdio.h>
//this program is supposed to convert fahrenheit into celsius
int main() {
float fahrenheit, celsius;
int max, min, step;
max = 100;
min = 0;
step = 5;
fahrenheit = 0.0;
celsius = (fahrenheit - 32.0) * 5.0/9.0;
printf("\n");
printf("This program converts fahrenheit into celsius \n");
while(fahrenheit <= max) {
printf("%3.0f %6.2f\n", fahrenheit, celsius);
fahrenheit = fahrenheit + step;
}
}
When you set a value to a variable, the actual value is computed, and then stored to the variable. You do not store a formula for the variable, however. Thus, when you run
celsius = (fahrenheit - 32.0) * 5.0/9.0;
outside of the while loop, it uses the current value of fahrenheit (which is 0.0), and calculates the value for celsius, which is -17.78.
Inside the while loop, although fahrenheit changes, celsius will not, because there are no statements inside of the while loop to actually change the value of the variable. This is why you have to move the statement into the while loop, to make sure that the celsius value updates every time the fahrenheit value changes.
Welcome to SO. You are coming to grips with one of the basic flow control statements present in every programming language.
Basically, any computer program is a sequence of instructions and you can envisage the computer executing them one at a time from the top to the bottom of the page.
Logically, that means every instruction gets executed only once, unless there's a way to tell the program to "go backwards" and do something again. That's what the while statement (it's not a function) does. In english a while statement (usually called a while loop) does something like this
if a condition is true
execute some instructions
go back and check the condition and keep looping
else
continue after the loop
So, your main loop has a group of statements which are executed only once (every thing before the while).
Then the statements inside the while loop are repeated with fahrenheit taking on the a different value each time through the loop: 0, 5, 10, 15, ..., 95, 100. When you add 5 the last time, fahrenheit has the value 105, so the program leaves the loop and continues on (in this case, ending the main() function and exiting the program.
If you have the celsius calculation outside the loop, it only happens once. Then each time through the loop it prints out the value you calculated - even though fahrenheit is constantly changing.
When you move the calculation inside the while loop, celsius gets recalculated on each pass through the loop, using the new value of fahrenheit and thus producing a different results.
Hope this helps
The code:
celsius = (fahrenheit - 32.0) * 5.0/9.0;
assigns the variable celsius with (fahrenheit - 32.0) * 5.0/9.0
Where you call the code outside the while loop, fahrenheit is 0 so celsius becomes -17.78
If you put the code inside the while loop, it will reassign celsius each time the while block loops. Fahrenheit changes inside this loop so each time the while loop executes, both fahrenheit and celsius will be different if you reassign celsius with celsius = (fahrenheit - 32.0) * 5.0/9.0; inside the while loop.
That's because while is a loop that is used to repeat a block of code.
If you put celsius = (fahrenheit - 32.0) * 5.0/9.0; /* Works here */ outside the block of while it will iterate just once (and it's going to be when fahrenheit = 0.0), but if you put celsius = (fahrenheit - 32.0) * 5.0/9.0; /* Works here */ inside the block of while celsius will be changing his value as fahrenheit does, while the condition fahrenheit <= max is true.
You cannot expect the value of fahrenheit to change outside the loop as you are converting a single value:
(-32.0) * 5.0/9.0 = -17.8 (every time)
The only time the value with change is if you are changing the value of fahrenheit itself. Outside of the loop. That doesn't happen.
Additionally, comparing an int against a float is never a good idea. The reason being that a floating point number, stored in IEEE-754 Single Precision Floating Point Format is not an exact representation of the value provided for storage. (e.g 32.2 is not stored in floating point format as exactly 32.2) While you can make a relative comparison:
while(fahrenheit <= max)
It is better practice to do:
int i = 0;
for (i = 0; i < max; i+=5) {
fahrenheit += (float)i;
celsius = (fahrenheit - 32.0) * 5.0/9.0;
...
You are changing fahrenheit here within the loop and you can then expect the value of celsius to change accordingly.
Regarding floating point comparison, you will want to read The Floating-Point Guide - What Every Programmer Should Know .... The problem is you are not comparing what you think you are.
Program only displaying the 32 for when I have it print "NewTemp"
NewTemp = 32 + input * 180/100; this part seems like the main problem
#include <stdio.h>
float celsius(float input) {
float NewTemp;
**NewTemp = 32 + input * 180/100;
printf("Please enter the temperature value to convert to fahrenheit\n");
scanf("%f", &input);
printf("The temperature in celsius is: %f\n", NewTemp);
return NewTemp;
}
int main(void){
float CelToFahren, input;
CelToFahren = celsius(input);
}
You do the math before you read the input. You need to do it the other way around.
Also, there's no reason to pass a meaningless and uninitialized value to the celsius function.
Lastly, 180/100 is 1 remainder 80 because when you divide two integers, you get integer division. You can use 180.0/100.0.
Basically, you need to learn C.
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.