0

これは簡単な質問のように思えるかもしれませんが、コンソールがすばやく開いたり閉じたりする原因が何であるかわかりませんか? 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;
 }
4

2 に答える 2

2

ReadPrices() 関数では、prices.dat ファイルを開くことができず、単に exit(1) アプリケーションを終了するためです。

  indata.open("prices.dat"); 
  if(!indata) 
  { 
    cerr << "Error: file could not be opened" << endl;
    exit(1);
  }

VisualStudio を使用している場合は、アプリケーションを実行し、CTL + F5 コンソールが残ります。

アプリケーションをデバッグする方法を学ぶことは非常に重要です。コードの各行をステップ実行すると、ケースの問題を簡単に見つけることができます。

于 2012-11-18T01:48:02.413 に答える
0

exit() は ReadPrices によって呼び出されると思います。exit() は system("pause") を呼び出しません。

考えられる回避策:

  • std::at_exit()
  • exit を呼び出す代わりに、ReadPrices 関数が成功した場合にフラグを立てるブール値を返すようにします。
于 2012-11-18T01:48:58.567 に答える