Thread: Converting tempherature

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    5

    Question Converting tempherature

    Please help me fix issue with my program.
    Test Case
    Input
    100
    C
    K
    Output
    373

    But my program always 0.0000 in output. Please help me to fix.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    float Konversi(x,y,z){
        float hasil;
        if(x=='C' && z=='K'){
            hasil=y+273;}
        else if(x=='C' && z=='F'){
            hasil=((9/5)*y)+32;}
        else if(x=='C' && z=='R'){
            hasil=(4/5)*y;}
        else if(x=='R' && z=='C'){
            hasil=(5/4)*y;}
        else if(x=='R' && z=='F'){
            hasil=((9/4)*y)+32;}
        else if(x=='R' && z=='K'){
            hasil=((5/4)*y)+273;}
        else if(x=='F' && z=='C'){
            hasil=(5/9)*(y-32);}
        else if(x=='F' && z=='C'){
            hasil=(4/9)*(y-32);}
        else if(x=='F' && z=='K'){
            hasil=((5/9)*(y-32))+273;}
        else if(x=='K' && z=='C'){
            hasil=y-273;}
        else if(x=='K' && z=='R'){
            hasil=(4/5)*(y-273);}
        else if(x=='K' && z=='F'){
            hasil=((9/5)*(y-273))+32;}
        return hasil;
    }
    
    
    int main()
    {
        char a,c;
        float b;
        scanf("%f",&b);
        scanf("%s",&a);
        scanf("%s",&c);
        printf("%f",Konversi(a,b,c));
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Check your compiler warnings:

    Code:
    main.c||In function 'Konversi':|
    main.c|5|warning: type of 'x' defaults to 'int'|
    main.c|5|warning: type of 'y' defaults to 'int'|
    main.c|5|warning: type of 'z' defaults to 'int'|
    main.c||In function 'main':|
    main.c|43|warning: control reaches end of non-void function|
    The parameters in your function header should include the data types (e.g. char).

    You should have some prompts in your program so the user knows what they are supposed to enter.

    Code:
    scanf("%s",&a);
    scanf("%s",&c);
    "a" and "c" are of type char, so you cannot read a string into them. Perhaps you meant "%c"? (If so, be aware of this.)

    Also, use descriptive variable names so your code is easier to read.

    Your "Konversi()" function uses integer division, e.g. (9/5) and (5/4). Integer division yields an integer result (any decimal value is truncated). To get a double result, use doubles instead, e.g. (9.0 / 5.0).

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    5
    Hi Can you create the fix version of the whole code then paste here? I followed your step, buy nothing happen. Or maybe I was wrong. Please help me.
    Quote Originally Posted by Matticus View Post
    Check your compiler warnings:

    Code:
    main.c||In function 'Konversi':|
    main.c|5|warning: type of 'x' defaults to 'int'|
    main.c|5|warning: type of 'y' defaults to 'int'|
    main.c|5|warning: type of 'z' defaults to 'int'|
    main.c||In function 'main':|
    main.c|43|warning: control reaches end of non-void function|
    The parameters in your function header should include the data types (e.g. char).

    You should have some prompts in your program so the user knows what they are supposed to enter.

    Code:
    scanf("%s",&a);
    scanf("%s",&c);
    "a" and "c" are of type char, so you cannot read a string into them. Perhaps you meant "%c"? (If so, be aware of this.)

    Also, use descriptive variable names so your code is easier to read.

    Your "Konversi()" function uses integer division, e.g. (9/5) and (5/4). Integer division yields an integer result (any decimal value is truncated). To get a double result, use doubles instead, e.g. (9.0 / 5.0).

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Anggi R Edwarsa View Post
    Hi Can you create the fix version of the whole code then paste here? I followed your step, buy nothing happen. Or maybe I was wrong. Please help me.
    I will not provide a complete solution - that would take away a valuable learning experience, and goes against the homework policy.

    You said you tried to apply the advice I gave - you should post your updated code, along with any specific problems you're having. If you have any questions on anything I posted, just ask.

    You can do this. Give it your best shot and we'll help you along.

  5. #5
    Registered User
    Join Date
    Dec 2016
    Posts
    5
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    float Konversi(char x,float y,char z){
        float hasil;
        if(x=='C' && z=='K'){
            hasil=y+273;}
        else if(x=='C' && z=='F'){
            hasil=((9.0/5.0)*y)+32;}
        else if(x=='C' && z=='R'){
            hasil=(4.0/5.0)*y;}
        else if(x=='R' && z=='C'){
            hasil=(5.0/4.0)*y;}
        else if(x=='R' && z=='F'){
            hasil=((9.0/4.0)*y)+32;}
        else if(x=='R' && z=='K'){
            hasil=((5.0/4.0)*y)+273;}
        else if(x=='F' && z=='C'){
            hasil=(5.0/9.0)*(y-32);}
        else if(x=='F' && z=='C'){
            hasil=(4.0/9.0)*(y-32);}
        else if(x=='F' && z=='K'){
            hasil=((5.0/9.0)*(y-32))+273;}
        else if(x=='K' && z=='C'){
            hasil=y-273;}
        else if(x=='K' && z=='R'){
            hasil=(4.0/5.0)*(y-273);}
        else if(x=='K' && z=='F'){
            hasil=((9.0/5.0)*(y-273))+32;}
        else{
            printf("%c",x);
            hasil=0;
        }
        return hasil;
    }
    
    
    int main()
    {
        char x,z;
        float y;
        scanf("%f",&y);
        scanf("%c",&x);
        scanf("%c",&z);
        printf("%f",Konversi(x,y,z));
    }
    Now input stop after second input.
    Quote Originally Posted by Matticus View Post
    I will not provide a complete solution - that would take away a valuable learning experience, and goes against the homework policy.

    You said you tried to apply the advice I gave - you should post your updated code, along with any specific problems you're having. If you have any questions on anything I posted, just ask.

    You can do this. Give it your best shot and we'll help you along.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're almost there. I anticipated the problem you're now having, and posted a link to the FAQ up in post #2 which explains the cause and several solutions.

    Code:
    scanf("%c",&x);
    scanf("%c",&z);
    When you enter the first character and press "enter", two characters are received - the one you entered, and a newline ('\n'). The second "scanf()" is reading that newline waiting in the buffer.

    Try putting a single space before the %, as in " %c".

    (FYI: I haven't thoroughly checked your code, as I'm just aiming to get you over this immediate hump.)

  7. #7
    Registered User
    Join Date
    Dec 2016
    Posts
    5
    Thanks for your help. It's working now.
    Quote Originally Posted by Matticus View Post
    You're almost there. I anticipated the problem you're now having, and posted a link to the FAQ up in post #2 which explains the cause and several solutions.

    Code:
    scanf("%c",&x);
    scanf("%c",&z);
    When you enter the first character and press "enter", two characters are received - the one you entered, and a newline ('\n'). The second "scanf()" is reading that newline waiting in the buffer.

    Try putting a single space before the %, as in " %c".

    (FYI: I haven't thoroughly checked your code, as I'm just aiming to get you over this immediate hump.)

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're welcome. See how much more gratifying it is to get there with a bit of guidance, rather than just being given the answer?

    Well done.

  9. #9
    Registered User
    Join Date
    Dec 2016
    Posts
    5
    Yass. Learning is for fun.
    Quote Originally Posted by Matticus View Post
    You're welcome. See how much more gratifying it is to get there with a bit of guidance, rather than just being given the answer?

    Well done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting Doc to PDF and PDF to DOC
    By veera in forum C# Programming
    Replies: 2
    Last Post: 05-04-2014, 10:55 AM
  2. Converting from wma to mp3
    By yaku in forum C Programming
    Replies: 7
    Last Post: 06-25-2012, 01:12 PM
  3. need help converting c dll to cpp
    By *DEAD* in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2007, 10:22 PM
  4. Converting C to C++
    By cdn_bacon in forum C++ Programming
    Replies: 20
    Last Post: 03-24-2007, 01:53 PM
  5. Converting exe to DLL
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2005, 12:05 PM

Tags for this Thread