Having trouble getting this program to assign a seat and loop

Viewed 33

Having trouble getting this code to print 1 seat then loop back to user prompt. it should loop back until the seats are filled then display first class/ecoomy full. this is the code i have so far.

int main(int argc, char** argv) {

    const int firstClass[] = {1, 2, 3, 4}; //first class array list
    const int economyClass[] = {5, 6, 7, 8, 9, 10, 11, 12}; //economy array list
    int firstLen = 4; // first class length
    int economyLen = 8; //economy class length
    int userInput;
    int i;
    
    //user prompt
    printf("Please type 1 for First Class\n");
    printf("Please type 2 for Economy\n");
    printf("Please type 0 to Quit\n");
    printf("\n");
    scanf("%d", &userInput); // scanning for user input
    
     
    if(userInput == 1){
        for(i = 0; i < 4; i++){
            if(i < firstLen){
                printf("Class: First     Seat Number: %d\n", firstClass[i]);
            }
            else{
                printf("First Class is Full. Next flight is tomorrow.");
            }
            
        }
    }
    
    if(userInput == 2){
        for (i = 0; i < 8; i++){
            if(i < economyLen){
                printf("Class: Economy      Seat Number: %d\n", economyClass[i]);
            }
            else{
                printf("Economy is Full. Next flight is tomorrow.");
            }
        }
    }
}
1 Answers

As you want to take input from user as long as userInput != 0, so you need to take user input within a loop.

This loop will break when userInput will be 0.

Now about printing available seat from firstClass and economyClass, you can maintain 2 variables through which you keep track of last printed seat. Just like you maintain indexing while printing values of an array.

Here's a sample code. I've given some inline comments for better understanding.

int main(int argc, char** argv) {
    const int firstClass[] = {1, 2, 3, 4}; //first class array list
    const int economyClass[] = {5, 6, 7, 8, 9, 10, 11, 12}; //economy array list
    int firstLen = 4; // first class length
    int economyLen = 8; //economy class length
    int firstClassSeatNumber = 0;
    int economyClassSeatNumber = 0;
    int userInput;
    int i;
    
    while(true) {
        //user prompt
        printf("Please type 1 for First Class\n");
        printf("Please type 2 for Economy\n");
        printf("Please type 0 to Quit\n");
        printf("\n");
        scanf("%d", &userInput); // scanning for user input
        
        if (userInput == 0) {
            break;  // breaking the loop as we won't take further input from user
        }
        
        if(userInput == 1) {
            if (firstClassSeatNumber < firstLen) {
                // if we've any seat left in first class, then we'll print it
                printf("Class: First     Seat Number: %d\n", firstClass[firstClassSeatNumber]);
                firstClassSeatNumber++; // moving to next seat of first class
            }
            else {
                // don't have any seat left in first class
                printf("First Class is Full. Next flight is tomorrow.");
            }
        }
        else if(userInput == 2) {
            if (economyClassSeatNumber < economyLen) {
                // if we've any seat left in economy class, then we'll print it
                printf("Class: Economy      Seat Number: %d\n", economyClass[economyClassSeatNumber]);
                economyClassSeatNumber++; // moving to next seat of economy class
            }
            else {
                // don't have any seat left in economy class
                printf("Economy is Full. Next flight is tomorrow.");
            }
        }
    }
}
Related