Thread: Help with arrays!!!

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    5

    Help with arrays!!!

    (Airline reservation problem) Capacity of 10 seats, type 1 for first class, 2 for economy, can not fill a seat twice, if full then a message will appear when the next flight leaves. I have worked on this for awhile now and can not get this to execute. Anyone have any suggestions? 1 Error message on the close of the last while.

    Code:
    #include <stdio.h>   
    #define MAX_PASSENGERS  10
    #define  EMPTY 0                   
    #define  FULL  1                     
    #define  FIRST_CLASS 1 
    #define  ECONOMY 2
    /* function prototypes */
    int iSeats[ MAX_PASSENGERS ]; /* array of the number of seats */
    
    int main( void ) /* function main begins program execution */
    {
       /* initialize */ 
       int response; /* response counter */
       int iPassengers = 0, i; /* passengers set to zero */
    
       /* output original array */
       for( i = 0; i < MAX_PASSENGERS; i++ ){ 
          iSeats[i] = 0; /* seats set to zero */
       
          while( iPassengers < MAX_PASSENGERS ){ /* for passengers until max */
         
             do{ /* prompt the user */
                printf( "Please type 1 for First Class.\n" ); /* prompt */
                printf( "Please type 2 for Economy Class.\n"); /* prompt */
                
                scanf( "%d" , &response ); /* read response from user */
             
                /* while checking response from user */   
                while ( response != FIRST_CLASS && response != ECONOMY ){
                   iPassengers++; /* increase the passengers */
                
                   if( response == FIRST_CLASS ){ /* checking against first class */
                      i = 0; /* int loop counter */
      
                      /* while checking if seats are full */           
                      while( iSeats[ i ] == FULL  && i < MAX_PASSENGERS / 2 ){
                         i++; /* array looking for an empty */ 
                     
                         if( i >= MAX_PASSENGERS / 2 ) /* if seat are full */
                            printf( "Next flight leaves in 3 hours" );
                         else{ /* able to get a seat */
                            iSeats[i] = FULL;
                         } /* ends else */
                      } /* ends while */
                   } /* ends if */        
                } /* ends while */
             } /* ends do */   
          } /* ends while */ error message here!!!!!!!!!!!!
       } /* ends for */   
    
       system("PAUSE"); /* freeze viewing screen */
       
       return 0; /* indicates successful termination */
    } /* ends main */

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    ... so what's the error message?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So you've dug a very nice 7 meter deep hole. It makes it tough to get out and debug anything.

    Did the idea of using other functions to help break up this job, not seem like a good idea? This is what I can "clever" code. Debugging clever code is a meat cleaver. I like that alliteration.

    I know any time now, you'll let us in on just WHAT the error message was, right? That's an important detail to include!

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    The error message is syntax error before "}" token, which is at the very end of the program at the last while loop. This is the only message that shows up when I try to compile this.

    Please bare with me, I am only 6 weeks into c programming and every week is something new.

    Thanks for the quick response!

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    It's a
    Code:
    do
    {
    }while (whatever);
    loop. Where's your while(whatever)?

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Do you think that I should switch the do with the while. In other words, do (iPassengers < MAX_PASSENGERS) while prompting the user for information.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Does this program need a lot of help?

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You're doing something like this:
    Code:
    while(x != 0)
    {
      if(x == 0) ...
    }
    This clearly is problematic.

    I'd recommend working out, on paper, how this program should flow. Once you've satisfied yourself that it makes sense, work on translating it to C. The desire to just sit down and code is strong, I know; but having a good design to follow will make your life easier, I promise.

    I'd also like to discuss your comments. I suspect this is for a class, and you're required to use comments (which is a good thing); however, proper commenting is difficult to do. The goal in commenting should not be to translate C to English. Anybody reading the code already knows C; and if he doesn't, some comments aren't going to teach him the language.

    Comments like the following do not add any information:
    Code:
    iPassengers++; /* increase the passengers */
    printf( "Please type 1 for First Class.\n" ); /* prompt */
    int iPassengers = 0, i; /* passengers set to zero */
    int main( void ) /* function main begins program execution */
    scanf( "%d" , &response ); /* read response from user */
    All these comments merely explain what the code does; they're redundant. Nobody is going to be confused when they see “iPassengers++”. Instead, what you want your comments to do is explain, in more broad terms, what the code is doing. You might sketch out the algorithm you're using and why it was chosen, for example.

    Here's a comment from FreeBSD's syslogd:
    Code:
    /* Don't allow users to log kernel messages.
     * NOTE: since LOG_KERN == 0 this will also match
     *       messages with no facility specified.
     */
      if ((pri & LOG_FACMASK) == LOG_KERN && !KeepKernFac)
              pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri));
    If you're not familiar with the code, what it's doing might not make sense, hence the first sentence. But even more important, I think, is the note: this is an excellent use of commenting, because it explains behavior that might otherwise be surprising. It's not clear at all from this code that no facility and a facility of kernel look identical. The comment here doesn't say “perform a bitwise and of pri with LOG_FACMASK and see if it equals LOG_KERN”, etc. That much is clear from the code. Rather, it explains what the code means.

    It'll take time to learn proper commenting, and I'm much happier that you're over-commenting rather than under-commenting.

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Thanks cas for your time and explanation. I have time and will go back to the drawing board to try to organize this.

    This assignment is for a c programming college level class. We are using the Deitel, C How To Program sixth edition text. Most of my documentation came directly from the book. Java last semester and C Programming this semester. Almost done with the programming classes :-)

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    I wonder what's better, Java first or C first...

    Anyway, if you'd like some help getting started, you can use a high level pseudo-pseudocode that can be relatively easily turned into C. Something like:
    Code:
    While there are seats available
        Ask the user what type of seat
            If the seat type is valid
                Insert seat (this should be expanded)
            Else <the seat type is not valid>
    ... etc etc
    You can organize it however you want, of course. But you can see how you could translate this to C:
    Code:
    while(seats_avail < MAXSEATS)
    {
      seat = read_seat();
      if(seat == ECONOMY || seat == FIRST) ...
    }

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What I don't like about this whole outer loop logic, is this:

    What happens if MAX_PASSENGERS, never reserve a seat? Most planes I've flown on had at least one seat that was empty.

    I'd like to see main() call a menu() function. In the menu function, you could do things like:
    1) Reserve a seat 2) Cancel a reservation, 3) Query seat availability and etc.

    Depending on what you chose from the menu(), you'd call various functions to do what you'd selected. Switch statements are good for this sometimes.

    Like all menu systems, it runs in a big (total) loop - and you get out only when you quit the menu() function, by making that choice, as your selection.

    I'd definitely recommend that kind of a design.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create and manipulate Terabyte size Arrays with Win32API
    By KrishnaPG in forum Windows Programming
    Replies: 1
    Last Post: 11-05-2009, 04:08 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

Tags for this Thread