Thread: whats wrong here??

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    Angry whats wrong here?? please its urgent!

    this is an airline program..seats 1 to 5 go to first class...seats 6 to 10 to economy class...if one class is full the message of choosing another class or wating 3 hours for nexf flight should appear...after 3 days my brain is consumed...heeeelp!!
    here is the code


    #include <stdio.h>
    #define SIZE 10

    int main ()

    {

    int seats[SIZE]= {0}, answer, first =1, economy = 6;

    printf( "Welcome to the Airline Reservation System of JFL AIR\n" );
    printf( "Choose your desired option:\n" );
    printf( "Please type 1 for \"First Class\".\n" );
    printf( "Please type 2 for \"Economy\". " );
    scanf( "%d", &answer);


    while( first <= 5 && economy <= 10){
    if ( answer == 1 ) {
    seats[first] = 1 ;
    printf( "\nYour seat number is: %d, First Class.\n", first);
    first++;
    }
    if( answer == 2) {
    seats[economy] = 6 ;
    printf( "\nYour seat number is: %d, Economy Class.\n", economy);
    economy++;
    }
    printf( "Please type 1 for \"First Class\".\n" );
    printf( "Please type 2 for \"Economy\". " );
    scanf( "%d", &answer);
    }

    while (first == 5 || economy ==10){
    if ( first == 5){
    printf( "First Class is full.\n" );
    printf( "Type 2 for an Economy Seat or Type 3 to wait for next flight\n ");
    scanf( "%d", &answer);
    }

    if ( economy == 10) {
    printf( "Economy Class is full.\n" );
    printf( "Type 1 for a First Class Seat or Type 3 to wait for next flight.\n ");
    scanf( "%d", &answer);
    }
    if (answer == 3)
    printf( "Next flight leaves in 3 hours." );
    }

    return 0;
    }
    Last edited by pancho; 03-20-2002 at 08:53 PM.

  2. #2
    Unregistered
    Guest
    Change:
    int seats[SIZE]= {0}, answer, first =1, economy = 6;

    to:
    int seats[SIZE]= {0}, answer, first =0, economy = 5;

    and change:
    while( first <= 5 && economy <= 10){

    to:
    while( first < 5 && economy < 10){

    Remember, array indexes start at 0, not 1.
    Also, why are you using an array, after you set the values in it, the data is not used again.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    Unhappy

    those changes still make my program run improperly..plz help me...i did my work but cant figure it out! i have 40 minutes to submit it..please! here is the code...run it and see what i mean....plz!

    #include <stdio.h>
    #define SIZE 10

    int main ()

    {

    int seats[SIZE]= {0}, answer, first = 1, economy = 6;

    printf( "Welcome to the Airline Reservation System of JFL AIR\n" );
    printf( "Choose your desired option:\n" );
    printf( "Please type 1 for \"First Class\".\n" );
    printf( "Please type 2 for \"Economy\". " );
    scanf( "%d", &answer);


    while( first <=5 && economy <= 10){
    if ( answer == 1 ) {
    seats[first] = 1;
    printf( "\nYour seat number is: %d, First Class.\n", first);
    first++;
    }
    if( answer == 2) {
    seats[economy] = 6;
    printf( "\nYour seat number is: %d, Economy Class.\n", economy);
    economy++;
    }
    printf( "\nPlease type 1 for \"First Class\".\n" );
    printf( "Please type 2 for \"Economy\". " );
    scanf( "%d", &answer);

    if ( first == 5){
    printf( "\nFirst Class is full.\n" );
    printf( "Type 2 for an Economy Seat or Type 3 to wait for next flight\n ");
    scanf( "%d", &answer);
    }

    if ( economy == 10) {
    printf( "\nEconomy Class is full.\n" );
    printf( "Type 1 for a First Class Seat or Type 3 to wait for next flight.\n ");
    scanf( "%d", &answer);
    }
    if (answer == 3)
    printf( "\nNext flight leaves in 3 hours." );

    }



    return 0;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are still using your arrays wrong. If you want the first five cells of your array, you access 0 through 4, not 0 through 5, not 1 through 5.

    If you want the last half, you access 5 through 9.

    As such, you loop this way:

    for( x = 0; x < 5; x++ ) //the first half

    for( x = 5; x < 10; x++ ) //the last half


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM