Hello I am trying to make a function that requires the user to press the enter key to continue.
The function that I want to work looks like this:
Code:
void ent (void)
{
printf("Press Enter: ");
while( getchar() != '\n' );
}
I have been fiddling around with different versions of this particular code, but every time I call this function it runs the printf statement but never lets me enter any key, let alone the enter key. I think I am not understanding something about getchar().
I have seen a similar example doing an internet search and their code is in the same vein and did exactly what I want, but it just won't work for me. Any ideas?
EDIT:
Here is the code that I found on my google search that essentially does what I want:
Code:
#include <stdio.h>
int main()
{
int i;
for (i = 0; i < 3; i++) {
printf("i = %d\n", i);
printf("Press 'Enter' to continue: ... ");
while ( getchar() != '\n')
;
}
printf("\n\n");
printf("Ttttthat's all, folks\n");
printf("Press 'Enter' to exit the program ( ironic, isn't it?) ...");
while (getchar() != '\n')
;
printf("\n\n");
printf("Ttttthat's really all!\n");
return 0;
}
http://www.gidforums.com/t-12871.html
This code works as a standalone program, but when I try to use the same technique in my code, the while loop appears to be skipped.