Thread: How to approach this c program

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    5

    How to approach this c program

    i am writing a text based program for a car indicators. to beable switch the left indiactor on the right on and emergency on. e.g if the left is on and i turn the steering wheel left and turn it back to (right) it should automatically switch of jus like a normail car would.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And what part are you struggling with?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should probably start with thinking through the logic -- if A happens, what do I do next? How do I know whether the left blinker is on?

    Then you may need to decide how to get input from your user.

    Once you have both of those, the path forward should be clear(er).

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    5
    yea i kinda kw where to go but im nt sure i was gonna use char input or a string input. the problem is the automaticlly switch off when it returns from a turning.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    char = one letter. string = more than one letter.

    And the other bit is part of the logic -- the auto switch off needs to be built in from the start.

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    5
    dnt understant use fsd to explain

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by jaykay View Post
    dnt understant use fsd to explain
    What?

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Quote Originally Posted by tabstop View Post
    What?
    http://en.wikipedia.org/wiki/FSD
    Maybe second one?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rasta_freak View Post
    Oh. I thought he had just left vowels out like he'd done with everything else.

    Assuming the first link is what's meant , then "the blinker should turn off after the car finishes turning in that direction" is the fsd. It's your job to turn that into logic.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by tabstop View Post
    Oh. I thought he had just left vowels out like he'd done with everything else.
    And this here is why we need to insist people not use text-message-speak when requesting help on the forum. You're not chatting up your buddy on what's going on this evening, you're requesting assistance to a problem that requires clarity of communication.

  11. #11
    Registered User
    Join Date
    Aug 2008
    Posts
    5

    error

    can anyone help with this error cant find it


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    enum steerval {INVALID_SWITCH,
                     L_SL_RCVD,
                     R_SR_RCVD,
                     L_SR_RCVD,
                     R_SL_RCVD};
    
    				 
    enum stateval {INVALID_STATE,
                     OFF,
                     L_WINK,
                     R_WINK,
                     LEFT_TURN,
                     RIGHT_TURN};
    
    
    				 
    
    				 
    enum eventval {INVALID_EVENT,
                     TURN_LEFT,
                     TURN_RIGHT,
                     TURN_CANCEL,
                     SL_CLICKED,
                     SR_CLICKED};
    
    
    enum indval {INVALID_SIG,
                   IND_OFF,
                   L_ON,
                   R_ON};
    
    			   
    struct car
    {
      enum stateval state;
      enum indval indicator;
      enum steerval sr_sl;
      enum eventval event;
    };
    
    
    
    void OnEventLeftIndON (struct car *myCar)
    {
      if (myCar->state == OFF)
      {
      
        myCar->state = L_WINK;
        myCar->indicator = L_ON;
        myCar->event = TURN_LEFT;
        printf("\n Left Indicator is Turned ON\n");
      }
      else if (myCar->indicator == L_ON)
      {
      
        printf("\n Left Indicator is already ON\n");
      }
      else if (myCar->indicator == R_ON)
      {
      
        printf("\n Right Indicator is already ON.Can't Turn ON Left Indicator\n");
      }
      else if (myCar->state == LEFT_TURN)
      {
        /* Turn ON Left Indicator in half turn */
        myCar->indicator = L_ON;
        printf("\n Left Indicator is Turned ON\n");
      }
      else
      {
        /* Not a valid Request */
        printf("\n Invalid Request\n");
      }
      return;
    };
    
    /* Event Handler for Right indicator ON */
    void OnEventRightIndON (struct car *myCar)
    {
      if (myCar->state == OFF)
      {
        /* Turn ON Right indicator and change state */
        myCar->state = R_WINK;
        myCar->indicator = R_ON;
        myCar->event = TURN_RIGHT;
        printf("\n Right Indicator is Turned ON\n");
      }
      else if (myCar->indicator == R_ON)
      {
        /* Right indicator is already ON  */
        printf("\n Right Indicator is already ON\n");
      }
      else if (myCar->indicator == L_ON)
      {
        /* Left indicator is already ON  */
        printf("\n Left Indicator is already ON.Can't Turn ON Right Indicator\n");
      }
      else if (myCar->state == RIGHT_TURN)
      {
        /* Turn ON Right Indicator in half turn */
        myCar->indicator = R_ON;
        printf("\n Right Indicator is Turned ON\n");
      }
      else
      {
        /* Not a valid request */
        printf("\n Invalid Request\n");
      }
      return;
    };
    
    /* Event Handler for indicator OFF */
    void OnEventCancelInd (struct car *myCar)
    {
      if (myCar->indicator == IND_OFF)
      {
        printf("\n Indicator already turned OFF\n");
        return;
      }
      if ((myCar->state == L_WINK) || (myCar->state == R_WINK))
      {
        myCar->indicator = IND_OFF;
        myCar->state = OFF;
        printf("\n Indicators Turned OFF\n");
        return;
      }
      /* Turn OFF the indicator */
      myCar->indicator = IND_OFF;
      printf("\n Indicators Turned OFF\n");
      return;
    };
    
    /* Event Handler for SL_CLICKED(Left Turn of steering wheel) */
    void OnEventSLClicked (struct car *myCar)
    {
      switch (myCar->state)
      {
        case OFF:
        case L_WINK:
                     /* Left Turn when Left Indicator is ON/OFF */
                     myCar->state = LEFT_TURN;
                     myCar->sr_sl = L_SL_RCVD;
                     printf("\n Taking a Left Turn\n");
                     break;
        case LEFT_TURN:
                        /* No change as it is a small swerve */
                        printf("\n Taking a small left swerve\n");
                        break;
        case RIGHT_TURN:
                         /* Turn Complete. Make the steering steady and auto turn-OFF indicator */
                         myCar->state = OFF;
                         myCar->indicator = IND_OFF;
                         printf("\n Turn Complete. Steering Steady. Indicator OFF\n");
                         break;
        default:
                 /* Not a valid request */
                 printf("\n Invalid Request\n");
                 break;
      }
    };
        
                   
    /* Event Handler for SR_CLICKED(Right Turn of steering wheel) */
    void OnEventSRClicked (struct car *myCar)
    {
      switch (myCar->state)
      {
        case OFF:
        case R_WINK:
                     /* Right Turn when Right Indicator is ON/OFF */
                     myCar->state = RIGHT_TURN;
                     myCar->sr_sl = R_SR_RCVD;
                     printf("\n Taking a Right Turn\n");
                     break;
        case RIGHT_TURN:
                        /* No change as it is a small swerve */
                        printf("\n Taking a small right swerve\n");
                        break;
        case LEFT_TURN:
                       /* Turn Complete. Make the steering steady and auto turn-OFF indicator */
                       myCar->state = OFF;
                       myCar->indicator = IND_OFF;
                       printf("\n Turn Complete. Steering Steady. Indicator OFF\n");
                       break;
        default:
                 /* Not a valid request */
                 printf("\n Invalid Request\n");
                 break;
      }
    };
    
    
    int initializeSystem (struct car *myCar)
    {
      myCar->state = OFF;
      myCar->indicator = IND_OFF;
      myCar->sr_sl = INVALID_SWITCH;
      myCar->event = INVALID_EVENT;
      return 0;
    };
    
    
    int usageDisplay (void)
    {
    printf("\n Press \'C\' to Turn-OFF indicators\n");
    printf("\n Press \'l\' to Turn-ON Left indicator\n");
    printf("\n Press \'r\' to Turn-ON Right indicator\n");
    printf("\n Press \'L\' to Turn Left\n");
    printf("\n Press \'R\' to Turn Right\n");
    printf("\n Press \'X\' to Quit\n");
    return 0;
    };
    
    int main (void)
    {
      struct car *myCar;
      char choice;
      int dummy;
    
      /* Memory alloc for myCar */
      myCar = (struct car *)malloc (sizeof(struct car));
      if (myCar == NULL)
      {
        /* malloc failed. Return failure */
        printf("\n Invalid memory\n");
        return -1;
      }
    
      /* Initialize Car Control System */
      int initializeSystem (myCar);
    
      while(1) 
      {
        /* Display help context */
    	int usageDisplay (void);
    
        /* Receive inputs from user through keyboard */
        choice = getchar();
        dummy = getchar();
    
        /* Validate and act upon user input */
        switch(choice)
        {
    	         
          case 'l':
                    OnEventLeftIndON(myCar);
                    break;
          case 'r':
                    OnEventRightIndON(myCar);
                    break;
          case 'L':
                    OnEventSLClicked(myCar);
                    break;
          case 'R':
                    OnEventSRClicked(myCar);
                    break;
          case 'X':
                    free(myCar);
                    return 0;
          case 'C':
                    OnEventCancelInd(myCar);
                    break; 
          default:
    	  
                   printf("\n\n Error\n");
        }
        printf("\n\t\t\t\tCURRENT STATE\n");
        switch(myCar->state)
        {
          case OFF:
                    printf("\nSteering wheel steady. Indicator OFF\n");
                    break;
          case L_WINK:
                       printf("\nSteering wheel steady. Left Indicator ON\n");
                       break;
          case R_WINK:
                       printf("\nSteering wheel steady. Right Indicator ON\n");
                       break;
          case LEFT_TURN:
                       printf("\nTaking Left Turn\n");
                       break;
          case RIGHT_TURN:
                          printf("\nTaking Right Turn\n");
                          break;
        }
      printf("\n\n");
     }
      free(myCar);
      return 0;
    }
    
    
    --warning C4013: 'initializeSystem' undefined; assuming extern returning int
    
    ---d:\my documents2\uwe\csa\ind_new\indicator\ind.c(175) : warning C4013: 'usageDisplay' undefined; assuming extern returning int
    
    ---d:\my documents2\uwe\csa\ind_new\indicator\ind.c(234) : error C2371: 'initializeSystem' : redefinition; different basic types
    
    ---d:\my documents2\uwe\csa\ind_new\indicator\ind.c(243) : error C2371: 'usageDisplay' : redefinition; different basic types
    Error executing cl.exe.]
    
    ind.obj - 2 error(s), 2 warning(s)

  12. #12
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Code:
    int initializeSystem (myCar);
    ---===>>>

    Code:
    initializeSystem (myCar);
    Same for other one.

  13. #13
    Registered User
    Join Date
    Aug 2008
    Posts
    5
    already done dat but i come up with an error saying

    error C2143: syntax error : missing ';' before 'type'

  14. #14
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Ok, tried to compile it and it's OK. Nice game (kind of adventure type), by-the-way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. Need Delimiter Program helpful hints.
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 02-16-2002, 06:27 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM