Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

scanf() and gets() problem 1

Status
Not open for further replies.

Walid

Programmer
May 18, 2000
382
US
I don’t know if it is one of these days or what, but I can’t see what is going wrong here!

I was demonstrating the limitation of scanf() as it uses any white space to signal the end of the string being entered and as a result there is no way to enter multiword string using scanf(). in other words, you can’t enter “Hello, World!” using scanf() but you can using gets().

The following code, logically at least was supposed to run just fine. However, the sample run is weird. It doesn’t wait for me to enter string when it reaches gets(), instead it continues and print the second part of the first string I entered (if any) in the scanf() part of the program!!!

Now, if I comment out the first part of the program, the scanf() part, gets() works just fine. Any advice?

Code:
#include <stdio.h>


int main()
{
	char Buffer[32];
  char a[20];
	printf("Please, Enter your name:\n");
	scanf("%s",Buffer);
	printf("Hello, %s\n",Buffer);


	// Now, try it with gets()
	
	printf("Please, Enter your name:\n");
	gets(a);
	puts("Hello, ");
	puts(a);
}


Walid Magd (MCP)

The primary challenge of every software development team is to engineer the illusion of simplicity in the face of essential complexity.
-Grady Booch
 
The [tt]scanf()[/tt] is ending when it gets your carriage return, but the return isn't being pulled from STDIN's buffer by the [tt]scanf()[/tt]. The [tt]gets()[/tt] is getting the return that's still in the input buffer.

You can see this by entering something like "one two three". The "one" is picked up by the [tt]scanf()[/tt] and the " two three" is picked up by the [tt]gets()[/tt].

You need to flush the input buffer before the [tt]gets()[/tt].
 
I didn't use C (pure C, I mean) for very long time; .NET spoiled the heck out of me and out of so many programmers for that matter. Are there any replacements for those functions without using C++ standard library? Also, you mentioned something about flushing the keyboard buffer, is there any function to do that?
Thanks in advance.


Walid Magd (MCP)

The primary challenge of every software development team is to engineer the illusion of simplicity in the face of essential complexity.
-Grady Booch
 
Also, you mentioned something about flushing the keyboard buffer, is there any function to do that?
NOOOOOOoooo!!! [The evil seed begins to grow.]

Use a buffer that ought to be "big enough". Use [tt]fgets[/tt] to read input as a string. Examine the string to see if a newline is at the end. If so, there may be excess data. If not, possibly trim the newline. Or do other conversions, if necessary, using [tt][blue]s[/blue]scanf[/tt].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top