Find the answer to your Linux question:
Results 1 to 5 of 5
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 ...
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    10

    Post Help with simple program

    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.

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    This sounds to me a lot like a homework problem. Homework problems are not allowed on this forum.

    To me, the easiest solution would seem to be threading, but that may not be for you.

    I am going to lock this thread because it sounds like homework: if it's not, please PM me and I will reopen it.


    EDIT: Unlocking thread because OP verified that it is not homework.
    Last edited by Cabhan; 03-10-2010 at 12:02 PM.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Within every process are a number of what are called threads. These threads run separate instructions (that is, their logic is separate), but they share IO and can share memory as well.

    What you could do is have a thread that prints the numbers in a loop, and have a second thread wait for input. When the user enters "e", the waiting thread can kill the looping thread and exit the program.

    Actually doing this will be a bit tougher. If you're using C, you can look up the pthreads library, which is what we use to do threading in C. For Java, there are the Thread and Runnable classes to look at. Other languages have their own threading implementations.
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    One could define "e" as the interrupt character for the terminal. See man stty, man bash (for builtin trap), and experiment ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  5. #5
    Just Joined!
    Join Date
    Mar 2010
    Posts
    10
    You could try the following program:

    Code:
    #include <iostream>
    #include <pthread.h>
    #include <stdlib.h>
    
    using namespace std;
    
    /*
     * Thread method
     * Will run forever until the program exits
     */
    void *threadMethod(void *arg)
    {
    	for (int i=1; true; i++)	// Count up forever
    	{
    		cout << i << endl;	// Output the current count
    		sleep(1);		// Sleep for 1 second, remove this if you don't want a delay
    	}
    }
    
    /*
     * Main program entry point
     */
    int main(int argc, char* argv[])
    {
    	pthread_t handle;	// Handle for the thread
    	int ret = 0;		// Return code used for thread creation
    
    	cout << "Please press 'e' to exit" << endl;	// Message output before creating the thread
    
    	// Create the thread, storing the handle in 'handle', no custom attributes are used,
    	// the method 'threadMethod' will be run as the thread and no arguments are passed in
    	ret = pthread_create(&handle, NULL, threadMethod, NULL);
    	if (ret != 0 ) {
    		cerr << "There was a problem creating the thread" << endl;
    		cerr << "Return code is "+ret;
    		exit(EXIT_FAILURE);
    	} 
    	pthread_detach(handle);		// Detach the thread so resources are freed on its exit
    
    	while ('e' != getchar()){}
    
    	exit(EXIT_SUCCESS);
    }
    save it as test.cpp and compile it with g++ -lpthread test.cpp -o test

    You'll have to press e+return to exit, but then capturing unbuffered input from a terminal is a little out of scope of this....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...