C代码数组.不确定如何使用用户选择将数组从0更新到1



[edit]我能够解决我最初的一个问题。事实证明,这是我if语句的一个问题。如果在那里,我需要添加一些其他内容,并将5和10改为6和11。

如果有人有任何关于如何帮助我处理数组部分的信息-如何打印我的"座位表",将开放座位显示为0,并在每次就座时将其更改为1,我将不胜感激。

根据您使用的C编译器,您应该收到一个关于语句无效的警告。

/home/craig/C_Programs/Console/Reservations/main.c|99|warning: statement with no effect [-Wunused-value]|

这将引用代码中的以下语句之一。

taken_seats[economy_class];

这句话基本上没有起到任何作用。

考虑到这一点,我对您的代码进行了一些修改和清理,并更改了这些代码行,为它们分配了一个布尔值,这可能就是应该发生的事情。以下是您的代码的修订版本。

#include <stdio.h>
#include <stdbool.h>
int main(void)
{
/*write a program to assign seats on each flight of the airline's only plane
(capacity 10 seats).
Display the following menu:
Please type 1 for "first class"
Please type 2 for "economy"
*1 = first class (seats 1-5).
*2 = economy (seats 6-10).
Print a boarding pass indicating seat number and class.
* Use a one-dimensional array to represent seating chart.
*initialize all the elements of the array to 0 to indicate open_seat, 1 = taken_seat
Never assign a seat that has already been assigned.
When one section is full, ask person if other class is acceptable.
*yes, make appropriate seat assignment.
*no, print message "Next flight leaves in 3 hours."*/
bool taken_seats [10] = {0};
int first_class = 2;
int economy_class = 6;
int class_choice;
int backup_class_choice;
do
{
printf("nnPlease type 1 for 'First Class'nPlease type 2 for 'Economy'nPlease type 3 to exit the menunn");
scanf("%d", &class_choice);
//FIRST CLASS
if(class_choice == 1 && first_class < 6)
{
taken_seats[first_class] = true;   /* Perform update of the seat */
printf("First Class ticket.nSeat: %d", first_class);
first_class++;
}//end if
if(class_choice == 1 && first_class == 6)
{
printf("Sorry, all seats are taken.");
}//end if
if(class_choice == 1 && first_class == 6 && economy_class == 11)
{
printf("1There are no more seats available.  The next flight leaves in 3 hours.  We apologize for any inconveniencen");
} //end if

if(class_choice == 1 && first_class == 6 && economy_class < 10)
{
printf("Would you like a seat in Economy? Type 1 for yes and 2 for no.");
scanf("%d", &backup_class_choice);
}//end if
if(backup_class_choice == 1)
{
taken_seats[economy_class] = true;
printf("Economy Class ticket.nSeat: ");
printf("%dn", economy_class);
economy_class++;
}//end if
if(backup_class_choice == 2)
{
printf("The next plane leaves in 3 hours. We apologize for any inconvenience.  Goodbye.n");
}//end if

//ECONOMY
if(class_choice == 2 && economy_class < 10)
{
taken_seats[economy_class] = true;
printf("Economy Class ticket.nSeat: ");
printf("%dn", economy_class);
economy_class++;
}//end if
if(class_choice == 2 && economy_class == 10)
{
printf("Sorry, all seats are taken.");
}//end if
if(class_choice == 2 && economy_class == 10 && first_class == 5)
{
printf("There are no more economy seats available.  The next flight leaves in 3 hours.  We apologize for any inconveniencen");
}//end if

if(class_choice == 2 && economy_class == 10 && first_class < 5)
{
printf("Would you like a seat in First Class? Type 1 for yes and 2 for no.");
scanf("%d", &backup_class_choice);
}//end if
if(backup_class_choice == 1)
{
taken_seats[first_class] = true;
printf("First Class ticket.nSeat: ");
printf("%d", first_class);
first_class++;
}//end if
if(backup_class_choice == 2)
{
printf("The next plane leaves in 3 hours. We apologize for any inconveniences. Goodbye.n");
}//end if
}//end do
while(class_choice != 3);
return 0;
}//end main

这段代码似乎还有更多的改进要做,比如可能打印出座位分配数组,但这应该会让你继续前进。

尝试一下,看看它是否符合你项目的精神。