Thread: Please explain...

  1. #1
    -
    Join Date
    Feb 2003
    Posts
    47

    Please explain...

    This message is for anyone realy but if 'Prelude' is around id rather get a reply off him/her because he/she helped me with the code, here it is...
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    static int OutOfRange ( int Val, int Lower, int Upper )  /* Start */
    {
      return Val < Lower || Upper < Val;
    }
    
    static int Menu ( void )
    {
      int Option = 0;
      while ( OutOfRange ( Option, 1, 4 ) )                       /* end */
      {
        
        printf ( "Please chose from the options below:\n" );
        printf ( "[1] Start\n" );
        printf ( "[2] About\n" );
        printf ( "[3] Help\n" );
        printf ( "[4] Exit\n\n" );
        printf ( "[select]: " );
        
        /* Always check the return value of input functions */
        if ( scanf ( " %d", &Option ) != 1 )                              /* start */
        {
          /* Tidy up */
          int Ch;
    
          while ( ( Ch = getchar() ) != '\n' && Ch != EOF );
            
        }
        
        if ( OutOfRange ( Option, 1, 4 ) )
          printf ( "\nInvalid entry, please try again.\n" );
      }
      
      return Option;                                                            /* end */
    }
    
    int main ( void )
    {
      SetConsoleTitle ( "Robinson's Quizzes" );  
      switch ( Menu() ) 
      {
        case 1:
            Start();
            /* test stub */
            system ( "PAUSE" );
            break;
        
        case 2:
            About();
            /* test stub */
            system ( "PAUSE" );
            break;
        
        case 3:
            Help();
            /* test stub */
            system ( "PAUSE" );
            break;
        
        case 4:
            printf ( "Have a nice day!\n" );
            system ( "PAUSE" );
            break;
      }
      
      return 0;
    }
    
    /* functions */
    
    /* About the program */
    About()
    {
    printf ( "wang" );
    }
    
    /* starting the program */
    Start()
    {
        printf ( "Starting...\n" );
    }
    
    /* help to work the program */
    Help()
    {
        printf ( "No Help atm...\n" );
    }
    The question im asking is what does the code enclosed in /* start */ and /*end*/ mean? this was code i didnt write my self, if somone would help me understand this id appreciate it.

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    C style comments, they are ignored by the compiler.

    /* starts a comment

    */ ends a comment
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    -
    Join Date
    Feb 2003
    Posts
    47
    Maybe i didnt explain my self proply, i ment the code inside thoes comments, i know what comments are, i wrote all the code you see bar the parts inside /* start */ and /* end */
    e.g

    Code:
    static int OutOfRange ( int Val, int Lower, int Upper )  /* Start */
    {
      return Val < Lower || Upper < Val;
    }
    
    static int Menu ( void )
    {
      int Option = 0;
      while ( OutOfRange ( Option, 1, 4 ) )                       /* end */
    90% of this i dont understand...
    thanks if anyone can help.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I thought OutOfRange was pretty self-explanitory.
    Code:
    static int OutOfRange ( int Val, int Lower, int Upper )
    {
      return Val < Lower || Upper < Val;
    }
    The function OutOfRange takes parameters Val, Lower, and Upper. It returns true (1) if Val is less than Lower or Val is greater than Upper. If Val is equal to or greater than Lower and Val is equal to or less than Upper, it returns false (0). Or simply, it returns 1 if Val is OutOfRange of Lower to Upper (inclusive).
    Code:
    static int Menu ( void )
    {
      int Option = 0;
      while ( OutOfRange ( Option, 1, 4 ) )
    When called int the function Menu, Option (whose value is passed to OutOfRange as the parameter Val) is initialized to 0. This will cause OutOfRange(Option,1,4) to return 1 and the while loop will be entered.
    Code:
        if ( scanf ( " %d", &Option ) != 1 )
        {
          /* Tidy up */
          int Ch;
          while ( ( Ch = getchar() ) != '\n' && Ch != EOF );
        }
    The scanf statement checks to see if it successfully scanned an integer into Option. If not, the "Tidy up" part consumes characters remaining in the stdin up to a newline. This might happen if the user enters "twelve".
    Code:
        if ( OutOfRange ( Option, 1, 4 ) )
          printf ( "\nInvalid entry, please try again.\n" );
      }
      return Option;
    Then the OutOfRange function is used again to display an error message if the new value of Option is out of range. The while loop keeps going as long as you enter a value that is OutOfRange. If you enter a valid Option, the loop is broken and the value of Option is returned.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    -
    Join Date
    Feb 2003
    Posts
    47
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you explain these bitwise operations?
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 10-29-2008, 01:19 PM
  2. Please explain?
    By neo_phyte in forum C Programming
    Replies: 3
    Last Post: 08-25-2006, 05:23 AM
  3. Can someone explain to me what this code means
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2002, 12:36 PM
  4. Replies: 4
    Last Post: 11-19-2002, 09:18 PM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM