Thread: Help with almost solved problem

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    Help with almost solved problem

    I am a newbie when it come to C++ but I think I am close to solving this code. Can anyone tell me where my mistake is. I am having on the Temperature reading.

    Code:
    #include<stdio.h>
    
    iint choice; /* 1 to convert celsius, 2 to convert fahrenheit. //
        int c; // degrees in celsius//
        int f; // degrees in fahrenheit//
        float ctemp, ftemp; //temperatures//
    
    int main ( void )
    
    {
        printf( "This program converts from celsius to fahrenheit and fahrenheit to celsius.\n" );
    
        printf( "Enter 1 for Fahrenheit to Celsius or 2 for Celsius to Farenheit.\n" );
    
        scanf("%d", &choice); //input from user//
    
    if (choice == 1)
    {
        printf( "Enter degrees in Fahrenheit.");
        scanf ("%d", &f); 
        ctemp = (5/9) * (f-32);
        printf ( "Temperature in Celsius is", ctemp); //Degrees in celsius//
    }
    else{ 
        if (choice == 2)
        {
        printf( "Enter degrees in Celsius.");
        scanf ("%d", &c); //get degrees celsius from user//
        ftemp = (9/5) * (c+32);
        printf ( "Temperature in farenheit is", ftemp); //output degrees in celsius//
        }
        else 
        printf ( "Bad selection." );
    }
    
    return 0; //end program//
    }
    Last edited by Kaname; 09-12-2009 at 04:48 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    5/9 is zero, and multiplying anything by zero gives zero. Similarly 9/5 is one, and one times anything is itself. (Perhaps you want 5.0/9.0 etc.)

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    I tried it but, I still get same results. Thanks for the pointer, I added it to code.
    Last edited by Kaname; 09-12-2009 at 05:11 AM.

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by Kaname View Post
    I am a newbie when it come to C++ but I think I am close to solving this code. Can anyone tell me where my mistake is. I am having on the Temperature reading.

    Code:
    #include<stdio.h>
    
    iint choice; /* 1 to convert celsius, 2 to convert fahrenheit. //
        int c; // degrees in celsius//
        int f; // degrees in fahrenheit//
        float ctemp, ftemp; //temperatures//
    
    int main ( void )
    
    {
        printf( "This program converts from celsius to fahrenheit and fahrenheit to celsius.\n" );
    
        printf( "Enter 1 for Fahrenheit to Celsius or 2 for Celsius to Farenheit.\n" );
    
        scanf("%d", &choice); //input from user//
    
    if (choice == 1)
    {
        printf( "Enter degrees in Fahrenheit.");
        scanf ("%d", &f); 
        ctemp = (5/9) * (f-32);
        printf ( "Temperature in Celsius is", ctemp); //Degrees in celsius//
    }
    else{ 
        if (choice == 2)
        {
        printf( "Enter degrees in Celsius.");
        scanf ("%d", &c); //get degrees celsius from user//
        ftemp = (9/5) * (c+32);
        printf ( "Temperature in farenheit is", ftemp); //output degrees in celsius//
        }
        else 
        printf ( "Bad selection." );
    }
    
    return 0; //end program//
    }
    There are a whole lot of things to be pointed out in your code-
    1. This is not a C++ code, it's perfectly C, so you posted in a wrong section.
    2. There is nothing called "iint" in C.
    3. Comment your code properly. If you've started with /* so you should end up with */ not //. // is a single line comment used with one lines only. For eg. here is a single line comment:
    int i; //this is a comment
    4. As tabstop mentioned change 5/9 and 9/5 to 5.0/9 and 9.0/5.
    5. Use proper format specifier to print the required value. eg %f in you case.
    6. It would be better not to make global declarations for all the variables.
    Last edited by BEN10; 09-12-2009 at 06:59 AM.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    I figured it out thanks for all the help.
    Code:
    #include<stdio.h>
    int choice; // 1 for Celsius, 2 for Fahrenheit. //
    int a; // Temperature value//
    int ctemp; // Celsius //
    int ftemp; // Fahrenheit //
    
    int main( void )
    
    {
        printf( "This program converts from Celsius to Fahrenheit or Fahrenheit to Celsius.\n" );
        printf( "Enter 1 for Fahrenheit to Celsius or 2 for Celsius to Farenheit.\n" );
        scanf("%d", &choice); // User input //
    
    	
    if (choice == 1)
    {
        printf( "Enter degrees in Fahrenheit.");
        scanf ("%d", &a); // User input //
        ctemp = (5.0/9.0) * (a-32); //  function //
        printf ( "%d", ctemp); // Degrees in celsius //
    } 
        else if (choice == 2)
        {
        printf( "Enter degrees in Celsius.");
        scanf ("%d", &a); // User Input //
        ftemp = (9.0/5.0) * a +32; // function //
        printf( "%d", ftemp); // Degrees in Fahrenheit //
        }
    	else 
        printf( "Only can choose 1 or 2" );
    
    return 0; //end program//
    }

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Still your commenting is wrong.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. A confusing problem with my linked list
    By edd1986 in forum C++ Programming
    Replies: 0
    Last Post: 03-19-2006, 10:33 AM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM

Tags for this Thread