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 MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Gets command problem

Status
Not open for further replies.

misbird

MIS
Jun 20, 2003
4
US
I have gets command in my code but when it should prompt me it skips right past it and it appears to insert a number string that looks to be related to the number of character designated in the char declaration. Her is a copy of the function. Is thare any other command that can be used to prompt for a keyboard input as a continious string including spaces?

int retrieve (AVL_TREE *tree)
{
/* Local Declarations */
char crap[27];
void *addr;

/* Statements */
puts("Please enter a Name: ");
gets( crap );
addr = AVL_Retrieve (tree, crap);
printf("%d",crap);
if (addr)
printf("\nKey found. Contains %d\n", addr);
else
printf("\n\aKey %d not found\n", crap);

return(0);
} /* retrieve */
 
Well gets() just works, it always has. Something else must be responsible for the behavior you are seeing. Perhaps your application already has data in the input buffer before you call gets()? Did you try emptying the input buffer before calling gets()?

Did you try to do something like below to verify how gets works?
Code:
void main(){
	char buf[27];
	puts("Enter something...");
	gets( buf);
	cout << &quot;You gave: &quot; << buf << endl;
}

-pete
 
Thanks for the suggestion. I tried that but still it keeps passing gets right on by. If I can determine what is the cause I will post my resolve.
 
>> I tried that but still it keeps passing gets right on by.

tried what? emptying the buffer? If so post the code you used, that may be the problem.

-pete
 
I have figured out that the problem is some how caused by my menu system. I tested by calling the function before the menu function in main and when I did this it did prompt me. For some reason with the menu function running it passes some parameter over to the variable assigne as the variable for my gets. Here is a copy of the main, the menu function, and the function that has gets. I am still stumpt.

int main ( void )
{
/* Local Declarations */
char option;

tree = AVL_Create (compare);

/* Statements */
//printf( &quot;Start AVL Tree\n&quot; );

if (!tree)
printf(&quot;\aERROR: Cannot create tree\a\n&quot;), exit (100);

avl_load ();
//insert ();
/* ======= Selected Options ====*/
while ((option = Menu () ) != 'q' )
{
switch (option)
{
case 'p': AVL_Print (tree);
break;
case 'i': insert ();
break;
case 's': retrieve(tree);
break;
case 'd': delete (tree);
break;
default : printf(&quot;\a\a\nInvalid option. Please try again.\n&quot;);
} /* switch */
}




/*(void) writefile ();*/


printf( &quot;End Program\n&quot; );
return 0;

} /* main */

char Menu ( void )
{
/* Local Declarations */
char option;
//int valid;

/* Statements */
printf(&quot;=============== MENU ================ \n&quot;);
printf(&quot;Here are your choices:\n\n&quot;);
printf(&quot; S: Search for a Name in the list \n&quot;);
printf(&quot; I: Insert a New Name and Number \n&quot;);
printf(&quot; D: Delete a Name from the list \n&quot;);
printf(&quot; P: Print the entire list \n&quot;);
printf(&quot; \n&quot;);
printf(&quot; Q: Quit \n\n&quot;);
printf(&quot;Enter your choice: &quot;);

scanf(&quot; %c&quot;, &option);
option = tolower (option);

return option;
} /* End Menu */

int insert ()
{
//cout << &quot;MyMap is empty&quot; << endl;
char newPtr[27];
int success;
//FILE *fpData;
//char c='\n';


/* Statements */
printf(&quot;\nEnter a Name and Number (First Last xxx-xxx-xxxx) Below:\n &quot;);
printf(&quot;:&quot;);
//cout << &quot;MyMap is empty&quot; << endl;
gets(newPtr);
printf( &quot;The line entered was: %d\n&quot;, newPtr );
//cout << &quot;MyMap is empty&quot; << endl;
//scanf(&quot;%d&quot;,newPtr);


success = AVL_Insert (tree, newPtr);

if (success)
printf(&quot;%3d inserted\n&quot;, newPtr);
else
printf(&quot;\aERROR inserting %d\a\n&quot;, newPtr);

return(0);
} /* insert */
 
Got it working. I found the fflush command and entered it prior to the gets.

/* Statements */
fflush(stdin);
printf(&quot;\nEnter a Name and Number Below:\n &quot;);
printf(&quot;:&quot;);
gets(newPtr);
printf( &quot;The line entered was: %s\n&quot;, newPtr );

success = AVL_Insert (tree, newPtr);
 
Note the behavior of fflush on input streams can vary from compiler to compiler, though Microsoft does document fflush to discard characters from the input stream buffer. A portable solution:

int c;

while ((c = getchar()) != '\n' && c != EOF)
continue;

Also, you should never use gets, because there's no way to use it safely. gets doesn't limit the number of characters that the user can enter. A malicious user can crash your program and even gain privileged access to the system the program runs on by exploiting gets.

Use fgets instead:

char buf[1024]; /* or whatever size */
char *tmp;

fgets(buf, sizeof buf, stdin);
tmp = strchr(buf, '\n');
if (tmp != NULL) *tmp = '\0';

Since you're programming in C++, why not use cin.getline?.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top