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