Click to See Complete Forum and Search --> : C - Online Books


aeav
12-13-2003, 12:58 AM
Hello,

How I do to construct one program in C, when the user press any key the program continuous the execution?


Remember: I stoped the program with the function pause() and now I want to restart it when press any key.


Thanks... :D

Xenokamikazi
12-15-2003, 10:37 AM
I'd just use the getch() function.


#include <conio.h>

void whatever()
{
char wait;

printf("Program Paused. Press any key to continue.\n");
wait = getch();
return;
}

bwkaz
12-15-2003, 07:53 PM
<conio.h> and getch() are only available on DOS machines though (or OSes built on top of DOS like Win9x, or OSes doing reasonable DOS emulation like NT).

For Linux, ncurses (<ncurses.h> and link with -lncurses) has an equivalent capability, but that's a lot of overhead for something that small. You might be able to find an example of putting the console into some sort of unbuffered mode on the Web somewhere, too, which would be less portable but would be easier.

aeav
12-15-2003, 10:22 PM
Using the comand getch(); showed an error, but using the getchar() I got run the program...

thanks!

bwkaz
12-16-2003, 08:09 PM
But getchar() won't work unless the key you pressed is the enter key. Linux's terminals all (at least as far as I know) do line-buffering, which means you program doesn't see any input until enter is pressed.

That's why you have to put the terminal in unbuffered mode for it to work (or use ncurses, which does that for you).

Does your function work if you hit the spacebar (or the A key)?

aeav
12-17-2003, 10:18 PM
When I press spacebar, anything happen but pressing the Enter the script go to next line...

aeav
12-17-2003, 10:27 PM
The soucer's it:


#include <stdio.h>

main(){
printf("test");
getchar();
printf("test");
}


When I try to use this code>

#include <stdio.h>

main(){
printf("test");
getch();
printf("test");
}

The compiler print this error>

pause.c:7:2: warning: no newline at end of file
/tmp/ccMuC9Ym.o(.text+0x21): In function `main':
: undefined reference to `getch'
collect2: ld returned 1 exit status


What's happened?

bwkaz
12-17-2003, 11:17 PM
Like I said above, getch() doesn't exist on Linux (unless you link in the ncurses library, but that's way too much overhead). That's what the linker error means.

But what I was asking about was this. When I compile and run a slightly cleaner version of your first snippet -- that is, this:

#include <stdio.h>

int main()
{
printf("test\n");
getchar();
printf("test\n");

return 0;
}, I get this:

$ ./testprog
test
<waits here>
test Where I put <waits here>, the program sits there and does nothing until I hit the return key. If I hit the spacebar, it does not print the next "test" string until I also hit the return key.

If you want something to happen when the user hits the spacebar too (actually any key other than return acts just like the spacebar does), then you have to do something special with the terminal. What that is depends on the terminal type.

aeav
12-18-2003, 12:20 AM
I understood what you say...

But how I'm newbie in C...

I have one ask:

What the return 0; make in the program?


thank you...

bwkaz
12-18-2003, 07:57 PM
It means that after the program runs, echo $? from bash will print a 0.

The return value from your main function is just some value that your parent process can access. By convention, 0 means your program succeeded, and anything other than 0 means that some part of your program failed.

exit(0); does the same thing, which is a bit confusing since exit is a function call, and return is a return... but whatever.