lundi 10 octobre 2016

States of Water (temperature program) - C programming

I am working on a program where I have to use boolean operators in the if/else to get a temperature value and degree symbol from the user and print out the state (ice, liquid, steam) the water is in.

Example input & output:

   Enter temperature, such as 31 F: 101c
   Water is steam at 101c.

My code is posted below. It seems as if it should work, but I think I am not doing the input correctly and thus the loop is not being entered. Not sure how to fix it.

#include <stdio.h>


int main(void)
{

    double temp;
    char CorF;

    printf("Please enter a temperature, such as 31 F:");

    while (scanf_s("%lf %c", &temp, &CorF, 2) == 1)
    {

        if (temp <= 32 && CorF == 'F')
        {
            printf("Water is ice at %lf %c.\n", temp, CorF);
        }
        else if (temp >= 212 && CorF == 'F')
        {
            printf("Water is steam at %lf %c.\n", temp, CorF);
        }
        else if (temp > 32 && temp < 212 && CorF == 'F')
        {
            printf("Water is liquid at %lf %c.\n", temp, CorF);
        }
        else if (temp <= 0 && CorF == 'C')
        {
            printf("Water is ice at %lf %c.\n", temp, CorF);
        }
        else if (temp >= 100 && CorF == 'C')
        {
            printf("Water is steam at %lf %c.\n", temp, CorF);
        }
        else if (temp < 0 && temp > 100 && CorF == 'C')
        {
            printf("Water is liquid at %lf %c", temp, CorF);
        }
        else
        {
            // blank line!
        }
    }

    return 0;

}

Aucun commentaire:

Enregistrer un commentaire