tags:

views:

154

answers:

4

I was wondering how I could prompt the end-user of my program to type in a value they want to be converted from Fahrenheit into Celsius in C.

Basically, since I'm a total n00b and I'm writing amazing "programs" such as this one:

    //Simple program to convert Fahrenheit to Celsius

    int main (int argc, char *argv[])
    {
     double celsius, fahrenheit, result;

     celsius = result;
     fahrenheit = 27;
     result = (fahrenheit - 32) / 1.8;

     printf("27 degress Fahrenheit is %g degrees Celsius!", result);

     return 0;
    }

I would like to add some actual "functionality" to it if you know what I mean. Instead of just making this a test program where really it just shows off some simple arithmetic expression evaluating, I would like to actually make it somewhat mildly useful.

Anyway, I was wondering if I could use the function listed in the scanf(3) Man page to aid me in the recognition of user-inputted data, and then somehow store it into the Fahrenheit variable.

Now, it would really be cool if the program, upon running, could prompt the end-user with a question asking whether he or she would like to convert from Celsius to Fahrenheit or from Fahrenheit to Celsius, but let's just take it one step at a time, and I'll wait until I read the chapter in my book about "Making Decisions"! :)

UPDATE:

Removes useless variable result as pointed out by kiamlaluno:

    //Simple program to convert Fahrenheit to Celsius

    int main (int argc, char *argv[])
    {
 double fahrenheit, celsius;

 fahrenheit = 27;
 celsius = (fahrenheit - 32) / 1.8;

 printf("27 degress Fahrenheit is %g degrees Celsius!", celsius);

 return 0;
    }

UPDATE UPDATE:

I've been trying to incorporate everyone's helpful suggestions posted here, but I'm running into more problems with my code:

//Simple program to convert Fahrenheit to Celsius and Celsius to Fahrenheit

int main (int argc, char *argv[])
{
int celsius, fahrenheit, celsiusResult, fahrenheitResult;
celsiusResult = (fahrenheit - 32)*(5/9);
fahrenheitResult = (celsius*(9/5)) + 32;
int prompt;

printf("Please press 1 to convert Fahrenheit to Celsius, or 0 to convert Celsius to Fahrenheit please:\n ");
scanf("%i", &prompt);
if(prompt == 1) {
    printf("Please enter a temperature in Fahrenheit to be converted into Celsius!:\n");
    scanf("%i", &fahrenheit);
    printf("%i degress Fahrenheit is %i degrees Celsius!", fahrenheit, celsiusResult);

}
else {
    printf("Please enter a temperature in Celsius to be converted into Fahrenheit:\n");
    scanf("%i", &celsius);
    printf("%i degreses Celsius is %i degrees Fahrenheit", celsius, fahrenheitResult);
}

return 0;
}

Everything's working great, except for the calculations themselves, which come out completely wrong.. For a second I thought this may have been because I changed the numbers themselves to integers types, but I made them doubles again and it was still kind of screwy.

Any thoughts?

+2  A: 

To use scanf() to read a double, you'd need to use the correct format string. It would be %lf for "long float" (where %f alone would read into a float). You could then read directly to a double. Same goes for printing it out.

int main (int argc, char *argv[])
{
    double fahrenheit, celsius;

    printf("farenheit? ");     /* write a prompt */
    scanf("%lf", &fahrenheit); /* read a double into the fahrenheit variable */

    celsius = (fahrenheit - 32) / 1.8;

    printf("%lf degress Fahrenheit is %lf degrees Celsius!\n", fahrenheit, celsius);

    return 0;
}

Note that this doesn't handle non-numeric inputs at all. You'd need to use other input techniques.

[edit]

You are certainly taking jumps in your code which seems like you're making a lot of progress. :)

To address your most current update, there are a couple of issues. Your code doesn't actually calculate anything, you applied the formula too soon. You should wait until fahrenheit or celsius have meaningful values (i.e., after the user has input the value to be converted). It would be a good idea to move these formulas into functions to perform the conversion. You should also stick with using double and not integers. You will not get the precision you want using integers.

double convert_fahrenheit_to_celsius(double fahrenheit)
{
    return (fahrenheit - 32)*(5/9);
}

double convert_celsius_to_fahrenheit(double celsius)
{
    return (celsius*(9/5)) + 32;
}

int main (int argc, char *argv[])
{
    double celsius, fahrenheit, celsiusResult, fahrenheitResult;
    int prompt;

    printf("Please press 1 to convert Fahrenheit to Celsius, or 0 to convert Celsius to Fahrenheit please:\n ");
    scanf("%i", &prompt);
    if(prompt == 1) {
        printf("Please enter a temperature in Fahrenheit to be converted into Celsius!:\n");
        scanf("%lf", &fahrenheit);

        /* now convert user-input fahrenheit to celsius */
        celsiusResult = convert_fahrenheit_to_celsius(fahrenheit);

        printf("%lf degress Fahrenheit is %lf degrees Celsius!", fahrenheit, celsiusResult);
    }
    else {
        printf("Please enter a temperature in Celsius to be converted into Fahrenheit:\n");
        scanf("%lf", &celsius);

        /* now convert user-input celsius to fahrenheit */
        fahrenheitResult = convert_celsius_to_fahrenheit(celsius);

        printf("%lf degreses Celsius is %lf degrees Fahrenheit", celsius, fahrenheitResult);
    }

    return 0;
}
Jeff M
Hey, thanks for keeping up with my problems! I just tried implementing your fixes, which didn't appear to work. Although, I noticed you put the functions outside of the main function. Does this have to do with any of the @interface or @implementation sections of Objective-C code? Should I be defining those functions in the @implementation after I declare them in the @interface section? I had read about this in my book, but that was dealing with only classes, and methods of those classes. I have't dealt with that unrelated to classes.
BOSS
I'm not too familiar with Objective-C but this applies to regular C code. If it requires you to place new functions elsewhere (say, a class), go ahead and do that. The point I was trying to get across was that the conversion formulas were prime candidates for putting in a separate function.
Jeff M
`%lf` is necessary for `scanf` but unnecessary for `printf`. Default argument promotions for variadic functions ensure that it's impossible to pass a `float` to `printf` (it's always promoted to a `double`), so plain `%f` was specified for doubles. The `l` modifier with floating point format specifiers has no effect.
R..
@R..: That's true, but I believe that as a beginner, it would be a good habit to pick up. So he can mirror the use of the format specifiers in both `printf()` and `scanf()`. Then when working with odd datatypes such as `short` or other "special" types, it won't be too much of a surprise. After gaining more experience, then he could use whichever is best for the situation.
Jeff M
+1  A: 

Ok if you want to prompt the user for input just use scanf with the tag %lf:

//Simple program to convert Fahrenheit to Celsius

int main (int argc, char *argv[])
{
    double fahrenheit, result;

    printf("Please enter a number for farenheit: ");
    scanf("%lf", &fahrenheit);
    result = (fahrenheit - 32) / 1.8;

    printf("27 degress Fahrenheit is %g degrees Celsius!", result);

    return 0;
}

As for prompting the user if he/she wants celcius or farenheit you need something called and if statement and else statement.

int main (int argc, char *argv[])
{
    double celsius, fahrenheit, result;
    int ForC;

    printf("1 for Celsius or 0 for Fahrenheit plz: ");
    scanf("%d", &ForC);
    if(ForC == 1) {
        scanf("%lf", &fahrenheit);
        result = (fahrenheit - 32) / 1.8;
        printf("fahrenheit to celsius:%lf", result);
    }
    else {
        scanf("%lf", &celsius);
        result = (celsius*9.0)/5.0 + 32;
        printf("celsius to farenheit:%lf", result);
    }

    return 0;
}
thyrgle
Cool! I like that if else statement. I can really see myself eventually doing a simple iPhone appplication based on this simple method. Obviously it would have to be adjusted, but I'm learning.
BOSS
@Neil Butterworth: Thnx, I always miss those.
thyrgle
A: 

Handling the possibly erroneous user input could be done by checking the return value of scanf(). It must be equal to the number of values expected (1 in this case). In other case, the input should be repeated.

mbaitoff
A: 

For your UPDATE UPDATE : Try doing the calculation after you get the value from the user. If not, the resulting value of celsius, fahrenheit, celsiusResult, fahrenheitResult is unknown.

Edward Leno