I wrote up this quickly and every time I execute the program I always end up with 32 for fahrenheit and 0.00 for celsius and am not sure what the issue is here.
#include <stdio.h>
int main(void)
{
double celsius=0, fahrenheit=0;
printf("Enter a temperature in degrees Celsius: ");
scanf("%f", &celsius);
fahrenheit =(5.0/9.0)*celsius + 32;
printf("That is %.2f Fahrenheit \n", fahrenheit);
printf("Enter a temperature in degrees fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * (5.0/9.0);
printf("That is %.2f Celsius \n", celsius);
return(0);
}
Your format specifier for double is wrong, it should be "%lf". But that's not enough, you should also check that scanf() successfuly read read the value, like this
#include <stdio.h>
int report_error(const char *const message)
{
// TODO: add message formatting capabilities to this function
fprintf(stderr, "error: %s\n", message);
return EXIT_FAILURE;
}
int main(void)
{
double celsius = 0;
double fahrenheit = 0;
printf("Enter a temperature in degrees Celsius: ");
if (scanf("%lf", &celsius) != 1)
return report_error("Invalid Input");
fahrenheit = (5.0 / 9.0) * celsius + 32;
// ^ Define this as a constant?
printf("That is %.2f Fahrenheit \n", fahrenheit);
printf("Enter a temperature in degrees fahrenheit: ");
if (scanf("%lf", &fahrenheit) != 1)
return report_error("Invalid Input");
celsius = (fahrenheit - 32) * (5.0 / 9.0);
// ^ see if it was a constant???
printf("That is %.2f Celsius \n", celsius);
return 0;
}
Also, it appears that your algebra went wrong. You should check the celsius expression apparently.
Related
https://onlinegdb.com/-9jZX-uU0 [link]
I want to have the second line of code to have the user input the Fahrenheit not the Celsius twice. The int main() code is correct and the output number is correct but the user does not input the Fahrenheit. How do I fix this? enter image description here
What is expected is that the user inputs the Fahrenheit and the code continues. I do not want the output to skip over lines 12-14. I don't know how to fix this. I expect that once the user inputs Fahrenheit the code will continue to run as it currently does
#include <stdio.h>
#include <math.h>
double ask_for_air_celcius()
{
double temp_celcius, temp;
printf("Enter the current air temperature(in Celcius): ");
scanf(" %lf", &temp_celcius);
return(temp_celcius);
printf("Enter the current air temperature(in Fahrenheit): ");
scanf(" %lf", &temp);
return (temp);
}
int main()
{
double temp_celcius, spsound_1,spsound_convert_1,temp,
spsound,spsound_convert;
{
temp_celcius = ask_for_air_celcius();
spsound_1 = 1086 * sqrt(((5 * (temp_celcius*1.8+32)) + 297)
/ 247);
spsound_convert_1=spsound_1*1.09728;
temp = ask_for_air_celcius();
spsound = 1086 * sqrt(((5 * temp) + 297) / 247);
spsound_convert=spsound*1.09728;
printf("\nThe speed of sound at %.2f degrees Celcius is %.2f
ft/s.\n", temp_celcius, spsound_1);
printf ("The speed of sound at this temperature in Celciusis
equivalent to %.2f km/h.\n\n", spsound_convert_1);
printf("The speed of sound at %.2f degrees Fahrenheit is %.2f
ft/sec.\n", temp, spsound);
printf ("The speed of sound at this temperature in Fahrenheit
is equivalent to %.2f km/h.\n", spsound_convert);
}
return(0);
}
You used used return twice in the function so that it won't go ahead for asking the fahrenhiet question. Whenever return is encountered while compiling it terminates the function there and there only.
Now what you can do is that just make another function for fahrenhiet and copy paste the fahrenhit part in it.
#include <stdio.h>
#include <math.h>
double ask_for_air_celcius(){
double temp_celcius;
printf("Enter the current air temperature(in Celcius): ");
scanf(" %lf", &temp_celcius);
return(temp_celcius);
}
double ask_for_air_Fahrenhiet(){
double temp;
printf("Enter the current air temperature(in Fahrenheit): ");
scanf(" %lf", &temp);
return (temp);
}
int main()
{
double temp_celcius, spsound_1,spsound_convert_1,temp,
spsound,spsound_convert;
{
temp_celcius = ask_for_air_celcius();
spsound_1 = 1086 * sqrt(((5 * (temp_celcius*1.8+32)) + 297)
/ 247);
spsound_convert_1=spsound_1*1.09728;
temp = ask_for_air_Fahrenhiet();//changes here
spsound = 1086 * sqrt(((5 * temp) + 297) / 247);
spsound_convert=spsound*1.09728;
printf("\nThe speed of sound at %.2f degrees Celcius is %.2f ft/s.\n", temp_celcius, spsound_1);
printf ("The speed of sound at this temperature in Celciusis equivalent to %.2f km/h.\n\n", spsound_convert_1);
printf("The speed of sound at %.2f degrees Fahrenheit is %.2f ft/sec.\n", temp, spsound);
printf ("The speed of sound at this temperature in Fahrenheit is equivalent to %.2f km/h.\n", spsound_convert);
}
return(0);
}
Hope it helps!!!
;)
I'm a beginner to C language and I'm writing through a practice question where I use functions to convert between Fahrenheit and Celsius. I have written the program using 2 functions: one to convert from Fahrenheit to Celsius and another vice verca:
#include <stdio.h>
int celsius2fahrenheit(int celsius, int fahrenheit){
fahrenheit = (9/5) * celsius + 32;
printf("%d", fahrenheit);
}
int fahrenheit2celsius(int fahrenheit, int celsius){
celsius = (fahrenheit - 32) * (5/9);
printf("%d", celsius);
}
int main(void){
int celsius, fahrenheit;
celsius = fahrenheit = 0;
printf("-----------------Menu-----------------\n");
printf("Option 1. Convert Celsius to Fahrenheit\n");
printf("Option 2. Convert Fahrenheit to Celsius\n");
printf("\n");
printf("Option: ");
int Option;
scanf("%d", Option);
if (Option == 1){
printf("Enter the temperature (Celsius) : ");
scanf("%d", celsius);
printf("\n");
celsius2fahrenheit(celsius, fahrenheit);
}
else if (Option == 2){
printf("Enter the temperature (Fahrenheit) : ");
scanf("%d", fahrenheit);
printf("\n");
fahrenheit2celsius(fahrenheit, celsius);
}
else {
printf("Invalid Option");
}
return 0;
}
When the program was compiled, the error messages occur:
"Segmentation fault" after executing the option to select the operation. I assume there was something wrong with the variable declaration but I'm not too sure.
You are missing '&' in your scanf functions (line 23, 27 and 33)
Also you should take the values as floats to get more accurate results, because right now answers comes as integer..
#include <stdio.h>
int main()
{
float fh,cl;
int choice;
printf("\n1: Convert temperature from Fahrenheit to Celsius.");
printf("\n2: Convert temperature from Celsius to Fahrenheit.");
printf("\nEnter your choice (1, 2): ");
scanf("%d",&choice);
if(choice ==1){
printf("\nEnter temperature in Fahrenheit: ");
scanf("%f",&fh);
cl= (fh - 32) / 1.8;
printf("Temperature in Celsius: %.2f",cl);
}
else if(choice==2){
printf("\nEnter temperature in Celsius: ");
scanf("%f",&cl);
fh= (cl*1.8)+32;
printf("Temperature in Fahrenheit: %.2f",fh);
}
else{
printf("\nInvalid Choice !!!");
}
return 0;
}
scanf needs the specifier and the memory address of the variable.
scanf("%d", &Option);
Note that Option and &Option are two different things. & means the memory address where the variable is located.
Change each one in the code (line 27 and 33).
I am trying to combine Switch into Void Function to convert Celsius to Fahrenheit and vice versa. I am not sure what is correct way to combine Switch and Void Function together.
My code is at below. Any advice is valuable for me.
Thank you.
#include<stdio.h>
void fahtocel(void);
void celtofah(void);
int main(void)
{
int tem;
printf("Please select your choice\n");
printf("Enter 1 if you need to convert Fahrenheit to Celsius\n");
printf("Enter 2 if you need to convert Celsius to Fahrenheit\n");
scanf("%d", &tem);
switch(tem) {
case 1:
fahtocel();
break;
case 2:
celtofah();
break;
}
return 0;
}
void fahtocel()
{
float fahr;
printf("Please input Fahrenheit: ");
scanf("%2.f", &fahr);
printf("%3f Fahrenheit is equal %3.0f Celsius \n",fahr,(5.0/9.0)*(fahr - 32)*5/9;
}
void celtofah()
{
float celsius;
printf("Please input celsius: ");
scanf("%2.f", &celsius);
printf("%2.f Celsius is equal %2.f Fahrenheit\n",celsius, (9*celsius/5)+32);
}
You must be getting compiler issues. Put a parentheses at the end of your printf, and change the %2.f to just %f in your scanfs.
This code works...
#include<stdio.h>
void fahtocel(void);
void celtofah(void);
int main(void)
{
int tem;
printf("Please select your choice\n");
printf("Enter 1 if you need to convert Fahrenheit to Celsius\n");
printf("Enter 2 if you need to convert Celsius to Fahrenheit\n");
scanf("%d", &tem);
switch (tem) {
case 1:
fahtocel();
break;
case 2:
celtofah();
break;
}
return 0;
}
void fahtocel()
{
float fahr;
printf("Please input Fahrenheit: ");
scanf("%f", &fahr);
printf("%3f Fahrenheit is equal %3.0f Celsius \n", fahr, (5.0 / 9.0) * (fahr - 32) * 5 / 9);
}
void celtofah()
{
float celsius;
printf("Please input celsius: ");
scanf("%f", &celsius);
printf("%2.f Celsius is equal %2.f Fahrenheit\n", celsius, (9 * celsius / 5) + 32);
}
Perform the following conversions to convert from CELSIUS to RANKINE:
degrees Fahrenheit = (9.0/5.0) * degrees Celsius + 32
degrees Rankine = degrees Fahrenheit + 459.67"
This program converts degrees Celsius to degrees Rankine. Prompt the user for a temperature in Celsius.
#include <stdio.h>
int main(void)
{
double f,c,r;
printf("Enter the temperature in degrees Celsius:" );
scanf("%d", &c);
f = (9.0/5.0) * c +32;
r = f + 459.67;
printf("After your conversion, your temperature in Rankin is: ", r);
return(0);
}
Why is my CELSIUS to RANKINE conversion failing when prompting user for a value for CELSIUS?
Code is using the incorrect type of data per the format specifiers. For type double, scan with "%lf" and to print use "%f"
#BLUEPIXY comment to use the matching format specifiers in sncaf() and printf():
Insure prompt is flushed.
Check scanf() return value.
.
#include <stdio.h>
int main(void) {
double f,c,r;
printf("Enter the temperature in degrees Celsius:" );
fflush(stdout);
if (scanf("%lf", &c) != 1) {
puts("Non-numeric input" );
return -1;
}
f = (9.0/5.0) * c +32;
printf("After your conversion, your temperature in Fahrenheit is: %.1f F", f);
r = f + 459.67;
printf("After your conversion, your temperature in Rankine is: %.1f R", r);
return 0;
}
I was wondering how I could prompt the end-user of my program to type in a value they want to be converted from Fahrenheit into Celsius in C.
Basically, since I'm a total n00b and I'm writing amazing "programs" such as this one:
//Simple program to convert Fahrenheit to Celsius
int main (int argc, char *argv[])
{
double celsius, fahrenheit, result;
celsius = result;
fahrenheit = 27;
result = (fahrenheit - 32) / 1.8;
printf("27 degress Fahrenheit is %g degrees Celsius!", result);
return 0;
}
I would like to add some actual "functionality" to it if you know what I mean. Instead of just making this a test program where really it just shows off some simple arithmetic expression evaluating, I would like to actually make it somewhat mildly useful.
Anyway, I was wondering if I could use the function listed in the scanf(3) Man page to aid me in the recognition of user-inputted data, and then somehow store it into the Fahrenheit variable.
Now, it would really be cool if the program, upon running, could prompt the end-user with a question asking whether he or she would like to convert from Celsius to Fahrenheit or from Fahrenheit to Celsius, but let's just take it one step at a time, and I'll wait until I read the chapter in my book about "Making Decisions"! :)
UPDATE:
Removes useless variable result as pointed out by kiamlaluno:
//Simple program to convert Fahrenheit to Celsius
int main (int argc, char *argv[])
{
double fahrenheit, celsius;
fahrenheit = 27;
celsius = (fahrenheit - 32) / 1.8;
printf("27 degress Fahrenheit is %g degrees Celsius!", celsius);
return 0;
}
UPDATE UPDATE:
I've been trying to incorporate everyone's helpful suggestions posted here, but I'm running into more problems with my code:
//Simple program to convert Fahrenheit to Celsius and Celsius to Fahrenheit
int main (int argc, char *argv[])
{
int celsius, fahrenheit, celsiusResult, fahrenheitResult;
celsiusResult = (fahrenheit - 32)*(5/9);
fahrenheitResult = (celsius*(9/5)) + 32;
int prompt;
printf("Please press 1 to convert Fahrenheit to Celsius, or 0 to convert Celsius to Fahrenheit please:\n ");
scanf("%i", &prompt);
if(prompt == 1) {
printf("Please enter a temperature in Fahrenheit to be converted into Celsius!:\n");
scanf("%i", &fahrenheit);
printf("%i degress Fahrenheit is %i degrees Celsius!", fahrenheit, celsiusResult);
}
else {
printf("Please enter a temperature in Celsius to be converted into Fahrenheit:\n");
scanf("%i", &celsius);
printf("%i degreses Celsius is %i degrees Fahrenheit", celsius, fahrenheitResult);
}
return 0;
}
Everything's working great, except for the calculations themselves, which come out completely wrong..
For a second I thought this may have been because I changed the numbers themselves to integers types, but I made them doubles again and it was still kind of screwy.
Any thoughts?
To use scanf() to read a double, you'd need to use the correct format string. It would be %lf for "long float" (where %f alone would read into a float). You could then read directly to a double. Same goes for printing it out.
int main (int argc, char *argv[])
{
double fahrenheit, celsius;
printf("farenheit? "); /* write a prompt */
scanf("%lf", &fahrenheit); /* read a double into the fahrenheit variable */
celsius = (fahrenheit - 32) / 1.8;
printf("%lf degress Fahrenheit is %lf degrees Celsius!\n", fahrenheit, celsius);
return 0;
}
Note that this doesn't handle non-numeric inputs at all. You'd need to use other input techniques.
[edit]
You are certainly taking jumps in your code which seems like you're making a lot of progress. :)
To address your most current update, there are a couple of issues. Your code doesn't actually calculate anything, you applied the formula too soon. You should wait until fahrenheit or celsius have meaningful values (i.e., after the user has input the value to be converted). It would be a good idea to move these formulas into functions to perform the conversion. You should also stick with using double and not integers. You will not get the precision you want using integers.
double convert_fahrenheit_to_celsius(double fahrenheit)
{
return (fahrenheit - 32)*(5/9);
}
double convert_celsius_to_fahrenheit(double celsius)
{
return (celsius*(9/5)) + 32;
}
int main (int argc, char *argv[])
{
double celsius, fahrenheit, celsiusResult, fahrenheitResult;
int prompt;
printf("Please press 1 to convert Fahrenheit to Celsius, or 0 to convert Celsius to Fahrenheit please:\n ");
scanf("%i", &prompt);
if(prompt == 1) {
printf("Please enter a temperature in Fahrenheit to be converted into Celsius!:\n");
scanf("%lf", &fahrenheit);
/* now convert user-input fahrenheit to celsius */
celsiusResult = convert_fahrenheit_to_celsius(fahrenheit);
printf("%lf degress Fahrenheit is %lf degrees Celsius!", fahrenheit, celsiusResult);
}
else {
printf("Please enter a temperature in Celsius to be converted into Fahrenheit:\n");
scanf("%lf", &celsius);
/* now convert user-input celsius to fahrenheit */
fahrenheitResult = convert_celsius_to_fahrenheit(celsius);
printf("%lf degreses Celsius is %lf degrees Fahrenheit", celsius, fahrenheitResult);
}
return 0;
}
Ok if you want to prompt the user for input just use scanf with the tag %lf:
//Simple program to convert Fahrenheit to Celsius
int main (int argc, char *argv[])
{
double fahrenheit, result;
printf("Please enter a number for farenheit: ");
scanf("%lf", &fahrenheit);
result = (fahrenheit - 32) / 1.8;
printf("27 degress Fahrenheit is %g degrees Celsius!", result);
return 0;
}
As for prompting the user if he/she wants celcius or farenheit you need something called and if statement and else statement.
int main (int argc, char *argv[])
{
double celsius, fahrenheit, result;
int ForC;
printf("1 for Celsius or 0 for Fahrenheit plz: ");
scanf("%d", &ForC);
if(ForC == 1) {
scanf("%lf", &fahrenheit);
result = (fahrenheit - 32) / 1.8;
printf("fahrenheit to celsius:%lf", result);
}
else {
scanf("%lf", &celsius);
result = (celsius*9.0)/5.0 + 32;
printf("celsius to farenheit:%lf", result);
}
return 0;
}
Handling the possibly erroneous user input could be done by checking the return value of scanf(). It must be equal to the number of values expected (1 in this case). In other case, the input should be repeated.
For your UPDATE UPDATE : Try doing the calculation after you get the value from the user. If not, the resulting value of celsius, fahrenheit, celsiusResult, fahrenheitResult is unknown.