# How to scanf a negative number?

 osh Ranw · June 1, 2015 I have this question Write a program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros entered.so i code this ,my problem is i can't seem to enter a negative number,,also i can't change the scan to int because  i want to end the program when, '\$' sign is pressed.any suggestions? ``#include #include #include int main(){    char scan;    int a =0 , b=0 , c =0 ; printf("Enter Your Number,Press '\$' To Quit \n"); do{    scanf(" %c", &scan);       if(isdigit(scan)){                            if(scan > '0'){                                        a++;                      }else if(scan == '0'){                                        b++;                      }else if(scan < '0'){                                        c++;                      }       }else if(scan == '\$'){            printf(" you have entered Positive:%d Negative:%d Zero:%d ",a, c, b);            break;       }else{            printf("ENTER A NUMBER OR \$ TO QUIT \n");       } }while(1);}``

## Replies

 osh Ranw · June 1, 2015 anyone c student · June 1, 2015 Maybe something like this?``#include #include #include #define MAX_DIGITS 10int main(void) {    char str[MAX_DIGITS+1];    do {        if (scanf ("%10s", str)) {            printf ("String: %s\n" "Number: %d\n", str, atoi(str));        }    } while (strncmp (str, "\$", 1) != 0);    printf ("\'\$\' detected. Exiting.\n");        return 0;}`` osh Ranw · June 2, 2015 @Dante Spardayeah that's the problem if i use %d.. osh Ranw · June 2, 2015 okay. i now try to use the funtions used by you @c student osh Ranw · June 2, 2015 @c student i used the 2 funtions you mentioned and got it to working``#include #include #include #include int main(){    char scan[100];    int a =0 , b=0 , c =0 ;    printf("Enter Your Number,Press '\$' To Quit \n");do{        scanf(" %s", scan);        int val = atoi(scan);              if(val > 0){ a++;        }else if(val < 0){c++;        }else if(val == 0 && strncmp (scan, "0" , 1) == 0){                                        b++;         }else{                         printf("ENTER A NUMBER OR \$ TO QUIT \n");        }       }while(strncmp (scan, "\$", 1) != 0);printf(" you have entered Positive:%d Negative:%d Zero:%d ",a, c, b);}``
• 1

## C

129,940 followers