How to scanf a negative number?

0 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);

}





Post a Reply

Replies

Oldest  Newest  Rating
+1 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);

}



0 osh Ranw · June 2, 2015
okay. i now try to use the funtions used by you @c student
0 osh Ranw · June 2, 2015
@Dante Sparda
yeah that's the problem if i use %d..
+1 Dante Sparda · June 1, 2015
This is going to break as soon as the user inputs anything that is not an integer

#include

int main(){
int a,b,c,scan,temp;
a = b = c = 0;
printf("Enter your numbers, Press '$' to quit \n");
while(1){
temp = scanf(" %d",&scan);
if(temp != 0){
if(scan > 0){
a++;
}else if(scan < 0){
b++;
}else{
c++;
}
} else {
printf("you have entered Positive:%d, Negative:%d, Zero:%d",a ,b ,c);
break;
}
}
return 0;
}
+1 c student · June 1, 2015
Maybe something like this?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_DIGITS 10

int 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;
}
0 osh Ranw · June 1, 2015
anyone :(
  • 1

C

107,082 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator