Thread: Arrays

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    3

    Exclamation Arrays

    I having trouble with my program. Here is my code:

    // Airlines Reservation system

    #include <stdio.h>

    main()
    {

    int business[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // declaring array
    int economy[] = {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; // declaring array
    int buscount = 0; // variable
    int econocount = 0; // variable
    int integer1; // variable

    printf("Welcome to Sydney Airlines\n");
    printf("Please choose 1 for business class or 2 for economy class or 3 to exit:"); //prompt
    scanf("%d", &integer1); // reads the integer
    printf("\n"); // inserts a new line
    printf("\n"); // inserts a new line

    // enter choice between business or economy class

    // initializing business array

    while(integer1 != 3) {
    if(integer1 == 1){
    if(buscount > 9){
    printf("Switch to Economy");
    break;

    // Switches back to economy

    econocount++;
    printf("You are in seat %d, Economy", economy[econocount]);
    printf("Would you like economy class? 1 = Y 2 = N:");
    scanf("%d", &integer1);

    } // this bracket belongs to the first if statement

    printf("Welcome to Business Class\n");
    buscount++;
    printf("Your seat is %d", business[buscount-1]);

    } // this bracket belongs to the second if statement

    printf("\nEnter another? 1 = Y 2 = N\n");
    scanf("%d", &integer1);


    // this bracket belongs to the while loop



    // initializing economy array

    if(integer1 == 2){
    if(econocount > 19){
    printf("Switch to Bussiness");




    // Swithches back to business

    buscount++;
    printf("You are in seat %d, Business", business[buscount]);
    printf("Would you like economy class? 1 = Y 2 = N:");
    scanf("%d", &integer1);

    } // this bracket belongs to the first if statement

    printf("Welcome to Economy Class\n");
    econocount++;
    printf("Your seat is %d", economy[econocount]);

    } // this bracket belongs to the second if statement

    printf("\nEnter another? 1 = Y 2 = N\n");
    scanf("%d", &integer1);




    // ends the program

    if(integer1 == 3)
    printf("Thank you for using the program\n");

    } // this bracket is end the first while loop

    return 0; // program excutes successfully
    }





    When I press 1 it goes through the business class until it gets to 10. Then the user gets a message if they want to switch to economy class. that works. But if I restart the program at economy class all I get is a message saying Enter 1 = Y 2 = N. What is wrong with the program. Can someone help me out plz

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Homework? No code tags? Posting all your code? ARRRRRRRRGGGGGGGGGGH

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Umm... basically, you just have your while loop organized in such a strange way, problems like this are bound to come up. You need to redo it, using basically the following structure...
    Code:
    while(integer1 != 3)
    {
     if(integer1 == 1)
     {
      // Handle business here.
     }
     else if (integer 1 == 2)
     {
      // Handle economy here.
     }
     printf("\nEnter another? 1 = Y 2 = N\n");
     scanf("%d", &integer1);
    }
    printf("Thank you for using the program\n");
    Don't use any breaks for this program. They're unneccisary.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 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