Thread: Please help converting Fahrenheit to Celsius

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    16

    Please help converting Fahrenheit to Celsius

    Hi I really need help I'm having a hard time trying to convert celsius to fahrenheit. As it stands right now I'm in an endless loop. I have to use the FOR statement in this program but its not working.


    /* This program converts Fahrenheit to Celsius in increment of 5 */
    int main()

    Code:
    {
    int main()
    /* This program converts Fahrenheit to Celsius in increment of 5 */
    {
    int Celsius,degreesF;
    
    float Fahrenheit;
    
    printf("\nThis program will convert Fahrenheit to Celsius\n\n");
    
    
    printf("Enter the degrees in Fahrenheit: ");
    scanf("%d", &degreesF);
    
                Celsius =5;
    
    printf("Fahrenheit  Celsius \n");
    printf("----------  ------- \n");
    
    for ( Celsius=0; Celsius= (degreesF*5); Celsius = degreesF+ 5)
    {
               	
    
      
     Celsius = (5.0 /9.0)*(Fahrenheit - 32);
    printf("%5d %10d\n", degreesF );
    } 
                Celsius = Celsius + 5;
    
    
    getchar();
    getchar();
    getchar();
    getchar();
    
    return 0;
    
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are assigning a value in the part of your loop where you should be controlling how it stops. (Hint: The middle portion. = is assignment == is comparison.)


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Also, instead of just putting some large number of getchar()s at the end of your program, you could write something like this:
    Code:
    while(getchar() != '\n') {}
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    16

    need explanation

    Quote Originally Posted by quzah View Post
    You are assigning a value in the part of your loop where you should be controlling how it stops. (Hint: The middle portion. = is assignment == is comparison.)


    Quzah.
    What do you mean by assignment? I'm trying to understand this.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I was pretty clear there. Here, maybe this will help:
    Code:
    for ( Celsius=0; Celsius= (degreesF*5); Celsius = degreesF+ 5)

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    16
    Quote Originally Posted by quzah View Post
    I was pretty clear there. Here, maybe this will help:
    Code:
    for ( Celsius=0; Celsius= (degreesF*5); Celsius = degreesF+ 5)

    Quzah.
    Hi again
    Ok I got that part which I do appericate your help but for some reason my program is still in an endless loop. If you see something wrong with it could you please explain.

    Thank you

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A for loop has three basic parts:

    1) The initial assignment:

    someVariable = 0; //for example

    2) The test to see if the loop should end:

    someVariable < number;
    someVariable == number;

    are common tests in a for loop.

    3) The increment (or decrement):

    someVariable++;
    someVariable--;

    are common

    In your program's case, the end test condition is:

    1) Wrong, since it needs a comparison operator ==, not an assignment operator =.
    2) The range is far too high. If the user enters 70 degrees Fahrenheit, your loop will continue until the temperature reaches 350 degrees Fahrenheit (5 * 70).

    That's way past T shirt weather!

    Figure out what your assignment requires for a reasonable range, and then set it up with a test comparison ==, not an assignment, OK?

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    16
    Quote Originally Posted by Adak View Post
    A for loop has three basic parts:

    1) The initial assignment:

    someVariable = 0; //for example

    2) The test to see if the loop should end:

    someVariable < number;
    someVariable == number;

    are common tests in a for loop.

    3) The increment (or decrement):

    someVariable++;
    someVariable--;

    are common

    In your program's case, the end test condition is:

    1) Wrong, since it needs a comparison operator ==, not an assignment operator =.
    2) The range is far too high. If the user enters 70 degrees Fahrenheit, your loop will continue until the temperature reaches 350 degrees Fahrenheit (5 * 70).

    That's way past T shirt weather!

    Figure out what your assignment requires for a reasonable range, and then set it up with a test comparison ==, not an assignment, OK?
    The assisgement I was given just ask the end user to enter in ANY temp. to convert F to C. My teacher told me to go fish.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    printf("%5d %10d\n", degreesF );
    You should compile with warnings on.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You will have to figure out what you want for your top temperature. I'm sure your assignment doesn't want the range to go up to 5 times the temperature entered by the user.

    You just had the number 5 on your brain, I'll bet.

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    16
    Quote Originally Posted by Adak View Post
    You will have to figure out what you want for your top temperature. I'm sure your assignment doesn't want the range to go up to 5 times the temperature entered by the user.

    You just had the number 5 on your brain, I'll bet.
    The assignment reads convert Fahrenheit to Celsius temperature in increments of 5 degrees. The initial value fo the Fahrenheit temperature and the total conversions to be made are to be requested as user nput during the program execution.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Compute the stop value first.
    Code:
    stopvalue = computeFtoCforinputvalue();
    for( x = 0; x < stopvalue; x += 5 )
        ...output...

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Please look at the code
    and enjoy C/C++ coding.......


    Code:
    #include <stdio.h>
    
    // Function for converting fahrenheit to celsuis
    float fahrenheit_to_celsius(const float temprature) {
      return (float)(temprature - 32) * (5.0 / 9.0);
    }
    
    // Function for converting celsuis to fahrenheit
    float celsuis_to_fahrenheit(const float temprature) {
      return (float)((temprature * (9 / 5)) + 32);
    }
    
    int main() {
      int choice             = 0;
      float start_temprature = 0;
      float end_temprature   = 0;
    
      printf("1... Fahrenheit to Celsuis\n");
      printf("2... Celsuis to Fahrenheit\n");
      printf("Please enter your choice\n");
      scanf("%d", &choice);
    
      printf("Please enter Start Temprature\n");
      scanf("%f", &start_temprature);
    
      printf("Please enter End Temprature\n");
      scanf("%f", &end_temprature);
    
      while (start_temprature <= end_temprature) {
        switch (choice) {
          case 1:
           printf("fahrenheit == %f || celsuis == %f\n",
                  start_temprature,
                  fahrenheit_to_celsius(start_temprature));
          break;
    
          case 2:
            printf("clesius  == %f || fahrenheit == %f\n",
                   start_temprature,
                   celsuis_to_fahrenheit(start_temprature));
          break;
    
          default:
            printf("[warning] Choice is not valid\n");
          break;
        }
    
        start_temprature += 5;
      }
    
      return 0;
    }

  14. #14
    Registered User
    Join Date
    Oct 2009
    Posts
    16
    Quote Originally Posted by RockyMarrone View Post
    Please look at the code
    and enjoy C/C++ coding.......


    Code:
    #include <stdio.h>
    
    // Function for converting fahrenheit to celsuis
    float fahrenheit_to_celsius(const float temprature) {
      return (float)(temprature - 32) * (5.0 / 9.0);
    }
    
    // Function for converting celsuis to fahrenheit
    float celsuis_to_fahrenheit(const float temprature) {
      return (float)((temprature * (9 / 5)) + 32);
    }
    
    int main() {
      int choice             = 0;
      float start_temprature = 0;
      float end_temprature   = 0;
    
      printf("1... Fahrenheit to Celsuis\n");
      printf("2... Celsuis to Fahrenheit\n");
      printf("Please enter your choice\n");
      scanf("%d", &choice);
    
      printf("Please enter Start Temprature\n");
      scanf("%f", &start_temprature);
    
      printf("Please enter End Temprature\n");
      scanf("%f", &end_temprature);
    
      while (start_temprature <= end_temprature) {
        switch (choice) {
          case 1:
           printf("fahrenheit == %f || celsuis == %f\n",
                  start_temprature,
                  fahrenheit_to_celsius(start_temprature));
          break;
    
          case 2:
            printf("clesius  == %f || fahrenheit == %f\n",
                   start_temprature,
                   celsuis_to_fahrenheit(start_temprature));
          break;
    
          default:
            printf("[warning] Choice is not valid\n");
          break;
        }
    
        start_temprature += 5;
      }
    
      return 0;
    }
    Thank you for all your help but I have to you a FOR statement in this program.

  15. #15
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    hey victory1
    Why u stuck with for plz can u tell me that First

    fOR for loop() here in the code just replace


    while (start_temprature <= end_temprature) {
    with

    for (float temprature = start_temprature; temprature <= end_temprature; temprature+=5) {

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 09-25-2009, 11:15 AM
  2. Conversion from Fahrenheit to Celsius problem
    By -Syntax in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2008, 08:56 PM
  3. Please Help!!!
    By F1uT3 in forum C++ Programming
    Replies: 16
    Last Post: 06-24-2005, 01:20 PM
  4. celsius to fahrenheit conversion
    By chipster18 in forum C Programming
    Replies: 11
    Last Post: 03-26-2003, 07:38 AM