Thread: help on main()

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    39

    help on main()

    Hi,
    I have some problem with code the main(). The program is for communicating with a micro controller through the SCI port.

    the main() is supposed to do:
    Main Program.
    1. Set up The message is: “ The main function ”

    2. Write the message to the screen.

    3. Wait approximately 1 second. with wait_sec() function

    4. Check to see if there is an input character waiting to be read. If not, loop back to b. above.

    5. If there is an input character waiting, read it in, echoing it, and then output a CR and LF (using CRLF()).
    • If the character is “N” or “n”, call MsgIn() to read in a new message that immediately replaces the previous message being printed once per second.
    • If the character is “X” or “x”, exit the program.
    • If the character is something else, just ignore it.
    6. Loop back to 2. above.


    Note that MsgIn(char inData[], unsigned int length) that reads in messages received at the SCI input. inData is the address of an input array prepared to receive input data, and the length of the array, and one output argument, the actual received message length.

    Code:
    void main(void) {
      /* put your own code here */
      const data[]="The main function";
      char input;
     
      InitSci(5); /* initialize SCI post*/
      
       message_out(data); /*display message*/
      wait_sec();/*1 sec wait*/
      
       while (1){ /*loop back until the character is ready to be read*/ 
            message_out(data);
         
        if(IntStatusSCI()){/*IntStatusSCI() returns true if a character available to be read;        returns false if not. */
         break;}/*break if the character is read*/
        }
        
        input=GetSci();/*input character into SCI port*/
     
        CRLF();/*write out a CR and LF*/
       
       if(( input=='N')| (input=='n'))
           MsgIn();  /*read the message*/
       else if( (input=='X')| (input=='n'))
           exit;
       else{/*ignore it  code*/}
    
    }/*end main*/
    I have problem understanding the main does especially when it reads the new message and loops back.

    My second issue is the function MsgIn(). How do we know the length of the new message?

    Can someone help me to fix the main function please?
    thankx
    B

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >My second issue is the function MsgIn(). How do we know the length of the new message?
    And this is answered above:
    Quote Originally Posted by braddy View Post
    Note that MsgIn(char inData[], unsigned int length) that reads in messages received at the SCI input. inData is the address of an input array prepared to receive input data, and the length of the array, and one output argument, the actual received message length.
    So:
    Code:
    length = MsgIn(inData, sizeof(inData))

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Also note the difference between | and ||.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    | is the bitwise OR: it OR's the bits of its operands together. Probably not very useful to you.
    || is the logical OR: it is true if either, or both, of it operands are true. Probably what you want.

    Also, tolower()/toupper() makes your job easier. These are all equivalent.
    Code:
    if(( input=='N') || (input=='n'))
    Code:
    if(input=='N' || input=='n')
    Code:
    #include <ctype.h>  /* at the beginning of the program */
    if(tolower(input) == 'n')
    [edit] Oh, and don't use void main(). cpwiki.sf.net/void_main [/edit]
    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.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    39
    Thank you dwks and swoopy,

    Beside what you mention, does my main follows the steps 1-7?
    I am not very sure it does. I need a feedback on it please?

    thank you

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Why don't you compile and run your code and then use steps 1-7 above to test it?

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    39
    [QUOTE=QuantumPete;731664]Why don't you compile and run your code and then use steps 1-7 above to test it?

    the Lab is closed now and I dont have code warrior on my machine. when I try to dowload it, it crashes.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by braddy View Post
    Beside what you mention, does my main follows the steps 1-7?
    I am not very sure it does. I need a feedback on it please?
    It's hard to say because of this:
    >4. Check to see if there is an input character waiting to be read. If not, loop back to b. above.
    And there is no b to loop back to. Also in step 6, I don't see where you loop back to 2.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Also in step 6, I don't see where you loop back to 2.
    Forget this comment, it appears there's just a right brace missing.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    the Lab is closed now and I dont have code warrior on my machine. when I try to dowload it, it crashes.
    Mmm, well, you're not going to learn too much if you don't have a compiler on your machine that you can experiment with.

    I take it you're using Mac OS? Code warrior runs on systems besides Macs, but I don't think it's widely used outside of the Mac world.

    So, when does it crash? Does the download crash? the installer? the program? the compile?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why To Use int main()
    By year2038bug in forum C Programming
    Replies: 2
    Last Post: 08-19-2005, 01:28 PM
  2. passing params to main()
    By mike11 in forum C++ Programming
    Replies: 14
    Last Post: 06-21-2005, 12:36 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Int Main or Void Main?
    By FromHolland in forum C++ Programming
    Replies: 9
    Last Post: 06-12-2003, 04:29 PM
  5. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 09:46 PM