Thread: problem : input and calculation working together

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    9

    problem : input and calculation working together

    i want to make some time counter that announce (call printf function) when the remaining time for the clock will get below 0
    now the problem is, that im running also a menu with it so i need recieve input from the user to press some letter to activate some menu function.
    and the getch() functon is stopping the program, so .. until the user won't press some letter , the remaining time in the counter won't update.
    is there an input function that checks if there is an input for .. let's say .. 0.01 seconds and then resume the program?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    28
    Consider taking a look at curses, or ncurses, which is available for almost all UNIX-like OSs, and has a non-blocking getch() call:
    Code:
    #include <ncurses.h>                    /* ncurses.h includes stdio.h */  
    #include <string.h> 
    #include <unistd.h> 
    
    int main()
    {
      char c;
      WINDOW * w = initscr();
      nodelay(w, 1);
      while((c = getch()) == ERR)
        sleep(1);
      mvprintw(0, 0, "You entered %c", c);
      refresh();
      sleep(5);
      endwin();  
      return 0;
    }
    Since you're using curses, compile with libcurses:
    Code:
    gcc curses.c -lcurses -o curses
    Last edited by pdc; 07-28-2005 at 08:44 AM. Reason: [typo in formatting]

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    9
    *cough* *cough* did i say that im compiling on UNIX????? im just regular guy with regular computer that compiles on Microsoft visual C++ or DEV C++

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well I guess you should have said that in the first place, Einstein. Not all of use use Windows all of the time, if it all.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    9
    Quote Originally Posted by quzah
    Well I guess you should have said that in the first place, Einstein. Not all of use use Windows all of the time, if it all.


    Quzah.
    ??? i didn't understand you. you just said "not all of use use windows.." maybe it's .. "not all of use windows..".. but it's still wrong because.. there is incorrect syntax between "all of use" , there should be noun after "all of".. like "all of the guys, all of them, all of x".. so let's say..
    "not all of the devs use windows all of the time, if it all"
    another syntax mistake . "if it all" ? what all? what it? lol
    this part i couldn't understand .. so .. let me guess what you meant to say.. like "even not all of them." lol this part you mistaken really bad.. i had to replace the 'if' because it was incorrect and the 'all' and add 4 words lol.
    next time don't write random words and expect someone to understand it
    so here is the correct one : "Not all of the dev's use Windows all of the time, even none of them."
    Last edited by itay390; 07-28-2005 at 03:28 PM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    "not all of use use windows" -> "not all of us use windows"

    Perhaps ncurses is available for Windows, too.

    The kbhit() function (perhaps in <conio.h>, available in Dev-C++), returns 1 if a keypress has been detected.

    Code:
    if(kbhit()) {
        c = getch();
    }
    kbhit() is very fast, and you can use it in loops.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    28
    If you're willing to use Cygwin it is, at least:

    http://mir.zyrianes.net/cygwin/release/ncurses/

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    9
    thank you very much
    i tested it on this code and it works

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
    char c;
    long a = 0;
    while (a != -1)
          {
          if(kbhit())
                     {
                     c = getchar();
                     }
          printf("%d\n", a);
          ++a;
          }
          
    return 0;
    }

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    28
    In general, C style is not to indent braces. I.e., the above should be:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
    char c;
    long a = 0;
    while (a != -1)
    {
          if(kbhit())
          {
                     c = getchar();
          }
          printf("%d\n", a);
          ++a;
    }
          
    return 0;
    }
    (I don't want to start a coding style flamewar, though, honest )

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void) {
        char c;
        long a = 0;
        while (a != -1) {
            if(kbhit()) {
                c = getchar();
            }
            printf("%d\n", a);
            ++a;
        }
          
        return 0;
    }
    Also try using getche() (in Dev-C++) . . . unbuffered input.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    9
    pdc i know it it should be in your way.
    but to me, my way looks more easier to view and understand

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    28
    By all means, code in whatever style suits you best. Just remember that current styles have evolved because they generally prove to be most readable in the long-run, and that you're probably not coding in a vacuum: others may have to read your work.

    But each to his own.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The Jargon File's entry on indent style lists itay390's style as a traditional indent style for C, so it may have a larger following than you think (though I personally find it rare).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    28
    I'm aware of it, yeah, but it (thankfully!) seems pretty dead these days.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Price Calculation Problem using Class
    By Bernard4 in forum C++ Programming
    Replies: 4
    Last Post: 07-23-2008, 12:12 PM
  2. Question About My Homework Pls Help Me İmmediately
    By jennyyyy in forum C Programming
    Replies: 27
    Last Post: 03-13-2008, 11:40 AM
  3. ACM Problem Confusion
    By drag0n69 in forum C Programming
    Replies: 6
    Last Post: 02-26-2008, 07:46 PM
  4. Problem grabbing k/b input
    By falcon9 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 11:47 AM
  5. Help with calculation and loop
    By Betty in forum C Programming
    Replies: 7
    Last Post: 04-10-2006, 05:37 AM