简体   繁体   中英

Theater Program Problems in C

I am creating a program for my class. I have to display the layout of a theater that has 15 rows with 30 seats in each row. The program should ask the user to enter a price for each row, which I have done and don't seem to have a problem with. Then the user needs to enter the rows and columns of seats that are being sold, and then display the theater with the seats taken up. Empty seats are represented with a # and taken seats are represented with a *. For some reason I can't get the program to display the theater layout correctly. It isn't using pound symbols for empty seats and doesn't show a difference in the layout when seats are sold. Here is my code:

#include <stdio.h>
#include <stdlib.h>

void mapSeats(int i, int j, char sts[][30]){
        char TAKEN='*';
        char EMPTY='#';
        int rw=15;
        int col=30;
        printf("                  Seats\n");
        fflush(stdout);
        printf("      123456789012345678901234567890");
        fflush(stdout);
        for(i=0; i<rw; i=i+1){
            printf("\nRow %d ", i);
            fflush(stdout);
            for(j=0; j<col; j=j+1){
                printf("%c", sts);
            }
        }
    }

void theatre(void){
    int row=15;
    int column=30;
    float prices[15];
    char sts[row][column];
    char TAKEN='*';
    char EMPTY='#';
    int reserve;
    int i=0;
    int j=0;
    //float cost;
    //int static total;

    printf("Enter the price for each row of seats.\n");
    fflush(stdout);
    for(row=0; row<15; row=row+1){
        printf("Row %d:\n", row);
        fflush(stdout);
        scanf("%f", &prices[row]);
    }

    /*printf("What would you like to do? Select a number:\n");
    printf("1: Reserve seats");
    printf("2: View total ticket sales");
    printf("3: View sold and available seats");*/

    for(i=0; i<row; i=i+1){
        for(j=0; j<column; j=j+1){
            sts[i][j]=EMPTY;
        }
    }

    mapSeats(i, j, sts);


    printf("\nHow many seats would you like to reserve?\n");
    fflush(stdout);
    scanf("%d", &reserve);

    printf("Enter the row and column number for the desired seat(s).\n");
    fflush(stdout);
    for(int k=1; k<=reserve; k=k+1){
        scanf("%d %d", &row, &column);
        printf("\nYou have selected Row %d, Column %d\n", row, column);
        fflush(stdout);
        for(i=0; i<15; i=i+1){
            for(j=0; j<=30; j=j+1){
                if(row==i && column==j){
                    sts[i][j]=TAKEN;
                }
            }
        }
    }
    mapSeats(i, j, sts);





}

int main(void) {
    theatre();
    return 0;
}

Just so you know, my professor instructed the class to write fflush(stdout) after every printf() statement because he said there was a bug in Eclipse (my compiler) that made it necessary.

If you are looking for 1 thing you can do that will really help you sort out issues with your code, enable Warnings when you compile. At least -Wall -Wextra . (you can add -pedantic for additional warnings).

The warnings tell you exactly where to look to address issues with your code. For example:

$ gcc -Wall -Wextra -Ofast -o bin/theater theater.c

theater.c: In function ‘mapSeats’:
theater.c:18:13: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char (*)[30]’ [-Wformat=]
             printf ("%c", sts);
             ^
theater.c:7:10: warning: unused variable ‘EMPTY’ [-Wunused-variable]
     char EMPTY = '#';
          ^
theater.c:6:10: warning: unused variable ‘TAKEN’ [-Wunused-variable]
     char TAKEN = '*';

Fixing just those issues will allow your code to show the map you are looking for:

#include <stdio.h>
#include <stdlib.h>

void mapSeats (int i, int j, char sts[][30])
{
//     char TAKEN = '*';
//     char EMPTY = '#';
    int rw = 15;
    int col = 30;
    printf ("                  Seats\n");
    fflush (stdout);
    printf ("       123456789012345678901234567890");
    fflush (stdout);
    for (i = 0; i < rw; i = i + 1) {
        printf ("\nRow %2d ", i);
        fflush (stdout);
        for (j = 0; j < col; j = j + 1) {
            printf ("%c", sts[i][j]);
            fflush (stdout);
        }
    }
    putchar ('\n');
}

void theatre (void)
{
    int row = 15;
    int column = 30;
    float prices[15];
    char sts[row][column];
    char TAKEN = '*';
    char EMPTY = '#';
    int reserve;
    int i = 0;
    int j = 0;
    //float cost;
    //int static total;

    printf ("Enter the price for each row of seats.\n");
    fflush (stdout);
    for (row = 0; row < 15; row = row + 1) {
        printf ("Row %2d:\n", row);
        fflush (stdout);
        scanf ("%f", &prices[row]);
    }

    /*printf("What would you like to do? Select a number:\n");
    printf("1: Reserve seats");
    printf("2: View total ticket sales");
    printf("3: View sold and available seats"); */

    for (i = 0; i < row; i = i + 1) {
        for (j = 0; j < column; j = j + 1) {
            sts[i][j] = EMPTY;
        }
    }

    mapSeats (i, j, sts);

    printf ("\nHow many seats would you like to reserve?\n");
    fflush (stdout);
    scanf ("%d", &reserve);

    printf ("Enter the row and column number for the desired seat(s).\n");
    fflush (stdout);
    for (int k = 1; k <= reserve; k = k + 1) {
        scanf ("%d %d", &row, &column);
        printf ("\nYou have selected Row %d, Column %d\n", row, column);
        fflush (stdout);
        for (i = 0; i < 15; i = i + 1) {
            for (j = 0; j <= 30; j = j + 1) {
                if (row == i && column == j) {
                    sts[i][j] = TAKEN;
                }
            }
        }
    }
    mapSeats (i, j, sts);
}

int main (void)
{
    theatre ();
    return 0;
}

Example Output

$ ./bin/theater <seatprice.txt

<snip>

How many seats would you like to reserve?
Enter the row and column number for the desired seat(s).

You have selected Row 1, Column 5

You have selected Row 1, Column 6

You have selected Row 2, Column 6

You have selected Row 2, Column 7
                Seats
       123456789012345678901234567890
Row  0 ##############################
Row  1 #####**#######################
Row  2 ######**######################
Row  3 ##############################
Row  4 ##############################
Row  5 ##############################
Row  6 ##############################
Row  7 ##############################
Row  8 ##############################
Row  9 ##############################
Row 10 ##############################
Row 11 ##############################
Row 12 ##############################
Row 13 ##############################
Row 14 ##############################

For completeness, the following was the input file used:

$ cat seatprice.txt
500
450
400
350
300
250
200
150
125
120
115
110
105
100
90
4
1 5
1 6
2 6
2 7

Note: to correct the column where the '*' are printed, adjust your index here:

if (row == i && column == j+1) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM