C++按enter键继续

C++ press enter to continue

本文关键字:继续 enter C++      更新时间:2023-10-16

晚上,我正在寻找一种方法,让程序继续运行,而不是在要求按enter键继续后退出。1我不能使用list命令,因为我在另一个函数中调用函数"seatingChart",而使用list命令会将我送回菜单。有什么建议吗?

void seatingChart()
{
    for(row = 0; SEATROWS > row; ++row) // Placeholder for '#'
    for (seat = 0; SEATS > seat; ++seat) // Placeholder for '#'
    theater[row][seat] = '#'; // Applying '#' to the chart
    cout << "nttSeats";
    cout << "n        123456789012345678901234567890" << endl; //seat header
    for (int row = 0; SEATROWS > row; ++row) 
    { // Initializing 15 rows
        cout << "nRow " << setw(2) << row+1 << "t";
        for (int seat = 0; SEATS > seat; ++seat)
        { // Initializing 30 seats
            cout << theater [row][seat];} //display seating chart
        }
        cout << "nnntLegend:t*  =  Sold";
        cout << "ntt#  =  Available";
        cout << "nnnPress the Enter key to continue.";
        cin.ignore();
        cin.get();
    }
}

下面的整个代码:当我在菜单上输入"1"以显示座位表时,我会遇到问题。

#include <iostream>
#include <iomanip>
using namespace std;
void list();
void getPrices();
void viewSales();
void seatingChart();
void ticketSales();
const int ROWS = 15;
const int COLS = 2;
double price[ROWS][COLS];
const int SEATROWS = 15; //PAT
const int SEATS = 30;//PAT
char theater[SEATROWS][SEATS];//PAT
int row;//PAT
int seat;//PAT
const char TAKEN = '*';//seats taken
const char EMPTY = '#';//seats free
int main()
{
    int x; // Loop counter
    for (x=0; x<ROWS;x++)
    {
        cout << "Please enter ticket price for Row " << setw(2) << (x + 1) << ": ";
        cin >> price[x][COLS];
    }
    list();
    return 0;
}
void list()
{
    int choice;
    cout << "nnnttC++ Theatre" << endl << endl;
    cout << "nt1.  View Available Seats";
    cout << "nt2.  View Seating Prices";
    cout << "nt3.  View Ticket Sales";
    cout << "nt4.  Purchase a Ticket";
    cout << "nt5.  Exit the Programnn";
    cout << "ntEnter your choice(1-5):  ";
    cin>>choice;
    while(choice>5 || choice<1)
    {
        cout<<"Choice must be between 1 and 5. Please re-enter:";
        cin>>choice;
    }
    if (choice == 1)
        seatingChart();
    else if (choice == 2)
        getPrices();
    else if (choice == 3)
        viewSales();
    else if (choice == 4)
        ticketSales();
}
void getPrices()
{
    cout<<"nTicket Prices By Row "<<endl;
    cout<<"      Row    Price"<<endl;
    cout<<"      ---    -----"<<endl;
    for (int x= 0; x < ROWS; x++)
    {
        cout<<setw(8)<<x+1<<setw(10);
        cout<<fixed<<showpoint<<setprecision(2)<<price[x][2]<<endl;
    }
    cout<<"nnnPress the Enter key to continue.";
    cin.ignore();
    cin.get();
    list();
}
void viewSales()
{
    double sum=0;
    cout<<"nnTotal Sales to Date: $"<<fixed<<showpoint<<setprecision(2)<<sum<<"nn";
    list();
}
void seatingChart()
{
    for(row = 0; SEATROWS > row; ++row) // Placeholder for '#'
        for (seat = 0; SEATS > seat; ++seat) // Placeholder for '#'
            theater[row][seat] = '#'; // Applying '#' to the chart
    cout << "nttSeats";
    cout << "n        123456789012345678901234567890" << endl; //seat header
    for (int row = 0; SEATROWS > row; ++row) { // Initializing 15 rows
        cout << "nRow " << setw(2) << row+1 << "t";
        for (int seat = 0; SEATS > seat; ++seat){ // Initializing 30 seats
            cout << theater [row][seat];} //display seating chart
    }
    cout << "nnntLegend:t*  =  Sold";
    cout << "ntt#  =  Available";
    cout << "nnnPress the Enter key to continue.";
    cin.ignore();
    cin.get();
}
void ticketSales()
{
    //*********************DISPLAY SEATING**********************************//
    int row;
    int seat;
    char showSeating;
    char anotherTicket = 'N';
    int tickets = 0;
    double totalPrice = 0;
    cout << "ntt        C++ Theatre" << endl;
    cout << "ttTicket Purchase Opportunity" << endl << endl;
    cout << "Do you wish to view the chart of available seats n"
    << "before making your selections (y/n)? ";
    cin  >> showSeating;

    if (toupper(showSeating) == 'Y')
        seatingChart();
    /*------------------Working display and working taken------------------------------*/
    do
    {
        cout << "nPlease enter desired row number (1-" << ROWS << "): ";
        cin >> row;
        while (row < 1 || row > ROWS)
        {
            cout << "Row must be between 1 and " << ROWS << ". Please re-enter: ";
            cin  >> row;
        }
        cout << "nPlease enter desired seat number (1-" << SEATS << "): ";
        cin >> seat;
        while (seat < 1 || seat > SEATS)
        {
            cout << "Seat must be between 1 and " << SEATS << ". Please re-enter: ";
            cin  >> seat;
        }
        row--; seat--; // row and seat indexing starts from 0
        if(theater[row][seat] == TAKEN)
        {
            cout << "This seat is taken! Try another one. n";
        }
        else{ // and if it is - sell the ticket
            theater[row][seat]==TAKEN;
            tickets++;
            // Need to update seating chart upon purchase
            totalPrice += price[row][COLS];
        }

        cout << "nWould you like to purchase another seat (y/n)? ";
        cin >> anotherTicket;

        anotherTicket = toupper(anotherTicket);
    }while (anotherTicket == 'Y');
    cout << "nnYou have purchased a total of " << tickets << " tickets " << "for a total price of $" << totalPrice;
    list();
}

除非您将整个菜单代码封装在一个循环中,并将此循环的退出条件设置为用户输入(在您的情况下,键入5表示退出),否则程序将在从seatingchart()函数返回时立即终止,因为list()函数将返回main(即seatingchart()返回list返回main())。你应该这样做:

do
{
   cout << "nnnttC++ Theatre" << endl << endl;
   cout << "nt1.  View Available Seats";
   cout << "nt2.  View Seating Prices";
   cout << "nt3.  View Ticket Sales";
   cout << "nt4.  Purchase a Ticket";
   cout << "nt5.  Exit the Programnn";
   cout << "ntEnter your choice(1-5):  ";
   cin>>choice;
   while(choice>5 || choice<1)
   {
      cout<<"Choice must be between 1 and 5. Please re-enter:";
      cin>>choice;
   }
   if (choice == 1)
      seatingChart();
   else if (choice == 2)
      getPrices();
   else if (choice == 3)
      viewSales();
   else if (choice == 4)
       ticketSales();
   else if (choice==5)//this is your exit condition
        break;//will break out of the menu loop
}while(1);//the is your menu loop

顺便说一下,您的代码中存在一些逻辑错误,请参阅注释部分中给出的注释并进行更正。