basic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • themadjester
    New Member
    • Mar 2008
    • 37

    basic

    so what i am trying to do is write a program for a robotics project (think remote control using keyboard as input). To start I am writing this thing as a C++ console app.

    Now I have taken a coarse on OOP so I have a decent knowledge of the syntax however my understanding of structure is very lacking (I would personnally blame the coarse for being a memorization session but blame wont get my anywere). while I could do this in C I mostly want to do this in C++ for education purposes.

    Now, as far as the program goes I could and have in the past done this same thing very easily in C quite easily by simply have the program:
    1) Collect user input
    2) Perform the appropriate action,
    3) waste some time with nested for loops or transmit signal, receive confirmation signal, stop transmitting.
    4) and then check for user inputs again

    with all of this in a while(1) things are somewhat easy.
    however it would have inherent flaw that the program is tied up while waiting! (could this be solved via use of interrupts perhaps?)

    would this be written as a lengthly driver with one class and one object, were the driver simply calls a bunch of functions:
    right now it occured to me that i should make input the object and give it characteristics (like isActive, currentState, timeCollected etc)

    is my interpretation that the input should be the object and there should be one class correct?

    much thanks in advance
  • themadjester
    New Member
    • Mar 2008
    • 37

    #2
    My honest apologies regarding the title, that was a little bit of an "oops." and now I can't seem to find an edit button so I assume posts are uneditable.

    Edit (oh the irony): Initial posts are uneditable >.>

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Read this: http://bytes.com/topic/c/insights/66...patterns-state

      Comment

      • themadjester
        New Member
        • Mar 2008
        • 37

        #4
        I hadn't even known one could change the focus of the switch in this way. Pretty neat, maybe the state machine stuff they taught me in school is in some way useful ;)

        question though:
        I was going through the c implementation you added (to make sure it made sense to me before I got to OO)
        I was confused by "state = 0" since there is no case 0: or default:
        Also I had to remove break; since it was breaking from the entire switch and not checking the value of state.

        That been said the concept is the most important thing!

        Also if anyone reading this knows a good site (or perhaps fabulous book if(bookAwesomen ess > siteAwesomeness ) that doesn't bother itself with syntax and just goes over oop and how it should be implemented structure wise that would be invaluable!

        Thanks!

        Comment

        • themadjester
          New Member
          • Mar 2008
          • 37

          #5
          so I have been going over this some more because I am very much beginning to see how advantageous this [using a state machine] can be, especially if I can have the switch move between output states and read in states quickly, updating the list of outputs making my program better handle simultaneous inputs.

          That being said I am having a problem. I sort of got to it above and thought it was a simple case of to break or not to break...

          for:
          Code:
          switch(state){
          ...
          default:
          break;
          }
          if I use break in my 1st case, it exits the switch entirely
          if I don't use break, the switch does not check the value of state when it decides what to do next it just moves down my list until it gets to default:

          what am I doing wrong? I looked via google, some people are doing this way and don't seem to have my problem, some are using GOTO... which I understand is a baaaaad idea and some are even putting it in a while loop (which actually makes some sense to me, but I want to understand why this isn't working without the while for me!)

          Thanks!

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Would something like the following solve your problem?

            Code:
            for (int state= STARTSTATE; state != ENDSTATE; ) {
               switch (state) { // yout state machine
                  // don't fall through the cases but switch the state value instead
               }
            }
            kind regards,

            Jos

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Yes, heed JosAH. The state machine requires that each state know the successor state. That said you must return to the switch (using C) using a loop.

              Personally, I would use while(1). That is, an infinite loop. The switch would have a case for the exit state and there I would return from the function.

              You enter the loop with the initial state already set.

              But what JosAH says works as well up until processing is involved with either the initial or exit state. Then you need actual cases for these states.

              Comment

              • themadjester
                New Member
                • Mar 2008
                • 37

                #8
                great to know ;) I just saw some people had written state machines without so I didnt want to learn to apply an unnecessary step.

                Thanks everyone for the help, and any tips on learning optimal structure for OOP are of coarse appreciated.

                Cheers

                Comment

                Working...