C ++按Enter键继续

时间:2016-04-08 06:52:23

标签: c++ c++11

晚上,我正在寻找一种让程序继续运行的方法,而不是在要求按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 << "\n\t\tSeats";
    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 << "\n\n\n\tLegend:\t*  =  Sold";
        cout << "\n\t\t#  =  Available";

        cout << "\n\n\nPress 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 << "\n\n\n\t\tC++ Theatre" << endl << endl;
    cout << "\n\t1.  View Available Seats";
    cout << "\n\t2.  View Seating Prices";
    cout << "\n\t3.  View Ticket Sales";
    cout << "\n\t4.  Purchase a Ticket";
    cout << "\n\t5.  Exit the Program\n\n";
    cout << "\n\tEnter 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<<"\n\n\nPress the Enter key to continue.";

    cin.ignore();
    cin.get();

    list();

}

void viewSales()
{
    double sum=0;

    cout<<"\n\nTotal Sales to Date: $"<<fixed<<showpoint<<setprecision(2)<<sum<<"\n\n";

    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 << "\n\t\tSeats";
    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 << "\n\n\n\tLegend:\t*  =  Sold";
    cout << "\n\t\t#  =  Available";

    cout << "\n\n\nPress 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 << "\n\t\t        C++ Theatre" << endl;
    cout << "\t\tTicket 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 << "\n\nYou have purchased a total of " << tickets << " tickets " << "for a total price of $" << totalPrice;
    list();
}

1 个答案:

答案 0 :(得分:1)

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

do
{
   cout << "\n\n\n\t\tC++ Theatre" << endl << endl;
   cout << "\n\t1.  View Available Seats";
   cout << "\n\t2.  View Seating Prices";
   cout << "\n\t3.  View Ticket Sales";
   cout << "\n\t4.  Purchase a Ticket";
   cout << "\n\t5.  Exit the Program\n\n";
   cout << "\n\tEnter 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

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