-1

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;

}
Markovnikov
  • 41
  • 4
  • 13
  • 3
    `scanf` returns the number of successful conversions, so the *good* return value in your case is 2. Also, if the user *does* enter `101c`, none of the conditions will be met because `'c'` is not the same as `'C'` – user3386109 Oct 11 '16 at 05:08
  • 1
    `scanf_s("%lf %c", &temp, &CorF, 2) == 1` --> `scanf_s("%lf %c", &temp, &CorF, 1) == 2` – BLUEPIXY Oct 11 '16 at 05:10
  • 1
    `temp < 0 && temp > 100` --> `temp > 0 && temp < 100` – BLUEPIXY Oct 11 '16 at 05:14
  • 2
    When you determine "my loop is not being entered", instead of spending time posting a question here, ask yourself "why is that?"... The answer, if you're not sure, is to experiment. Ask "I wonder what value `sscanf_s` is returning". Use a debugger, or change the code to store and display the result. This basic skill of exploring and being inquisitive will save you from asking 99% of the questions that you might otherwise ask on this site. – paddy Oct 11 '16 at 05:22

1 Answers1

0

There is a lot of mistake in your code. While is a loop, it is used for looping. When you are checking it with scanf_s, it will check for the value while entering every time in the loop. Instead of using while you should use if statement.

#include<stdio.h>
int main(void)

{


double temp;
char CorF;

printf("Please enter a temperature, such as 31 F:");
int i=scanf("%lf %c", &temp, &CorF);
if(i==2)
{

    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;
}

You can use this code. From scanf: On success, the function returns the number of items successfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens. In the case of an input failure before any data could be successfully read, EOF is returned.

panshul
  • 103
  • 2
  • 9