gets() in a void function -- C help

trilobyte

Member
Feb 27, 2000
60
0
0
How come gets() works in my main, but if I call it in a void function (example: void change(char * string) ) it won't let me input from the keyboard?

lets say I have this

*psuedo-code btw
void change(char * string)
{
gets(string);
}

now gets(string) SHOULD give me a keyboard-input prompt right? It doesn't though. It will if I did the gets(string) in the MAIN...but in a void function..it simply assigns string to "" and exits. Can anyone tell my why and how may I go about fixing this? BTW this is for a line, not a string (so scanf won't work).

 

singh

Golden Member
Jul 5, 2001
1,449
0
0
You mean this doesn't work:


void Do(char* strInput)
{
gets(strInput);
printf("%s\n", strInput);

}


int main()
{
char strInput[100] = {0};

Do(strInput);
return 0;
}



?
 

trilobyte

Member
Feb 27, 2000
60
0
0
exactly, that doesn't work

on the printf in the do function, it'll just give me an extra line. it won't let me input any text.

*edit: ok...wtf. that little program you posted works...but in function in this program i'm writing...it doesn't...hrm...i'm going to have to really look now...
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: trilobyte
exactly, that doesn't work

on the printf in the do function, it'll just give me an extra line. it won't let me input any text.

Are you sure? It worked perfectly when I tried it.
 

trilobyte

Member
Feb 27, 2000
60
0
0
hrm, apparently it's something in my switch statement...oh well thanks anyway. gunna have to track this bug down
 

trilobyte

Member
Feb 27, 2000
60
0
0
Ah I think I got it now. I wasn't flushing the buffer properly after past keyboard inputs from other function calls ;)