c - Convert to negative integer by reading characters -


this program reads data character character until finds character digit , converts integer.

#include <stdio.h>  int main(void) {    char ch = getchar();   printf("type data including number");   while(ch < '0' || ch > '9') //as long character not digit.   {     ch = getchar();   }    int num = 0;   while(ch >= '0' && ch <= '9') //as long digit   {     num = num * 10 + ch - '0'; //convert char digit integer     ch = getchar();   }   printf("number %d\n",num);  } 

this program finds positive integers. want program find negative integers aswell or floating point number. how make program that? tried using if statement inside while loop looks digit didnt work me.

it appears though fscanf better solution


Comments