2012-11-18 2 views
0

간단한 질문처럼 보일 수도 있습니다. 내 콘솔을 신속하게 열고 닫을 수있는 것이 무엇인지 알 수 없습니까? 내 main() 함수에 시스템 ("PAUSE")을 포함 시켰습니다.C++ 콘솔 문제

프로그램 정보 : 이 프로그램은 다차원 배열에서 볼 수있는 것처럼 어떤 행의 좌석이 사용 가능한지를 보여주는 시네마 극장 티켓 시스템 용 프로그램입니다.

콘솔이 열리지 않는 이유를 아는 사람이 있습니까? 컴파일러에 오류 메시지가 표시되지 않습니다. 때문에 귀하의 ReadPrices에

#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

() 함수를 사용하면 prices.dat 파일을 단순히 종료 (1) 응용 프로그램을 열 수 없습니다 당신으로 VisualStudio 실행 응용 프로그램의 CTL + F5를 사용하는 경우

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

콘솔이 유지됩니다.

응용 프로그램을 디버깅하는 방법을 배우는 것이 매우 중요하므로 각 코드 행을 단계별로 실행하여 문제를 쉽게 찾을 수 있습니다.

+0

와우, 고맙습니다. – n0de

0

나는 exit()가 ReadPrices에 의해 호출된다고 생각합니다. exit()가 시스템을 호출하지 않습니다 ("일시 중지").

가능한 해결 방법 :

  • 표준 : at_exit()는
  • 는 성공적인 경우 귀하의 ReadPrices 기능을 대신 출구를 호출, 플래그 부울을 반환합니다.