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

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

Related

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 Convert to Farhenheit Convert to Celsius

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.

Converting temperatures from Celsius to Fahrenheit

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.

Why is my CELSIUS to RANKINE conversion failing when prompting user for a value for CELSIUS?

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;
}

Scanf always returning 0.000000

I'm trying to make a simple program to calculate the body mass index, but the scanf(s) always return 0.00000, no matter what i try. I searched everywhere, tried many things,
Thanks to everyone.
#include <stdio.h>
#include <stdlib.h>
int main() {
float height;
float initialheight;
float weight;
float bmi;
float nothing;
printf("What's your weight? ");
scanf("%lf", &weight);
printf("%f", &weight);
printf("What's your height? ");
scanf("%lf", &initialheight);
printf("%f", &initialheight);
height = (initialheight * initialheight);
printf("%f", &height);
bmi = (weight / height);
printf("Your BMI is ");
printf("%f", &bmi);
scanf("%f", nothing); //just to keep the program open
return 0;
}
If you print a value you dont have to print the adress!
So change this:
printf("%f", &weight);
to this:
printf("%f", weight);
So that you actually print the value
An also you have to change %lf to %f in your scanf
So your program should look something like this:
#include <stdio.h>
#include <stdlib.h>
int main(){
float height, initialheight, weight, bmi;
printf("What's your weight?\n>");
scanf(" %f", &weight);
printf("%.2f\n\n", weight);
printf("What's your height?\n>");
scanf(" %f", &initialheight);
printf("%.2f\n\n", initialheight);
height = (initialheight * initialheight);
bmi = (weight / height)*10000;
printf("Your BMI is ");
printf("%.2f\n\n", bmi);
system("pause");
return 0;
}
As an example with the input:
70 and 175
The result/ BMI is:
22.86
Side Note:
BMI = mass(kg) / (height(m) * height(m))
BMI = mass(lb) / (height(in) * height(in)) * 703
Well you have to change two things. First, change printf("%f", &weight) to printf("%f", weight). And also change scanf("%lf", &weight) to scanf("%f", &weight) will make your program fine.

Resources