Converting temperatures from Celsius to Fahrenheit - c

I wrote a program that converts temperatures from Celsius to Fahrenheit. When I enter c, the code works as expected, but when I enter f, it gives me a random number:
#include <stdio.h>
int main (void){
float f,c;
printf("x=Ende c=Umrechnung c->f f=Umrechnung f->c:");
if(scanf("c",&c)==c){
printf("Grad Celisius =");
scanf("%f",&c);
f=(c*1.8)+32;
printf("%.2f Grad Celisius sind %.2f Grad Fahreiheit\n",c,f);
}
else if(scanf("f",&f)==f){
printf("Grad fahrenheit =");
scanf("%f",&f);
c=(f-32)/1.8;
printf("%.2f Grad Fahrenheit sind %.2f Grad Fahrenheit",f,c);
}
return 0;
}
How can I fix my problem?

You didn't use the scanf() command properly. I modified your code a little bit and this works for me:
#include <stdio.h>
int main (void){
float f,c;
char ch;
printf("x=Ende c=Umrechnung c->f f=Umrechnung f->c:");
scanf(" %c", &ch);
if(ch=='c'){
printf("Grad Celsius =");
scanf("%f",&c);
f=(c*1.8)+32;
printf("%.2f Grad Celsius sind %.2f Grad Fahrenheit\n",c,f);
}
else if(ch=='f'){
printf("Grad Fahrenheit =");
scanf("%f",&f);
c=(f-32)/1.8;
printf("%.2f Grad Fahrenheit sind %.2f Grad Celsius",f,c);
}
return 0;
}
Basically, you have read a character (ch) only once and compare it afterwards to the options that your program provides. Please note the space character in " %c" in the scanf() line. It might be needed so that the code works properly, as described here.
I guess that you also want to put a loop around your code, so that it makes sense to ask the user to enter an x in order to end the program. Currently the program terminates if you don't enter c or f. The program also terminates after one calculation has been done.
By the way, I also changed one Celsius to Fahrenheit in your last printf(). I think you confused the words there. I also changed Celisius to Celsius.

Related

Simple C Coding Problem: Skipping Over Printf and Skipping User Input

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!!!
;)

Confusion with scanf() or if()

I am not so familiar with C. Therefore, maybe someone will easily find a solution, I will not mind if you share it.
After entering the data in the first scanf() always gives the option else(): "Error".
I was looking for possible options for the problem. I found a lot of things like that, but nothing that to help me specifically. I think the mistake is in the strcmp(). But I can not say for sure. Will you help?
#include <stdio.h>
#include <string.h>
int main()
{
float celsius, fahrenheit;
char tempConvering[10];
printf("Enter what to convert to what (F to C; C to F): ");
scanf(" %s", &tempConvering[10]);
if(strcmp(tempConvering, "F to C") == 0)
{
printf("\nEnter temperature in Fahrenheit: ");
scanf(" %f", &fahrenheit);
celsius = fahrenheit * 1.8 + 32;
printf("%.2f Fahrenheits = %.2f Celsius\n", fahrenheit, celsius);
}
else if(strcmp(tempConvering, "C to F") == 0)
{
printf("\nEnter temperature in Celsius: ");
scanf(" %f", &celsius);
celsius = (fahrenheit - 32) / 1.8;
printf("%.2f Celsius = %.2f Fahrenheits\n", celsius, fahrenheit);
}
else
{
puts("\nError!");
}
}
I believe there's an error with how you're using scanf, specifically, at this point:
scanf(" %s", &tempConvering[10]);
^
|
+---- here
The second argument to scanf should be the address of the place to store the result. Here, you're saying "place the string that's read in in the memory just after the buffer that I've set up," which isn't probably want you wanted to do. Instead, write this:
scanf(" %s", tempConvering);
This says "place the string inside the buffer named tempConverting." If you're just getting started with C and haven't learned much about pointers and arrays, a good rule of thumb is that if you're reading a string with `scanf, you should just give the name of the array variable where you want to store the string rather than using an ampersand.
Hope this helps!
#include <stdio.h>
#include <string.h>
int main()
{
float celsius, fahrenheit;
char tempConvering[20];
printf("what do you want to convert? ");
scanf("%s", tempConvering);
if(strcmp(tempConvering, "Fahrenheits") == 0)
{
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) / 1.8;
printf("%.2f Fahrenheits = %.2f Celsius\n", fahrenheit, celsius);
}
else if(strcmp(tempConvering, "Celsius") == 0)
{
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = celsius * 9 / 5 + 32;
printf("%.2f Celsius = %.2f Fahrenheits\n", celsius, fahrenheit);
}
else
{
puts("\nError!");
}
}
This was the answer. I have to thank you for the tips, I will try to remember all the details about the scanf(). However, the problem disappeared only when I changed the desired answer not to "F to C" but to "Fahrenheits". Well, respectively, I changed the question. The program instantly earned. Nevertheless, attempts to do something with the scanf() are unsuccessful, to the extent that the same thing happens with the fgets().
In any case, somehow the problem is solved, thank you all!
Don't put extra space while you execute the scanf() function.
This might be an issue sometimes!
This should work fine:
scanf("%s", tempConvering);
By the way, while you take a string as an input, there is no need to use & before the string name.
Someone has already described the details above.

C - printf inside "if" statement doesn't show

I'm trying to make this printf inside the if statement show, but it doesn't. Here is the code, I've highlighted the printf section.
char conv;
float cel;
printf("Enter what you'd like converted: \n\t1.Celcius to Fahrenheit\n\t2.Inch to CM\n\t3.CM to Inch\n");
scanf_s("%c", &conv);
// This one
if (conv == '1')
printf("Please enter amount of Celcius: \n");
scanf_s("%f", &cel);
float fahr;
fahr = 33.8 * cel;
printf("Conversion of %f Celsius is %f Fahrenheit", cel, fahr);
getch();
return 0;
What is the reason for this?
In looking at the line: scanf_s("%c", &conv);
I see the function scanf_s(),
which in not in any C library that I'm familiar with.
However, the function scanf(), which is in the C libraries,
has the following prototype and usage:
int scanf(const char *format, ...)
where the return value is the number of items in the variable parameter list '...'
that were actually filled from the input.
I.E. your code should, after the call to scanf_s() check the return code
those value should be 1 in your example.
Then you have this code:
if (conv == '1')
printf("Please enter amount of Celcius: \n");
scanf_s("%f", &cel);
supposedly with the idea that all those following lines would be
enclosed in the 'if' block.
however, only the first line following the 'if' is actually part of the
'if' block.
To correct this problem, write the code using braces to delineate
the 'if' block, like this:
if (conv == '1')
{
printf("Please enter amount of Celcius: \n");
scanf_s("%f", &cel);
float fahr;
fahr = 33.8 * cel; // BTW: this is not the correct conversion formula
printf("Conversion of %f Celsius is %f Fahrenheit", cel, fahr);
}

Why does my printf return the incorrect value?

Why does this basic code return an incorrect value? I'm relatively new to programming, and I can't understand why this basic function isn't working. I have a working version of this program, so I don't understand the issue.
#include <stdio.h>
int main (void)
{
int fahrenheit;
printf("Enter the temperature in degrees fahrenheit:\n");
scanf("%d", &fahrenheit);
printf("\n%d \n", &fahrenheit);
system("PAUSE");
return 0;
}
Output:
Enter the temperature in degrees fahrenheit:
53
2686788
Press any key to continue . . .
printf does not expect a pointer to your variables like scanf, it expects the data.
Try printf("\n%d \n", fahrenheit);
Just take the & off of the printf line.
Change:
printf("\n%d \n", &fahrenheit);
To:
printf("\n%d \n", fahrenheit);

Store user-input in a variable

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.

Resources