1

I'm running this program in C to convert Fahrenheit to Celsius and need to accept only integer value from the user.

Please tell me how I can modify this?

int main() {
    int x;
    double y;
    while(x>0) {
        printf("Enter the temperature in Fahrenheit:");
        scanf("%d", &x);
        y=((x-32)/1.8)
        printf("%f\n",y);
    }
}
2
  • 2
    Please initialise x before you test it (and enable compiler warnings). Commented Sep 9, 2015 at 0:40
  • 2
    First, the condition of your while loop is using the variable x before it has been assigned a value. Use the return value of scanf if you want to check whether it has successfully read an integer Commented Sep 9, 2015 at 0:43

2 Answers 2

3

The reason your code does not work is that sometimes scanf does not read anything, so it does not modify x.

You know that scanf read something by checking its return value. It returns the number of "scanned" items. In this case, the number must be 1.

When scanf returns 0 instead, you should read and discard the data in the buffer. You do it by supplying %*[^\n] format specifier, which means "read and discard input up to '\n' character. The complete snippet that reads an int until success looks like this:

while (scanf("%d", &x) != 1) {
    printf("Please enter a valid number:");
    scanf("%*[^\n]");
}

Note: It goes without saying that you should fix your syntax error with the missing semicolon ; on the line that computes y.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response, the code is working fine as long as I'm entering integer value but goes in infinite loop when I enter something else and hence in while(x>0) i need some expression that'll check if it's integer value and I need to add the code to exit if entered value isn't integer.
-1
  • You can use below code.

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
     int x;
     double y;
     char str1[5];
    int num1,i;
    bool yes = true;
      while(x>0) 
      {
          printf("Enter the temperature in Fahrenheit:");
          scanf("%s",str1);
          for(i=0;str1[i]!='\0';i++)
          if(!(str1[i]>=48&&str1[i]<=56))
          {
             printf("The value is invalid \n");
             yes = false;
          }
          num1 = atoi(str1);
    
          if(yes == true)
          {
            printf("This Number is %d\n",num1);
            y=((num1-32)/1.8);
            printf("%f\n",y);
           }
       }
    }
    

2 Comments

Code would be clearer, less error prone (and correct) with if(!(str1[i]>='0'&&str1[i]<='9')). str1[i]<=56 is certainly incorrect.
1) int x; ... while(x>0) replicates OP's failure to initialize x. 2) char str1[5]; ...scanf("%s",str1); does not properly limit input to 4 characters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.