simple code but still wrong C program

#include <stdio.h>
int main()
{int u,a,b,i;char j;
scanf("%d",&u);

for (i=0;i<u;i++){
scanf("%c",&j);
scanf("%d",&a);scanf("%d",&b);}
}


----
it is a meaningless code,meaningless output....but what I want is that:
4
a 1 2
b 3 4
c 2 3
d 9 10......end here
but when I compile it ,
the output is not what I want
It is...
4
a 1 2
b 3 4.....then suddenly here
why???
You didn't post the whole code; I don't see any printf or other output functions.

Also, post using [code][/code] tags for readability.
They posted the whole code. That was all their input. The problem is that their loop ends two iterations early.

The problem has something to do with the way scanf reads characters (from what I've read here http://stackoverflow.com/questions/5453650/scaning-multiple-inputs-from-one-line-using-scanf ). Your scanning for the character didn't work correctly, so the loop ignores the second two inputs. It loops two more times (picking up the last two characters), and then it goes to the third iteration for you to put more input.

The article I put above suggests reading a string, and then taking the first character.
Last edited on
I realized it after I posted. I tend to post before thinking things through.

It's late, but I was about to make this edit:

I think your problem is that you don't understand exactly how scanf works. When you use it to read a number, it will read your input until it reaches something that is not a number.

You need to know that when you type in 4, what you're actually giving the program is the input "4\n" where the '\n' is from pressing enter. The scanf will take the '4' but leave the '\n' in the stream.

When you use scanf to read two numbers in a row, this is fine because scanf will ignore preceding white spaces because those are obviously not a number (as in it will get '4' from "\n4" or " 4" or "\t4", etc.). But when you use it to read a number and then a letter, this becomes a problem.

After you take '4' from the stream, '\n' is still left. Suppose that now you type in 'a' and want the program to take 'a' as input. But after you typed in 'a', the stream is now "\na\n". So what does scanf take? It will take the first '\n' as a character and leave "a\n" in the stream.

So what happens when you want to input another number such as '1'? You type '1', and now the stream becomes "a\n1\n". It gets really complicated and probably is not what you were looking for.

There are a couple ways you can fix this:

1. Instead of having j be a char, you can have it be char[]. When reading cstrings, scanf will also take the '\n' and not leave it in the stream.

2. Declare a second char k. After every time you use scanf to read a number, use scanf to read a char to k in order to get rid of the '\n'.

3. Use fflush(stdin) after every input operation to ensure nothing remains in the input stream.
I never use C IO but this seems to work:
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
int main()
{
	int u,a,b,i;char j;
	scanf("%d",&u);

	for (i=0;i<u;i++)
	{
		scanf("\n%c %d %d",&j, &a, &b);
		printf("%c %d %d\n",j,a,b);
	}
}
http://ideone.com/x2rSl9
I've spent the last 40 minutes trying to fix this with scanf_s, and I can't figure out any way to do it. What I've learned here today is to never use scanf_s for characters and avoid scanf.
I agree. scanf doesnt always works properly.
Topic archived. No new replies allowed.