can you check it?-about function

What are my errors?
can you check it?
what will be the correct program?

[code] #include
double TEMPERATURE(double t, char s)
{
if (s=='F')
t=9/5*t-32;
if (s=='C')
t=5/9*(t+32);
return (t);
}
int main ()
{double m,a;
char b;
printf("Enter the temperature(a^b):
");
scanf("%lf^%c",&a,&b);
m=TEMPERATURE(a,b);
printf("temperature=%lf",a);
return 0;
}[/code]

Comments

  • You assign "m" the new temperature but never use it...

    Also, get in the habit of always initializing variables and giving them proper names--they can save alot of headaches later on ;-)

    I dont see anything else particularly wrong with the code.. (besides the formatting of the code can be better)
  • i dont see any syntax errors in this code. can't say there is logical error either. printing a in place of m cannot be counted as a logical error, it is the temperature that you are printing. still, m is unused.

    i also agree that the formatting could have been better. some spacing can do a lot good.
    cheers!

  • : What are my errors?
    : can you check it?
    : what will be the correct program?
    :
    : [code]: #include
    : double TEMPERATURE(double t, char s)
    : {
    : if (s=='F')
    : t=9/5*t-32;
    : if (s=='C')
    : t=5/9*(t+32);
    : return (t);
    : }
    : int main ()
    : {double m,a;
    : char b;
    : printf("Enter the temperature(a^b):
    ");
    : scanf("%lf^%c",&a,&b);
    : m=TEMPERATURE(a,b);
    : printf("temperature=%lf",a);
    : return 0;
    : }[/code]:
    :
    :
    You print the value of the wrong variable.. a instead of m that is the result ;)
    [code]
    1 #include
    2 double TEMPERATURE(double t, char s)
    3 {
    4 if (s=='F')
    5 t=((9/5)*t)-32;
    6 if (s=='C')
    7 t=(5/9)*(t+32);
    8 return (t);
    9 }
    10
    11 int main ()
    12 {
    13 double m,a;
    14 char b;
    15 printf("Enter the temperature: ");
    16 scanf("%lf",&a);
    17 getc(stdin);
    18 printf("To: ");
    19 scanf("%c",&b);
    20 m=TEMPERATURE(a,b);
    21 printf("temperature=%lf %c
    ",m,b);
    22 return 0;
    23 }
    24
    [/code]
  • : : What are my errors?
    : : can you check it?
    : : what will be the correct program?
    : :
    : : [code]: : #include
    : : double TEMPERATURE(double t, char s)
    : : {
    : : if (s=='F')
    : : t=9/5*t-32;
    : : if (s=='C')
    : : t=5/9*(t+32);
    : : return (t);
    : : }
    : : int main ()
    : : {double m,a;
    : : char b;
    : : printf("Enter the temperature(a^b):
    ");
    : : scanf("%lf^%c",&a,&b);
    : : m=TEMPERATURE(a,b);
    : : printf("temperature=%lf",a);
    : : return 0;
    : : }[/code]: :
    : :
    : :
    : You print the value of the wrong variable.. a instead of m that is
    : the result ;)
    : [code]:
    : 1 #include
    : 2 double TEMPERATURE(double t, char s)
    : 3 {
    : 4 if (s=='F')
    : 5 t=((9/5)*t)-32;
    : 6 if (s=='C')
    : 7 t=(5/9)*(t+32);
    : 8 return (t);
    : 9 }
    : 10
    : 11 int main ()
    : 12 {
    : 13 double m,a;
    : 14 char b;
    : 15 printf("Enter the temperature: ");
    : 16 scanf("%lf",&a);
    : 17 getc(stdin);
    : 18 printf("To: ");
    : 19 scanf("%c",&b);
    : 20 m=TEMPERATURE(a,b);
    : 21 printf("temperature=%lf %c
    ",m,b);
    : 22 return 0;
    : 23 }
    : 24
    : [/code]:
    :


    your program you sent to me did not terminate..
    I think the problem was the use of char in the function header.
    In function main, it must be stated as :
    [code]
    scanf("%d^%c", &a,&b);
    [/code]
    I tried it many times but it doesnt work. but if i change the type specifier "char", the program terminates..

    ??????
  • %d reads an integer, not a float. Float is %f and double is %lf.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories