c - read signed number from a char vector -


i got problem , need help, i'll thankful: when input negative number using fgets() , try verify string input user in char array read fgets() number isdigit() positive numbers, there way read negative numbers. (i need read numbers can use scanf because when read character makes me mess)

here's part of code:

char op[30]; int a[30];  int text_ssalto() {     size_t len = strlen(op);     fgets(op, sizeof(op), stdin);     len = strlen(op);     if (len > 0) {         op[len - 1] = '\0';     }      if (isdigit(*op)) {         sscanf(op, "%d", &a[x]);     }      return 0; } 

i think maybe somewhere else wrong in code, here sample similar yours , working. did include correct headers?

#include "ctype.h" #include "stdlib.h" #include "stdio.h"  int main () {         char op[30];         fgets(op, sizeof(op), stdin); /* input -11 */         printf("%s\n", op); /* output -11 */         if (isdigit(*op)) {             printf("wrong\n"); // never got printed if input negative             sscanf(op, "%d", &a[x]); // read positive number              }         else {               sscanf(op + 1, "%d", &a[0]); // a[0] has positive part               a[0] = -a[0]; // make negative if want.         }          return (0); } 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -