I am writing a simple converter to convert 3 metric measurements to British system values.I am supposed to write functions to solve it.
Below is the code I wrote.
#include <stdio.h>
void meter_to_feet(double,double);
void gram_to_pounds(double,double);
double Cel_to_Fahrenheit(double);
double target;
char letter;
int main(void){
printf("How many values to convert? ");
int time;
scanf("%d",&time);
//printf("%d\n",time);
for (int i=0;i<time;i++){
scanf("%lf",&target); //problem occurs here
scanf("%c",&letter); //and here
if('m'==letter){
meter_to_feet(target,3.2808);
}else if('g'==letter){
gram_to_pounds(target,0.002205);
}else if('c'==letter){
Cel_to_Fahrenheit(target);
}
}
return 0;
}
void meter_to_feet(double x,double y){
double r1;
r1=x*y;
printf("%0.6f ft\n",r1);
}
void gram_to_pounds(double x,double y){
double r1;
r1=x*y;
printf("%0.6f lbs\n",r1);
}
double Cel_to_Fahrenheit(double x){
double r1;
r1=x*1.8+32;
printf("%0.6f f\n",r1);
return r1;
}
I give it a .txt file looks like below to test this code in a step-by-step terminal and it passed.
4
12 m
14.2 g
3.2 c
19 g
However, when I complie and run above code using clang, I only get results of first two metric measurements--12m and 14.2g. It omitted the last two for some reason. I figured that I have to change my two scanf() lines into scanf("%lf %c",&target,&letter);
in order to get desired output.
I wonder how exactly scanf() here, treats input in such scenario?