我知道我可能缺少明显的东西,但是我的代码只会在main函数中运行一次do循环。运行所选功能(通过菜单选择)后,程序退出而不是重新启动循环。我不确定我的循环出了什么问题。请告知。
P.S。该程序用C ++编写]
这里是程序的整体。除主函数中的do循环外,所有函数均可用。
//It also includes functions to cancel a selection and to show all seats on the flight.
//These allow the predefined functions within the program to operate.
#include <iostream>
#include <string>
//This sets the namespace of the entire program to std
using namespace std;
//This creates a constant variable for the rows of seats on the flight.
const int rows = 13;
//This creates a constant variable for the seats in each row on the flight.
const int seats = 6;
//This line creates the array to store the seat assignments.
char flight[rows][seats];
//This function displays the seating chart for the flight
int display()
{
cout << "Displaying the current seating assignments for the flight." << endl;
cout << "The '*' symbol means the seat is available and 'X' means it is not." << endl;
cout << " A " << "B " << "C " << "D " << "E " << "F " << endl;
for(int i=0; i < rows; i++)
{
cout << "Row " << i+1 << " ";
for(int j=0; j < seats; j++)
{
cout << flight[i][j] << " ";
}
cout << endl;
}
}
//This function allows the user to assign their seat.
int assign()
{
int type;
cout << "To begin making your seat assignment please select your ticket type." << endl;
cout << "1. First Class" << endl;
cout << "2. Business Class" << endl;
cout << "3. Economy Class" << endl;
cout << "Please select 1, 2, or 3 from the menu." << endl;
cin >> type;
int fcrow;
string fcseat;
if(type == 1)
{
cout << "You selected First Class" << endl;
cout << "Please review available First Class Seats." << endl;
cout << " A " << "B " << "C " << "D " << "E " << "F " << endl;
for(int f = 0; f < 2; f++)
{
cout << "Row " << f+1 << " ";
for(int g = 0; g < seats; g++)
{
cout << flight[f][g] << " ";
}
cout << endl;
}
cout << "Please enter in the desired row (1 or 2)" << endl;
cin >> fcrow;
cout << "Please enter in the desired seat (A-F)" << endl;
cin >> fcseat;
cout << "You selected Row " << fcrow << " Seat " << fcseat << endl;
int fcsi;
if(fcseat == "A")
{
fcsi = 0;
}
if(fcseat == "B")
{
fcsi = 1;
}
if(fcseat == "C")
{
fcsi = 2;
}
if(fcseat == "D")
{
fcsi = 3;
}
if(fcseat == "E")
{
fcsi = 4;
}
if(fcseat == "F")
{
fcsi = 5;
}
flight[fcrow-1][fcsi] = 'X';
cout << "Displaying Updated Flight Seating Chart" << endl;
return display();
}
int bcrow;
string bcseat;
if(type == 2)
{
cout << "You selected Business Class" << endl;
cout << "Please review available Business Class Seats." << endl;
cout << " A " << "B " << "C " << "D " << "E " << "F " << endl;
for(int h = 2; h < 7; h++)
{
cout << "Row " << h+1 << " ";
for(int k = 0; k < seats; k++)
{
cout << flight[h][k] << " ";
}
cout << endl;
}
cout << "Please enter in the desired row (3-7)" << endl;
cin >> bcrow;
cout << "Please enter in the desired seat (A-F)" << endl;
cin >> bcseat;
cout << "You selected Row " << bcrow << " Seat " << bcseat << endl;
int bcsi;
if(bcseat == "A")
{
bcsi = 0;
}
if(bcseat == "B")
{
bcsi = 1;
}
if(bcseat == "C")
{
bcsi = 2;
}
if(bcseat == "D")
{
bcsi = 3;
}
if(bcseat == "E")
{
bcsi = 4;
}
if(bcseat == "F")
{
bcsi = 5;
}
flight[bcrow-1][bcsi] = 'X';
cout << "Displaying Updated Flight Seating Chart" << endl;
return display();
}
int ecrow;
string ecseat;
if(type == 3)
{
cout << "You selected Economy Class" << endl;
cout << "Please review available Economy Class Seats." << endl;
cout << " A " << "B " << "C " << "D " << "E " << "F " << endl;
for(int l = 7; l < rows; l++)
{
cout << "Row " << l+1 << " ";
for(int m = 0; m < seats; m++)
{
cout << flight[l][m] << " ";
}
cout << endl;
}
cout << "Please enter in the desired row (8-13)" << endl;
cin >> ecrow;
cout << "Please enter in the desired seat (A-F)" << endl;
cin >> ecseat;
cout << "You selected Row " << ecrow << " Seat " << ecseat << endl;
int ecsi;
if(ecseat == "A")
{
ecsi = 0;
}
if(ecseat == "B")
{
ecsi = 1;
}
if(ecseat == "C")
{
ecsi = 2;
}
if(ecseat == "D")
{
ecsi = 3;
}
if(ecseat == "E")
{
ecsi = 4;
}
if(ecseat == "F")
{
ecsi = 5;
}
flight[ecrow-1][ecsi] = 'X';
cout << "Displaying Updated Flight Seating Chart" << endl;
return display();
}
if(type != 1 && type != 2 && type !=3)
{
cout << "Error" << endl;
cout << "The Ticket Type Selected does not exist." << endl;
cout << "Exiting Program" << endl;
cout << ". . . . . . . ." << endl;
exit(13);
return(13);
}
}
//This function allows the user to cancel their seat selection.
int cancel()
{
int canrow;
string canseat;
cout << "We are sorry you wish to cancel your seat assignment." << endl;
cout << "Please enter the row of your seat (1-13)." << endl;
cin >> canrow;
cout << "Please enter the seat letter (A-F)." << endl;
cin >> canseat;
cout << "You selected Row " << canrow << " Seat " << canseat << endl;
int canseati;
if(canseat == "A")
{
canseati = 0;
}
if(canseat == "B")
{
canseati = 1;
}
if(canseat == "C")
{
canseati = 2;
}
if(canseat == "D")
{
canseati = 3;
}
if(canseat == "E")
{
canseati = 4;
}
if(canseat == "F")
{
canseati = 5;
}
flight[canrow-1][canseati] = '*';
return display();
}
//This function allows the user to exit the program.
int close()
{
cout << "Exiting Program" << endl;
cout << ". . . . . . . ." << endl;
exit(11);
return(11);
}
//This starts the main program/menu used to run the functions.
int main()
{
int z = 0;
do
{
//These for loops fill the flight array with '*' to show all seats as empty.
for(int a = 0; a < rows; a++)
{
for(int b = 0; b < seats; b++)
{
flight[a][b] = '*';
}
}
z = z+1;
}while (z = 0);
//This creates the LCV and sets it equal to 0 to run the do loop.
int x = 0;
do
{
//These four lines explain what the program does to the user.
cout << "Welcome to Flight Seat Assigner" << endl;
cout << "This program will allow you to select an available seat for your flight" << endl;
cout << "based on the ticket type and available seats." << endl;
cout << "You also can cancel a seat assignment or display the seating chart." << endl;
//This line tells the user to pick a menu option.
cout << "Please select from the following menu to begin." << endl;
//These lines tell the user what the menu options are.
cout << "1. Display Flight Seating Assignments" << endl;
cout << "2. Select a Seat" << endl;
cout << "3. Cancel a Seating Assignment" << endl;
cout << "4. Exit Program" << endl;
//This creates a variable to store the user's menu selection.
int menu = 0;
//This stores the user's selected menu option.
cin >> menu;
//This if statement runs if the user selects the first menu option.
if(menu == 1)
{
//This line runs the display function.
return display();
}
//This if statement runs if the user selects the second menu option.
if(menu == 2)
{
//This line runs the assign funciton.
return assign();
}
//This if statement runs if the user selects the third menu option.
if(menu == 3)
{
//This line runs the cancel function.
return cancel();
}
//This if statement runs if the user selects the fourth menu option.
if(menu == 4)
{
//This line runs the close function.
return close();
}
//This else statement runs if the user enters a non-menu option.
else
{
//These lines explain the user's meu selection mistake and closes the program.
cout << "Error" << endl;
cout << "The option selected does not exist." << endl;
cout << "Please enter a number between 1-4 when using the menu." << endl;
cout << "Closing Program" << endl;
cout << ". . . . . . . ." << endl;
exit (12);
return(12);
}
//This while statement controls when the do loop runs.
}while (x = 0 && x != 0);
}
这里是仅用于相关的do-while循环的代码。
do
{
//These four lines explain what the program does to the user.
cout << "Welcome to Flight Seat Assigner" << endl;
cout << "This program will allow you to select an available seat for your flight" << endl;
cout << "based on the ticket type and available seats." << endl;
cout << "You also can cancel a seat assignment or display the seating chart." << endl;
//This line tells the user to pick a menu option.
cout << "Please select from the following menu to begin." << endl;
//These lines tell the user what the menu options are.
cout << "1. Display Flight Seating Assignments" << endl;
cout << "2. Select a Seat" << endl;
cout << "3. Cancel a Seating Assignment" << endl;
cout << "4. Exit Program" << endl;
//This creates a variable to store the user's menu selection.
int menu = 0;
//This stores the user's selected menu option.
cin >> menu;
//This if statement runs if the user selects the first menu option.
if(menu == 1)
{
//This line runs the display function.
return display();
}
//This if statement runs if the user selects the second menu option.
if(menu == 2)
{
//This line runs the assign funciton.
return assign();
}
//This if statement runs if the user selects the third menu option.
if(menu == 3)
{
//This line runs the cancel function.
return cancel();
}
//This if statement runs if the user selects the fourth menu option.
if(menu == 4)
{
//This line runs the close function.
return close();
}
//This else statement runs if the user enters a non-menu option.
else
{
//These lines explain the user's meu selection mistake and closes the program.
cout << "Error" << endl;
cout << "The option selected does not exist." << endl;
cout << "Please enter a number between 1-4 when using the menu." << endl;
cout << "Closing Program" << endl;
cout << ". . . . . . . ." << endl;
exit (12);
return(12);
}
//This while statement controls when the do loop runs.
}while (x = 0 && x != 0);
对不起,代码很长,但是程序快要完成了。据我所知,我只需要修复循环而不会重启。
您正在循环条件中使用赋值运算符,而不是“ ==”,而您正在使用“ =。