Thread: Beginner using C programming. Please see if you can help me.

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    1

    Question Beginner using C programming. Please see if you can help me.

    I have to create a program that converts temperature values from Fahrenheit to Celsius, ask the user to input the number of Fahrenheit temperature to be converted (n). Use a ‘for’ loop to read the temperatures from keyboard , output the original and converted temperatures in a tabulated format with two decimal points.
    Calculate and output the average temperature in Celsius with two decimal points.
    Below is what I have so far.... Am I right so far with what I have?

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <dos.h>
    #include <iostream>
    #include <string.h>
    void main ()
    {
        int fahr, cel;
        
        printf ("Enter the starting temperature in degrees Fahrenheit: ");
        scanf("%d", &fahr);
        for( x = 0; x < 25; x++ )
        {
        cel=(fahr-32)*5/9;
        printf("\n%d Fahrenheit is equal to %d degrees celsius\n\n",fahr,cel);
            
        }
    printf("\nPress any key to exit the program ...\n\n");
    getche();
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try to compile your program with a high warning level. What does your compiler report?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Raj 89's Avatar
    Join Date
    Nov 2012
    Location
    Chennai, TamilNadu
    Posts
    16
    Quote Originally Posted by Cookieb24 View Post
    ask the user to input the number of Fahrenheit temperature to be converted (n). Use a ‘for’ loop to read the temperatures from keyboard , output the original and converted temperatures in a tabulated format with two decimal points.

    If you need to ask temperature values for every n times, go for providing the scanf inside the for loop.

    Follow the below mentioned code and ask for any doubts.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    
    void main ( )
    {
        int n ;
        float fahr , cel ;    
    
        printf ("Enter the no.of.values for which conversion is needed: ");
        scanf("%d", &n);
        for( x = 0; x < n; x++ )
        {
        printf("\nEnter Fahrenheit value %d",x+1) ; 
        scanf("%f",&fahr) ;
        cel=(fahr-32)*5/9;
        printf("\n%f Fahrenheit is equal to %f degrees celsius\n\n",fahr,cel);
            
        }
    printf("\nPress any key to exit the program ...\n\n");
    getch();
    }
    Do as per this code. Do not include iostream.h in this file. iostream is for c++ format and stdio.h is for c format. Include either any one among those two.

  4. #4
    Registered User dariyoosh's Avatar
    Join Date
    Nov 2012
    Location
    Iran / France
    Posts
    38
    Quote Originally Posted by Cookieb24 View Post
    ... Use a ‘for’ loop to read the temperatures from keyboard , output the original and converted temperatures in a tabulated format with two decimal points.
    Then the lines 11 and 12 in your code should be moved inside the loop (assuming that you wish to read at most 25 times temperature from the kyboard, yet if I were you I would use a infinite loop which will end based on a specific value entered by the user).

    The lines 19 and 20 in your code, as I understand are useless, because once the for loop ends, the program reaches automatically its end point as would be no more statement to run.

    NOTE: If you choose to read values inside a loop with scanf don't forget to put a space character before %d in the format string

    Therefore:
    Code:
    scanf(" %d", &fahr);
    insteadof
    Code:
    scanf("%d", &fahr);
    Otherwise you will have problem because end line characters of precedent reads will be mixed during the next reads.

    Regards,
    Dariyoosh

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by dariyoosh View Post
    Otherwise you will have problem because end line characters of precedent reads will be mixed during the next reads.
    Not really.

    Example
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int a;
    
        while(1)
        {
            printf("Type 5 to exit\n");
            scanf("%d",&a);
            if(a == 5)
                    break;
            printf("ok\n");
        }
    
        return 0;
    }
    It is ok.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char a;
    
        while(1)
        {
            printf("Type e to exit\n");
            scanf("%c",&a);
            if(a == 'e')
                    break;
            printf("ok\n");
        }
    
        return 0;
    }
    not ok.

    Why?

    Because with the %d it will eat the special characters ( logical enough, because they are not numbers ).
    In case of %c, reading characters that is, what you said is needed

  6. #6
    Registered User Erikknight's Avatar
    Join Date
    Nov 2012
    Location
    Earth, Sol System.
    Posts
    2
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main ()
    {
        int fahr, cel, x;
         
        printf ("Enter the starting temperature in degrees Fahrenheit: ");
        scanf("%d", &fahr);
        for( x = 0; x < 25; x++ )
        {
        cel=(fahr-32)*5/9;
        }
        printf("\n%d Fahrenheit is equal to %d degrees celsius\n\n",fahr,cel);
        printf("\nPress any key to exit the program ...\n\n");
        getch();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Programming help beginner
    By wwrmiw in forum C Programming
    Replies: 8
    Last Post: 03-04-2011, 09:35 AM
  2. Beginner programming
    By Tigerdude in forum C Programming
    Replies: 4
    Last Post: 02-01-2005, 05:22 PM
  3. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM
  4. Beginner Programming Help
    By Beginner921 in forum C Programming
    Replies: 2
    Last Post: 02-03-2003, 02:31 AM
  5. C Programming Beginner Needs Help!!!
    By pauljones2k in forum C Programming
    Replies: 13
    Last Post: 12-03-2002, 02:48 PM