C++ 为什么赢了';t my Do while loop work在C++;

C++ 为什么赢了';t my Do while loop work在C++;,c++,function,loops,menu,do-while,C++,Function,Loops,Menu,Do While,我知道我可能遗漏了一些明显的东西,但我的代码只会在主函数中运行do循环一次。运行所选功能(带有菜单选择)后,程序退出,而不是重新启动循环。我不确定我的循环出了什么问题。请告知 另外,这个程序是用C语言编写的++ 这是整个程序的主体。除主函数中的do循环外,所有函数都工作 //It also includes functions to cancel a selection and to show all seats on the flight. //These allow the predefi

我知道我可能遗漏了一些明显的东西,但我的代码只会在主函数中运行do循环一次。运行所选功能(带有菜单选择)后,程序退出,而不是重新启动循环。我不确定我的循环出了什么问题。请告知

另外,这个程序是用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);


}
//它还包括取消选择和显示航班上所有座位的功能。
//这些允许程序中的预定义功能运行。
#包括
#包括
//这将整个程序的名称空间设置为std
使用名称空间std;
//这将为航班上的座椅行创建一个常量变量。
const int rows=13;
//这将为航班上每一排的座位创建一个常量变量。
const int seats=6;
//此行创建用于存储座位分配的数组。
航班[前排][座位];
//此功能显示航班的座位表
int显示()
{

coutBessietechow的评论让我找到了该程序的正确解决方案。我将函数更改为无效,并删除了大部分返回。现在该程序运行良好。感谢大家的帮助。非常感谢这个编程noob。

这里的错误很明显,但将来请做一个。这是m表示您需要发布一个完整的程序,该程序也不包含任何不相关的代码。您发布的第二个代码块不是一个,因为它不是完整的程序,并且包含一些不相关的内容,例如打印菜单的代码。您似乎对
return
的功能有一个根本性的误解。为什么所有的函数都要重新执行n
int
何时应返回
void
?您声明了
display
函数以返回
int
,但您从不返回任何导致未定义行为的内容。还可以启用编译器警告。您在条件错误中有一个常见的赋值,好的编译器可以对此进行警告。
}而(x=0&&x!=0)你在这里的意图是什么?因为这毫无意义。如果我明白你想做什么,我会提供一个建议,但目前我不知道。一个问题(在许多问题中)是,如果你不想退出函数,那么就不要使用return<代码>返回显示()
返回赋值()将导致
main
函数退出,程序结束。很明显,您不理解
return
,如果您只是从代码中删除
return
,您似乎更接近正确的程序。
exit(12);返回(12)
你意识到
退出
退出你的程序了吗?因此,在调用
exit
之后再使用任何代码是绝对没有意义的。

    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);