Help with simple program

Discussion in 'C' started by en_7123, Mar 9, 2010.

  1. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    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. haru31773

    haru31773 New Member

    Joined:
    Mar 9, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    so it should continue printing until the user presses E?
     
  3. rekha_sri

    rekha_sri New Member

    Joined:
    Feb 20, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    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;
    }
    }
    
    
    
     
  4. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    Yes
     
  5. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28


    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).
     
  6. lipun4u

    lipun4u New Member

    Joined:
    Jan 25, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Mumbai
    Home Page:
    http://kodeyard.blogspot.com/
    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.
     
  7. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  8. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    Yup exactly guys could any one help me with this.How to go about it.
     
  9. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    in which operation system?
     
  10. thapchi

    thapchi New Member

    Joined:
    Mar 9, 2010
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Well the main thing u r missing is

    Scanf

    its user input?

    where will user enter?
     
  11. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    I'm using linux fedora
     
  12. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    If you put scanf it would wait for user input .Read the problem again.
     
  13. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Use getch() from the ncurses library.
     
  14. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    I'm using C.Could you please explain or give me a link.Thanks
     
  15. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Try Google.
     
  16. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    ???:surprised Ok thanks for that advice.Any one else who can help but not be so discreet would be appreciated.
     
  17. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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).
     
  18. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    I have no intention of getting flamed now ..he he :D .And Thanks for putting together that code yeah but I'm doing it by using threads.
     
  19. bluecoder

    bluecoder New Member

    Joined:
    Mar 11, 2010
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    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' .
     
  20. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    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();
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice