2015-11-10 55 views
-1

我正在爲我的班級創建一個程序。我必須展示每排有30個座位的15排劇院的佈局。該程序應該要求用戶輸入每行的價格,我已經完成並且似乎沒有問題。然後,用戶需要輸入正在出售的座位的行和列,然後顯示座位所佔據的座位。空座位以#代表,被佔席位以*代表。出於某種原因,我無法讓程序正確顯示劇院佈局。它不使用英鎊符號表示空座位,並且在座位出售時不顯示佈局的差異。這裏是我的代碼:C中的劇院程序問題

#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("  123456789"); 
     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; 
} 

只要你知道,我的教授指示類中的每個printf()語句後寫fflush(stdout),因爲他說有在Eclipse(我的編譯器),使得它需要一個錯誤。

+1

'的printf( 「%C」,STS);'所以你打印整個數組一個字符,並且你已經忘記了'fflush()'這個...... – John3136

+0

就這麼你知道,不知道他們在說什麼的教授比流行的編譯器中出現明顯的錯誤更頻繁。 –

+0

所以我試着將它改爲'printf(「%c」,sts [i] [j]);'我不確定這是否會阻止整個數組打印爲單個字符,但是我已經試着再次運行我的程序,它的行爲與以前一樣。我還添加了缺少的'fflush()'@ John3136 – redcardinal

回答

1

如果你正在尋找1件你能做的事情,那真的會幫助你解決你的代碼問題,在編譯時啓用警告。至少-Wall -Wextra。 (您可以添加-pedantic以獲取更多警告)。

這些警告告訴你在哪裏尋找解決你的代碼問題。例如:

$ 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 = '*'; 

僅修復這些問題將讓你的代碼,以顯示你正在尋找的地圖:

#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 ("  123456789"); 
    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; 
} 

示例輸出

$ ./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 
     123456789
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 ############################## 

爲了完整起見,以下是使用的輸入文件:

$ 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 

注:糾正其中的打印'*'列,調整您在這裏指數:

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

非常感謝大家,你們都非常樂於助人。看起來主要問題實際上是將'printf(「%c」,sts);''改爲'printf(「%c」,sts [i] [j]);'對所有人表示最良好的祝願。 – redcardinal

+0

是的,這是主要問題。我還添加了'%2d'字段寬度說明符,以便您的列可以直接打印。當您使用'scanf'時,您應該確認退貨,以便驗證與您的請求匹配的用戶輸入數據。例如if(scanf(「%d%d」,&row,&column)!= 2)'(處理錯誤,重複請求等)祝你好運! –