Why does scanf break my code!

Why does scanf break my code!
0

#1

I’m trying to write a basic code to count the number of words in a line. And I’ve to do multiple test cases. If I use scanf to get the number of testcases instead of what I’m doing, it counts the input from scanf as the first string. And so I don’t get the desired input if I use scanf.

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

int main(void)
{	
	int i;
	char val[10];
//Get number of testcases
	gets(val);
	int T = atoi(val);
	int T1 = T; 
    int count[T];
    for (i=0;i<T;i++)
    {
        count[i]=1;
    }
    while(T>0)
    {
		char line[200];
		int j=0;
		fgets(line,200,stdin);
		int length = strlen(line);
		do
		{	
			if(line[j]==' ')
			count[T-1]++;
			j++;	
		}while (j<=length);
		T--;
	}
		
	for(i=T1-1;i>=0;i--)
	{
		printf("No of words %d \n",count[i]);
	}
	
	
	return 0;
}


#2

First, do everyone a favour and start indenting your code properly.

It seems scanf doesn’t consume the input, so you’ll just have to fast forward the stream yourself — by the amount read, which is what scanf returns if there was no error (but in items, not chars). So really, gets to use in this case.


#3

Yeah sorry about the indentation, I fixed it. But, I don’t understand what you mean by “scanf doesn’t consume the input, so you’ll just have to fast forward the stream yourself”. I understand that scanf returns the number of items successfully read, but how does that affect the part where I read the strings?


#4

When you look at stdin, it still contains the same values you read with scanf. You’d have to advance a pointer, look past those few characters.


#5

there’s no scanf shown in your code - so I have to guess - which is no fun btw - maybe you’re not processing the first newline correctly?

scanf("%u\n", &T);

#6

btw man gets to read why gets should never be used - moot once you get scanf to work


#7

I’m using gets() to get the value(val) for T (the number of test cases) because when I use scanf("%d",&T)
and say I input “2” on prompt, I can only enter one sentence and not 2 .Also on using scanf it seems fgets somehow takes “2” as the first string and the next sentence as the 2nd string and gives me the output as
"No of words 1
No of words X (say)"
That’s what I want to know what is causing this. I had a test yesterday and this only problem wasted half of my time.


#8

How do I do that? Sorry if that’s a noob question.


#9

thanks for including your scanf snippet - it’s exactly what I surmised - did you miss the scanf usage I provided in my first post?


#11

I’ve never really used %u format specifier. “\n” in scanf fixed the problem. Facepalm!
Can you explain what’s happening?


#12

It’s the newline that did it, not the change of signedness. Before you weren’t reading the whole line.


#13

Don’t understand why scanf shouldn’t work. I’d do

int main(void)
{	
	int i;
	int T;
    scanf("%d", &T);
	int T1 = T; 
    int count[T];
    for (i=0;i<T;i++)
    {

#14

Yeah that’s what I was doing, but it doesn’t work. Using the new line “\n” after %d fixes the issue though.


#15

first like with any libc function man scanf - reading man pages is essential if you intend to program in C

the scanf format says exactly what to consume from the input - there are just 3 choices - whitespace, a literal character or a % specifier

a %d does this

d      Matches an optionally signed decimal integer; the  next  pointer
       must be a pointer to int.

after the input number is consumed, the newline remains unconsumed - I matched it with \n - a literal character - you can match it other ways within the scanf format or consume it through other means like getc, fgets etc

please mark the question solved if the solution has been provided