2012-11-18 122 views
0

這可能看起來像一個簡單的問題,但我不知道它是什麼讓我的控制檯快速打開和關閉?我在main()函數中包含了系統(「PAUSE」)。C++控制檯問題

程序信息: 該程序適用於電影院票務系統,該系統顯示哪一行可用(如多維陣列所示)。

有人知道爲什麼控制檯不能打開嗎?我在編譯器中沒有收到任何錯誤消息。

#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; 
} 
+0

嘗試從命令行運行程序。 – Pubby

+1

你如何運行你的程序?我懷疑這與編程環境中的程序本身一樣多。 –

+0

@ Code-Guru - VC++ 2010編譯 – n0de

回答

2

因爲在你ReadPrices()函數,您無法打開prices.dat文件,然後直接出口(1)應用

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

如果您正在使用VisualStudio的,運行應用程序,CTL + F5的控制檯將停留。

瞭解如何調試您的應用程序非常重要,請仔細檢查每行代碼,並且您可以輕鬆地找到您的案例。

+0

哇,非常感謝。 – n0de

0

我相信exit()被ReadPrices調用。 exit()不會調用系統(「暫停」)。

可能的解決方法:

  • 的std :: at_exit()
  • 讓你ReadPrices函數返回一個布爾值標誌,如果它是成功的,而不是調用exit。