Help for C Programming

This is a discussion on Help for C Programming within the C Programming forums, part of the General Programming Boards category; Hi there, I am very new to C programming and I have been trying to write a program for running ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Location
    Bristol, UK
    Posts
    2

    Question Help for C Programming

    Hi there, I am very new to C programming and I have been trying to write a program for running traffic lights, with an interrupt key press for a pedestrian crossing of 30 secs then return back to the free running process. I am going mad trying to work out where I am going wrong.. Would anyone be able to help me out at all? here's the code I've written so far...


    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>

    /*Timer set with integar of inputtime to be included after each phase of the traffic light operation*/

    timer(int inputtime)
    {
    int buttonpressed = 0;
    time_t start;
    time_t current;
    time(&start);
    do
    {
    time(&current);
    /*Interupt operation in timer*/
    //if enterkeypressed
    {
    int getchar();

    if (getchar()) goto interupt;{

    interupt:
    printf("SAFE TO CROSS\n");
    buttonpressed=timer(30.0);
    system("cls");
    exit(0);}

    }

    while(difftime(current,start) < inputtime);
    return buttonpressed;
    }


    /*free running operation*/
    freerunninglights()
    {
    int buttonpressed = 0;

    printf("Road A - RED; Road B - GREEN\n");
    buttonpressed=timer(6.0);
    system("cls");

    printf("Road A - RED/AMBER; Road B - AMBER\n");
    buttonpressed=timer(5.0);
    system("cls");

    printf("Road A - GREEN; Road B - RED\n");
    buttonpressed=timer(6.0);
    system("cls");

    printf("Road A - AMBER; Road B - RED/AMBER\n");
    buttonpressed=timer(5.0);
    system("cls");

    }

    /*main*/
    int main()
    {
    while(1)
    freerunninglights();
    return 0;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Getchar() blocks. It will wait until you press a key.

    This is the deal with a single process: it only does one thing. It would be nice it there were a getchar() with a timeout, but there isn't.

    To do this you need a threaded timer. I think that is going to be a little over your skill level, unless you can find a simple library which implements one.

    In other words: sorry friend, you probably can't do this right now. Try something else.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    READ THIS ... << !! Posting Code? Read this First !! >>

    Then go back and fix your first post, please.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    No need for threaded timer... I hope. There is kbhit(). It checks whether a key was hit but it would not stall there.
    "_kbhit returns a nonzero value if a key has been pressed. Otherwise, it returns 0."

    Then you have to run a loop to see if some time interval has been reached and simply check _kbhit() inside that loop.

    Sheesh. Nobody knows how to do simple programming any more without "threads", OOPs, and other crutches.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Quote Originally Posted by nonoob View Post
    No need for threaded timer... I hope. There is kbhit(). It checks whether a key was hit but it would not stall there.
    "_kbhit returns a nonzero value if a key has been pressed. Otherwise, it returns 0."

    Then you have to run a loop to see if some time interval has been reached and simply check _kbhit() inside that loop.

    Sheesh. Nobody knows how to do simple programming any more without "threads", OOPs, and other crutches.
    I'm pretty sure kbhit() is non-standard.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by nonoob View Post
    No need for threaded timer... I hope. There is kbhit(). It checks whether a key was hit but it would not stall there.
    "_kbhit returns a nonzero value if a key has been pressed. Otherwise, it returns 0."

    Then you have to run a loop to see if some time interval has been reached and simply check _kbhit() inside that loop.

    Sheesh. Nobody knows how to do simple programming any more without "threads", OOPs, and other crutches.
    Unfortunately, kbhit() was part of conio.h, and AFAIK, that is not any part of the C standard. IOW, it's a non-standard extension to the language. Several popular compilers don't support unbuffered input.

    Why do you think I've stuck with Turbo C for so very long?

    Pelles C, DOES support conio.h, and thus kbhit(), and I'm sure the Windows API has a way to do this, as well. You should check out Pelles C for this IMO. Free, NO C++ clutter, and an intuitive IDE to work with.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I looked it up under Microsoft Visual Studio 2005 and it's there. I don't know what you mean by "several popular compilers", but like it or not, if it's Microsoft, it's a standard.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Location
    Bristol, UK
    Posts
    2

    Unhappy

    thanks for the replies, sorry about the formatting on the post... I was told I need to use kbhit() and getch() for an interrupt, but due to me being a beginner I have no clue how to implement this into my program.

    Problem is when I put the interrupt in the middle of my timer it doesn't seem to wait for the button press and I cant get the program to go back out of the interrupt afterwards.. I think I'm out of my league here...back to basics lol

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Something like
    Code:
    time(&start);
    for (;;) {    /* forever */
        time(&current);
        if (current - start >= 5) {
            start = current;
            /* change state of traffic light */
            /* possibly resetting buttonpressed = 0 */
            }
        if (kbhit()) {    /* yes a key has been pressed */
            getch();    /* we can ignore the actual character */
            buttonpressed = 1;
            }
        }
    Last edited by nonoob; 04-05-2011 at 09:28 AM.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Quote Originally Posted by nonoob View Post
    I looked it up under Microsoft Visual Studio 2005 and it's there. I don't know what you mean by "several popular compilers", but like it or not, if it's Microsoft, it's a standard.
    By "non-standard" I meant "not part of the standard", e.g. C89/C99. Just because certain compilers include it, it doesn't make it part of the standard. Don't let your defensiveness confuse people trying to learn.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    No defensiveness intended. Just a glib political statement about some major players.
    "standard" is whatever I am using at work at the time.

    I get tired of the cranky standards junkies (not you) that always come back with "yeah but bytes can be 9 bits, and you are forgetting about EBCDIC, and what about mixed endienness, and twos-compliment is not guaranteed - on certain relay & tube machines". LOL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DIfference between , and ;
    By rocketman03 in forum C++ Programming
    Replies: 6
    Last Post: 10-21-2009, 09:46 AM
  2. Serialization problem
    By noobcpp in forum C++ Programming
    Replies: 17
    Last Post: 07-15-2008, 08:01 PM
  3. Structure pass-by-reference - help?
    By dxfoo in forum C Programming
    Replies: 28
    Last Post: 08-20-2005, 10:16 PM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 06:18 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21