• Advertisement
Sign in to follow this  

Strange scanf problem in C - Please help

This topic is 4275 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a possibly simple problem here. I am running a loop that asks a user for input, displays something then goes back to the start for more input. The problem is, after the user enters a letter and hits enter, it displays the appropraite data but then it seems to do another loop without stopping for the scanf. I think you would have to run my code to see what i mean.
#include <stdio.h>

int main()
{
	char letter;
	int ordinal = 0;

	while(1)
	{
		printf("Please enter a letter: ");
		scanf("%c", &letter);

		ordinal = (int)letter;

		if(ordinal == 35)
		{
			printf("You have quit");
			break;
		}

		if(ordinal < 65 || ordinal > 122)
		{
			printf("Letters only please\n\n");
			continue;
		}

		if(ordinal > 90 && ordinal < 97)
		{
			printf("Letters only please\n\n");
			continue;
		}

		printf("%c = %d\n", letter, ordinal);

		if(ordinal > 96)
			ordinal = ordinal - 32;
		else if(ordinal < 91)
			ordinal = ordinal + 32;

		printf("%c = %d\n\n", (char)ordinal, ordinal);

	}
}
If anyone could tell me why this extra loop is happening that would be great. Thanks :)

Share this post


Link to post
Share on other sites
Advertisement
Not very strange when you think about what you are doing to scanf. scanf maintains a buffer behind the scenes which the user input goes into. When you type your character nothing happens to the program execution until you then press enter.

The console IO picks up all keypresses as input so both your first character and the new line operation (that arises due to pressing enter) go into the scanf buffer. The first iteration of the loop causes scanf to look at its buffer, which is empty and therefore requests to the user to put something in it. Afterwards it gets the first entry in the buffer, therefore pulls out the type character correctly. The second iteration causes scanf to first look at its buffer and it finds there is already a character there (the new line) and therefore it does not need to request any input from the user, it can just pull out that new line character and use that for the current iteration.

It is only on the 3rd iteration of the loop that the scanf buffer is empty and requires user input to continue once more.

Share this post


Link to post
Share on other sites
You might also like to look at the function:

int getch( void );

which requires conio.h.

This function would do just what you want, read a character and then return immediately rather than waiting for enter to be pressed. However, this is not ANSI C compatible (technically not standard and more platform specific) however if you only ever use windows then it should not matter.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement