OK I'm trying to have a simple loop which keeps on printing each number from 1,2,... on a different line and it keeps on doing so until the user enters 'e'. e.g for(i=0;i<=9999999999;i++) { printf("%d\n",i); } It does not take any input from user and keeps on printing but as soon as user presses e the program exits.Any clue how to go about this.
Use the following program for printing the integer value unless press E or e. This example will help for your requirement. Code: #include<stdio.h> #include<stdlib.h> main() { long i; char c; for(i=0;i<=99999;i++) { printf("%d",i); c=getchar(); if(c == 'e' || c == 'E') { break; } continue; } }
with getchar you stop the execution of the program until the user press enter. certainly not what he wants. And no this is not a simple program. There is not an easy way to do it(especially platform independent).
You need thread or process creation and some signal stuff. Create a child process which will print the numbers and the parent process will take user input.
Well this doesn't exactly help because the program does not continue printing numbers and it waits for a user input after each number is printed.
???:surprised Ok thanks for that advice.Any one else who can help but not be so discreet would be appreciated.
Alright then, if you want it spoon-fed to you, how about: Code: int c=getch(); Then see what c contains when the user presses "e", for example by using printf, and if you need an example of that as well then it might be something like Code: printf("%d\n",c); Then you can test for that specific value. And here's an example: Code: int c=getch(); if (c==CODE_FOR_E) { printf("User pressed E\n"); } (change CODE_FOR_E to the number returned by the previous bit).
I have no intention of getting flamed now ..he he .And Thanks for putting together that code yeah but I'm doing it by using threads.
you will need the thread that will moniter the char to stop the printing . Create the new thread and use getchar( ) in that thread this will continue with the printing . till you press 'e' or 'E' .
this can be solved like this Code: #include<stdio.h> #include<conio.h> int main(){ int i=0; printf("enter key a to stop running"); char a='o'; while(a!='a'){ while(!kbhit()){ printf("\n%d",++i); } a=getch(); } printf("\nyou pressed a and stopped the process!!!"); getchar(); }