0
#include <stdio.h>

int main(void)

int movieChoice;
int seatNum;
int seatPlan[5][10] = { 0 };
int rows, cols;
char space;

void seatDisplay(int a[][10]);
void seatNew(int n[][10]);

void seatDisplay(int a[][10]) { //this function shows the seat plan

    for (int i = 0; i<5; i++) {
        for (int j = 0; j<10; j++) {
            cout << a[i][j];
            printf(" \n");
        }
    }
}

void seatNew(int n[][10]) { // this function shows the reserved seats

    for (int i = 0; i<5; i++) {
        for (int j = 0; j<10; j++) {
            cout << n[i][j];
            printf(" \n");
        }
    }

}

void seatRes(int x) {  //this function reserves the seats

    while (x>50 || x <= 0) {
        printf("Cannot process Reservation. ");
        printf("Please Enter number of seats to be reserved: ");
        scanf("%d", &seatNum);
    }
    printf("Please enter seat row and column you wish to reserve: \n");
    for (int i = 1; i <= x; i++) {
        printf("(row,column): ");
        scanf("%d %d %d", &rows, &space, &cols);
        seatPlan[rows][cols] = 1;
    }
}

int priceComp(int y) { //this function computes and displays the ticket price

    int price;
    price = y * 150;
    printf("You have reserved "  y  " seat(s).");
    printf("Ticket Price: ");
    return 0;
}

int main() { //this is the main function

    printf("Welcome to Movie World \n" );
    printf("1 - Movie 1 \n");
    printf("2 - Movie 2 \n");
    printf("3 - Movie 3 \n");
    printf("Enter your choice of Movie: ");
    scanf("%d", &movieChoice);

    switch (movieChoice) {

    case 1 :
        printf("Movie 1 \n");
        printf("Seating Reservation \n");
        seatDisplay(seatPlan);
        printf("How many seats would you like to reserve?: ");
        scanf("%d", &seatNum);
        seatRes(seatNum);
        printf("Seating Reservation \n");
        printf("1 marks your reserved seat \n");
        printf("------Movie 1 SEAT PLAN----------- \n");
        seatNew(seatPlan);
        priceComp(seatNum);
        break;

    case 2 :
        printf("Movie 2 \n" );
        printf("Seating Reservation \n");
        seatDisplay(seatPlan);
        printf("How many seats would you like to reserve?: ");
        scanf("%d", &seatNum);
        seatRes(seatNum);
        printf("Seating Reservation");
        printf("1 marks your reserved seat \n");
        printf("------Movie 2 SEAT PLAN----------- \n");
        seatNew(seatPlan);
        priceComp(seatNum);
        break;

    case 3 :
        printf("Movie 3 \n" );
        printf("Seating Reservation \n" );
        seatDisplay(seatPlan);
        printf("How many seats would you like to reserve?: ");
        scanf("%d", &seatNum);
        seatRes(seatNum);
        printf("Seating Reservation");

        printf("1 marks your reserved seat \n");
        printf("------Movie 3 SEAT PLAN----------- \n");
        seatNew(seatPlan);
        priceComp(seatNum);
        break;

    default:
        printf("Invalid Input \n");

    }
    return 0;
    getchar();
}

I'm studying C with converting C++ to C, but it doesn't moving.
I don't know C++ exactly but study it just for website.
I'm so thank you if you can help me, please...

2
Contributors
1
Reply
23
Views
9 Months
Discussion Span
Last Post by David W
0

You may like to try someting like this to get valid input and to prevent program crashing on accidental input of an invalid type.

#include <stdio.h>

#define ROWS 5
#define COLS 10
#define COST_PER_SEAT 150

/* Simple input of an integer...  i.e. NO range checking done */
int takeInInt( const char* prompt )
{
    int testInt;
    for( ; ; ) { /* an example of a C/C++ forever loop ... until 'return' */
        fputs( prompt, stdout );
        fflush( stdout );
        if( scanf( "%d", &testInt ) && getchar() == '\n' ) {
            return testInt;
        }
        /* else ...*/
        while( getchar() != '\n' ); /* 'flush' stdin ... as we go */
        puts( "Invalid input! Integers only please ..." );
    }
}

int takeInIntMinMax( const char* prompt, int min, int max )
{
    int testInt;
    for( ; ; ) { /* an example of a C/C++ forever loop ... until 'return' */
        fputs( prompt, stdout );
        fflush( stdout );
        if( scanf( "%d", &testInt ) && getchar() == '\n' ) {
            if( testInt >= min && testInt <= max ) {
                return testInt;
            } else {
                printf("Valid input range is %d..%d\n", min, max);
                continue;
            }
        }

        /* else ...*/
        while( getchar() != '\n' ); /* 'flush' stdin ... as we go */
        puts( "Invalid input! Integers only please ..." );
    }
}

void seatDisplay(int a[ROWS][COLS]) { /* this function shows the seat plan */
    int i, j;
    for (i = 0; i < ROWS; i++) {
        for (j = 0; j < COLS; j++) {
            printf( "%d ", a[i][j] );
        }
        putchar('\n');
    }
}

void seatRes(int x, int seatPlan[ROWS][COLS]) {  /* this function reserves the seats */
    int i, r, c;
    char space;
    while ( x > ROWS*COLS || x <= 0) {
        printf("Seats available  ");
        x = takeInInt("Please Enter number of seats to be reserved: ");
    }
    printf("Please enter seat row and column you wish to reserve: \n");
    for (i = 1; i <= x; i++) {
        printf("Re, seat %d (row,column): ", i);
        if( scanf("%d %c %d", &r, &space, &c) == 3 && getchar() == '\n' ) {
            if(r >= 0 && r < ROWS && c >= 0 && c < COLS ) {
                if( !seatPlan[r][c] ) {
                    seatPlan[r][c] = 1;
                }
                else {
                    printf( "That seat is already taken.\n" );
                    --i;
                }
            } else {
                printf( "Valid row, col is 0..%d, 0..%d.\n", ROWS-1, COLS-1 );
                --i;
            }
        }
        else {
            printf("Only integers are valid input here ... try again.\n");
            --i;
            while( getchar() != '\n' ); /* flush stdin ... */
        }
    }
}

void priceComp(int numSeats) { /* this function computes and displays the ticket price */

    int price = numSeats * COST_PER_SEAT;
    printf("You have reserved %d seat(s).\n", numSeats);
    printf("Ticket Price: %d\n", price );
}

void initAry( int a[ROWS][COLS] )
{
    int i, j;
    for (i = 0; i < ROWS; i++) {
        for (j = 0; j < COLS; j++) {
            a[i][j] = 0;
        }
    }
}

int main() {

    int movieChoice;
    int numSeatsRequested;
    int seatPlan[ROWS][COLS];
    initAry( seatPlan );

    printf("Seating Reservation \n");
    seatDisplay(seatPlan);

    printf("Welcome to Movie World \n" );
    printf("1 - Movie 1 \n");
    printf("2 - Movie 2 \n");
    printf("3 - Movie 3 \n");
    movieChoice = takeInIntMinMax("Enter your choice of Movie: ", 1, 3);

    printf("Movie %d \n", movieChoice );
    printf("Seating Reservation \n");
    numSeatsRequested = takeInIntMinMax("How many seats would you like to reserve? ", 1, ROWS*COLS);
    seatRes(numSeatsRequested, seatPlan);

    printf("Seating Reservation \n");
    printf("1 marks your reserved seat(s)) \n");
    printf("------Movie %d SEAT PLAN----------- \n", movieChoice );
    seatDisplay(seatPlan);

    priceComp(numSeatsRequested);

    return 0;
    getchar();
}

Edited by David W

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.