0

I have this function process_input that basically gets a line of a file and prints the numbers of the line, if it has 2 numbers it prints those numbers and 0 else it prints the 3 numbers.

My problem is that for some reason its printing the lines repeatedly with a -1 token and I get why. Is there any error in my logic or something that im missing, is there a more efficient way of doing this? What I really want is to get the number from the file.

file:

4 4
6 4
5 10
10 3
4 8
1 2 5
2 3 6
2 4 2
3 4 1

My output:

4 4 - tokens: 2
6 4 0 - tokens: 2
5 10 0 - tokens: 2
10 3 0 - tokens: 2
4 8 0 - tokens: 2
1 2 5 - tokens: 3
1 2 5 - tokens: -1
2 3 6 - tokens: 3
2 3 6 - tokens: -1
2 4 2 - tokens: 3
2 4 2 - tokens: -1
3 4 1 - tokens: 3
3 4 1 - tokens: -1
3 4 1 - tokens: -1

Correct output:

4 4 - tokens: 2
6 4 0 - tokens: 2
5 10 0 - tokens: 2
10 3 0 - tokens: 2
4 8 0 - tokens: 2
1 2 5 - tokens: 3
2 3 6 - tokens: 3
2 4 2 - tokens: 3
3 4 1 - tokens: 3

Program:

#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <stack>

using namespace std;

void process_input(int** &graph, int &V, int &E)
{
    int n,k;
    char line[6];

    fgets(line,6,stdin);
    int tokens = sscanf(line, "%d %d", &n, &k);
    printf("%d %d - tokens: %d\n",n,k,tokens);

    int x,y,w = 0;
    while (fgets(line,6,stdin) != NULL)
    {
        tokens = sscanf(line, "%d %d %d", &x,&y,&w);
        printf("%d %d %d - tokens: %d\n",x,y,w,tokens);
    }
}

int main(int argc, char **argv)
{
    int V, E;
    int **graph;
    process_input(graph,V,E);
    exit(0);
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
Martim Correia
  • 483
  • 5
  • 16
  • 2
    C-style strings are null-terminated, which means `char line[6];` can store five meaningful characters before the null terminator. `fgets(s, size, stream)` reads at most one less than `size` characters from `stream`, and this includes the newline character, if any. So the line that contains `1 2 5` is read up to `5` and no newline character is read, because there is no place to store it. The next call to `fgets` reads just the newline character and no numbers. You may want to increase that `6` somewhat. How about using , um, I dunno, `1024` instead? – n. m. could be an AI May 16 '21 at 15:26
  • 2
    On a different note, you may want to use C++ facilities such as `std::string` and `iostreams` instead of the low-level C baggage, and not worry about these minute details. – n. m. could be an AI May 16 '21 at 15:28
  • Thanks man, i can't believe i was making such an ameuter mistake, i chose 6 because i thought it was small enough to fit any line of that file. By the way, im only using c++ because of the data structures, because i don't really know how to program in c++. – Martim Correia May 16 '21 at 16:37

1 Answers1

1

Problem is that once you enter numbers in a loop you are not clearing your stdin buffer. You have mix of C and C++ here.

in C++ you would do this:

while (fgets(line,6,stdin) != NULL)
{   
    tokens = sscanf(line, "%d %d %d", &x,&y,&w);
    printf("%d %d %d - tokens: %d\n",x,y,w,tokens);
    std::cin.ignore();
    std::cin.clear();
}

you have to include iostream. Using C way is well not perfect, but here is SO post about it

Using hex output on closer inspection (this is for C, you are overwriting \n. Which makes scanf function fail that's why you are getting -1 and why is it not clearing buffer by itself.

int n,k;
char line[7];

fgets(line,7,stdin);
int tokens = sscanf(line, "%d %d", &n, &k);
printf("%d %d - tokens: %d\n",n,k,tokens);

int x,y,w = 0;                                                                                                                                            
while (fgets(line,7,stdin) != NULL)
{
    tokens = sscanf(line, "%d %d %d\n", &x,&y,&w);
    printf("%d %d %d - tokens: %d\n",x,y,w,tokens);
}