0

I am writing this post because I am getting stucked trying to get all the negative numbers using sscanf. I code the following:

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

#define M 1000

int main(void)
{
    char a;
    char enteros[M];
    int output[M];
    int newline = 0;
    int count = 0;
    int x = 0;
    int index = 0;
    int num = 0;
    char *str;

    while (fscanf(stdin, "%c", &a) != EOF)
    {
        enteros[count] = a;
        count++;
    }

    enteros[count] = '\0';

    str = enteros;

    while (*str)
    {
        x = 1;
        if (sscanf(str, "%d%n", &num, &x) == 1)
        {
            output[index] = num;
            newline = 0;
            index++;
        }

        str += x;

        for (; *str; str++)
        {
            if (*str >= '0' && *str <= '9') /* positive value */
                break;
            if (*str == '-' && *(str + 1) >= '0' && *(str + 1) <= '9') /* negative */
                break;
            else if (!newline && isalpha((unsigned int)*str))
            {
                while (index != 0)
                {
                    printf(" %d", output[index - 1]);
                    index--;
                }
                printf("\n");
                newline = 1;
            }
        }
    }

    return 0;
}

My idea is to read from an input of integers and characters, get only integers (negatives and positives) into an array and, finally, reverse each number. But when I entered the following input:

1 8 4000 2 -23 end 51 87 end –4
–3 2 end

I get the following output:

 -23 2 4000 8 1
 87 51
 2 3 4

I do not understand why I only get -23 instead of -23, -4 and -3 as the code received. Thanks for the help!

javiLPot
  • 25
  • 4
  • You have not NUL-terminated the string you built. After the input loop you need `enteros[count] = '\0'`. But also, if you have space/newlines in the input (i.e. entering character by character) then `"%c"` will read them all. Unless you want to include spaces too, use `" %c"`. But why are you going "round the houses"? Why didn't you go directly to `if(scanf("%d%n", &num, &x) == 1)`? – Weather Vane Oct 26 '22 at 22:22
  • When your code runs `if (*str == '-' && *(str + 1) >= '0' && *(str + 1) <= '9')`, it does nothing different from a positive value. – Andrew Henle Oct 26 '22 at 22:23
  • @javiLPot What is "end"? – Vlad from Moscow Oct 26 '22 at 22:23
  • 1
    Would be a perfect time to make friends with `fgets()`. – David C. Rankin Oct 26 '22 at 22:39
  • As Vlad asked, what is `end`? Is this just in the post as shorthand for `\n` or newline? If so, you could edit the code block and add real newlines. I don't see any code that looks for a _string_ that is `end`. If so, looping `fscanf(stdin,"%d",&output[index]);` (or `sscanf`) would be less cumbersome. – Craig Estey Oct 26 '22 at 22:51
  • Something using `fgets()` like [How to read only integers from a file with strings, spaces, new lines and integers in C](https://stackoverflow.com/a/42990132/3422102). Though, character-by-character is fine, as noted above, without a `'\0'` at the end of `enteros[]`, it can never be treated as a C-string. `fgetc()` is probably a better choice than `fscanf()` with `"%c"`. – David C. Rankin Oct 27 '22 at 00:38

0 Answers0