Newbie Question

This is a discussion on Newbie Question within the C Programming forums, part of the General Programming Boards category; my assignment is to write a program that asks for a temperature in celcius, then spits out a value in ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    18

    Newbie Question

    my assignment is to write a program that asks for a temperature in celcius, then spits out a value in fahrenheit, kelvin and reaumur.

    This is what i have done:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        float Temp;
        float Fahr, Kelvin, Reamur;
    
        printf(" Celcius >> ");
        scanf("%f", &Temp);
    
        Fahr = (Temp * 9/5) + 32;
        Reamur = Temp * 0.8;
        Kelvin = Temp + 273 ;
        printf("\n Fahrenheit >> %f\n", Fahr);
        printf("\n Reamur >> %f\n", Reamur);
        printf("\n Kelvin >> %f\n", Kelvin);
    
        printf("\n\n\n");
        system("pause");
        return 0;
      }
    Now the lecture want me to rewrite the code using either " ?: ". i'm not sure how this ?: actually works. Could someone please explain to me ??

    Thx in advanced

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Well you would have needed to use if / else to make sense of using ?:

    Code:
    if ( a < b ) x = 1; else x = 2;
    
    x = a < b ? 1 : 2;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    ?: is OK for small conditions, IMO, but NEVER nest it! It's the most ugly thing to dubug!!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's not any harder to debug than an if-else ladder. Just use whitespace like you would with with your if-else tree. Your code is only as ugly as you make it.


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

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    ic . many thanks guys. understood very well now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 05:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 03:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 03:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21