C++控制台问题
C++ console issue
这似乎是一个简单的问题,但我只是无法弄清楚是什么让我的控制台快速打开和关闭?我在我的main()函数中包含system("PAUSE")。
程序信息:该程序适用于电影院票务系统,该系统显示哪一排的哪些座位可用(如您在多维数组中看到的那样)。
有人知道为什么控制台不会保持打开状态吗?我在编译器中没有收到任何错误消息。
#include <iostream>
#include <fstream>
using namespace std;
using std::ifstream;
void Init();
void Display();
void SellTicket();
void ReadPrices();
char tickets[15][20];
int revenue = 0;
int ticketsSold = 0;
int prices[15];
int main()
{
Init();
ReadPrices();
int choice;
cout << "Enter your choice: " << endl;
cout << "Press 1 for Display Chart" << endl;
cout << "Press 2 for sell ticket" << endl;
cout << "Press 3 for exit" << endl;
cin >> choice;
cout << endl;
switch(choice)
{
case 1:
Display();
break;
case 2:
SellTicket();
break;
case 3:
exit(0);
break;
}
system("PAUSE");
return 0;
}
void Init()
{
for (int row = 0; row < 15; row++)
{
for (int col = 0; col < 20; col++)
{
tickets[row][col]='*';
}
}
}
void Display()
{
cout <<"Seats:t0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19"<<endl;
for (int row = 0; row < 15; row++)
{
cout << "Row"<< row << "t";
for (int col = 0; col < 20; col++)
{
if(col < 10)
cout << tickets[row][col] << " ";
else
cout << tickets[row][col] << " ";
}
cout << endl;
cout << endl;
}
cout << "Total sold seats are: " << ticketsSold << endl;
cout << "Total revenue is: " << revenue << endl;
cout << endl;
}
void SellTicket()
{
int rowNo,seatNo;
//while(1)
//{
cout << "Enter Row Number:";
cin >> rowNo;
cout << endl;
cout << "Enter Seat Number:";
cin >> seatNo;
cout << endl;
if (tickets[rowNo][seatNo]=='#')
{
cout << "Ticket is not available " << endl;
cout << endl;
SellTicket();
}
else
{
tickets[rowNo][seatNo]='#';
revenue+=prices[rowNo];
ticketsSold+=1;
char c;
cout << "Would you like to sell another ticket? Press y for yes or n for no: ";
cin >> c;
cout << endl;
if (c=='y')
{
SellTicket();
}
}
//}
}
void ReadPrices()
{
int count=0;
ifstream indata;
int num;
indata.open("prices.dat");
if(!indata)
{
cerr << "Error: file could not be opened" << endl;
exit(1);
}
indata >> num;
while ( !indata.eof() )
{
prices[count++]=num;
//cout<< "The next number is " << num << endl;
indata >> num;
}
indata.close();
//cout << "End-of-file reached.." << endl;
}
因为在 ReadPrices() 函数中,您无法打开 price.dat 文件并简单地退出(1) 应用程序
indata.open("prices.dat");
if(!indata)
{
cerr << "Error: file could not be opened" << endl;
exit(1);
}
如果您使用的是VisualStudio,请运行应用程序,CTL + F5控制台将保留。
了解如何调试应用程序非常重要,逐步完成每一行代码,您可以轻松找到适合您的案例的问题。
我相信exit()是由ReadPrices调用的。exit() 不调用 system("pause")。
可能的解决方法:
- 标准::at_exit()
- 让你的 ReadPrices 函数返回一个布尔值来标记它是否成功,而不是调用 exit。
相关文章:
- C++出现控制台错误.我无法识别源代码的问题
- C++无法解决我的问题。我有控制台错误
- 在 win32 控制台项目 VS2015 中静态链接 libcurl 的问题
- 分配控制台问题
- C++控制台问题
- C 浮点控制台输出问题
- 在微控制器上编写 UART 控制台时出现 UTF-8 问题
- C++(DLL)与C#控制台应用程序的交互问题
- 这个C++代码有什么问题?(控制台)
- C++控制台"屏幕捕获"问题
- 控制台输出C++问题
- 使用控制台的可视C++:while 循环的字符/字符串兼容性问题
- 简单的C++控制台 I/O 问题
- 将wchar_t打印到控制台时存在内存泄漏问题
- fopen和windows控制台的问题
- 我在编写控制台应用程序以创建一个简单的程序来解决养老金的数学方程时遇到问题
- Eclipse CDT 无法构建 - 不确定出了什么问题(包括控制台)
- 附件控制台的问题
- 简单的输入/输出控制台应用程序,不按给定的顺序(结构)打印问题
- 使用Boost::Regex的问题(控制台只是冻结)