C++按enter键继续
C++ press enter to continue
晚上,我正在寻找一种方法,让程序继续运行,而不是在要求按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
顺便说一下,您的代码中存在一些逻辑错误,请参阅注释部分中给出的注释并进行更正。
相关文章:
- 循环后如何继续阅读
- 我正在尝试使用 c++ 创建一个货币转换程序,我不知道如何继续
- C++:继续创建新的变量可以吗
- Alt+Enter 在 Win32 应用中,管理大小调整和分辨率
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 如何继续读取不同功能中的文件,而不是从头开始再次读取文件?
- 在 c++ 中三元运算符中不允许继续(关键字)吗?
- 修复完成输入后重复"enter names of teacher"?
- C++ 如何暂停控制台输出,以便用户可以在继续之前阅读信息
- 在 wxWidgets 中的 wxTextCtrl 上手动触发 ENTER 键按下事件C++?
- 我如何使它,无论用户用空白字符串按 Enter 多少次,它总是打印"开始"字符串?
- 循环不继续无法弄清楚为什么
- 附加到 gdb 会中断并且不会继续该过程
- C++ 在信号处理程序后继续执行
- 为什么我的Qt程序在断言失败后继续运行?
- 如何打印下一个字母直到 Z 并继续到 A?
- 超时后,线程睡眠不会继续执行
- 使用Enter继续前进并ESC退出C
- C++按enter键继续
- 辛:等<ENTER>着。两个继续 cin.忽略不起作用