-2

How can I input number (int) without spaces?

I want to stop scanf as long as the input isn't zero. How can I do it?

#include <stdio.h>
int main()
int num;
{
    while(num!=0){
        printf("Enter the account activity: ");
        scanf("%d", &num);
        if(num==0){
            break;
        }   
    }
}
klutt
  • 30,332
  • 17
  • 55
  • 95
Aspis
  • 55
  • 5
  • 2
    Where's num declared? – Hatted Rooster Sep 22 '18 at 13:22
  • I update the code – Aspis Sep 22 '18 at 13:26
  • Do you expect the input to have no spaces at all, not even between different numbers? What characters separate the numbers—tabs, commas, something else? Are there newline or return characters? Or is the input just a stream of digits with no other characters at all? – Eric Postpischil Sep 22 '18 at 13:27
  • I mean without newline, just space but not newline – Aspis Sep 22 '18 at 13:29
  • Your question needs to be more descriptive. Guessing from your printf statement, you are expecting to process a dollar amount. I would assume you expect to receive a multi-digit input. I assume you want the loop to stop when someone enters a 0 amount for the amount..... And I see you just changed the printf as I was typing this comment..... – dernst Sep 22 '18 at 13:30
  • You want to stop scanf read input after space is entered? Use getchar instead of scanf. Then use sscanf to parse the string. – re_things Sep 22 '18 at 13:31
  • I want to receive a few numbers. like: Enter the account activity: 12 432 -17 etc. – Aspis Sep 22 '18 at 13:31
  • Possible duplicate of [Putting numbers separated by a space into an array](https://stackoverflow.com/questions/9599794/putting-numbers-separated-by-a-space-into-an-array) – zdimension Sep 22 '18 at 13:33
  • How can I do that without array? – Aspis Sep 22 '18 at 13:33
  • How do you want to store the numbers then? – zdimension Sep 22 '18 at 13:34
  • I want to store numbers until the user type 0 – Aspis Sep 22 '18 at 13:35
  • 2
    @EladAspis yes, but *where* do you want to store the numbers if not in an array - your `num` can only hold one number at one time. – Swordfish Sep 22 '18 at 13:36

4 Answers4

1

I took the liberty of massaging your code a little bit, and assuming you want a sum...

#include <stdio.h>

int main(void) {
    int num, sum = 0;
    printf("Enter the account activity: ");
    fflush(stdout);
    for (;;) {
        if (scanf("%d", &num) != 1) break;
        if (num == 0) break;
        sum += num;
    }
    printf("Result is %d.\n", sum);
    return 0;
}
pmg
  • 106,608
  • 13
  • 126
  • 198
0

Rather than using scanf use something else, example:

#include<stdio.h>
#define MAX 100

int main(void)
{
    int i,count=0;
    int num=0, block=0;
    int sign=1;
    int arr[MAX] = {[0 ... MAX-1]=-1};

    while((i=getchar())!=EOF)
    {
         if(block==0)         //FIRST THING TO BE ENTERED BY USER IS SIGN OF INTEGER
         {
             if(i=='-')
                 block=1;
             sign=-1;
             continue;
         }
         if((i>'0')&&(i<='9'))
         {
             arr[count++]= i -'0';
         }
         else 
             break;
    }

            i=0;
    printf("The number is: ");
    while(i<count)
    {
        num=arr[i++]+num*10;
    }
    printf("%d",sign*num);
    return 0;
}

Dear downvoter - please give me a reason for downvote at least

Gaurav
  • 1,570
  • 5
  • 17
0
/*Program that prints and stores(last integer value) and terminates on input value of 0 */
#include <stdio.h>
int main(){
int num=1; 
while(num!=0){
    printf("\nEnter the account activity: ");
    scanf(" %d", &num);
    if(num==0)
        break;   
 }
return 0;
}
Zahid Khan
  • 2,130
  • 2
  • 18
  • 31
-1
int main(void)
{
    int num;
    int ch = 0; // used for reading and discarding garbage from stdin

    printf("Enter the account activity: ");
    do {
        if(scanf("%d", &num) == 1) // check if an in could be read successfull
            printf("%d\n", num);   // if so, print it.
        else {                     // otherwise
            // read characters from stdin until EOF is reached
            while ((ch = getchar()) != EOF)
                if (isdigit(ch)         // if the last character 
                    || ch == '+'        // was a valid character to
                    || ch == '-') {     // begin a new int,
                    ungetc(ch, stdin);  // put it back into the stream
                    break;              // and break the while
                }
        }
    } while (num && ch != EOF);
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43