2011-05-18 62 views
0

我正在創建一個座位程序,我想知道是否有一種方法來計算循環並將其放入一個變量中。我嘗試讓用戶知道他多少張門票購買計數循環?

#include <iostream> 
#include <iomanip> 
#include <string> 
#include <istream> 
#include <fstream> 
using namespace std; 

const int numberOfRow = 15; 
const int numberOfCol = 20; 
void print(char matrix[][20],int numberOfRow, int numberOfCol); 


int main() 
{ 
    ifstream datafile; 
    int i, j; 
    char matrix[numberOfRow][numberOfCol], seat[numberOfRow][numberOfCol]; 
    char option; 
    int row, col, totalsold; 
    float totSold, temp,price = 0 , ticketprice[numberOfRow], totRevenue; 
    bool another = true; 
    string filename; 
    datafile.open("c:\\price.dat"); 

    for(i=0;i<numberOfRow;++i) 
    { 
     datafile >> temp; 
     ticketprice[i]=temp; 
     cout<< "Row "; 
     cout<< setw(2)<< fixed << setprecision(2)<< i << setw(7) << ticketprice[i]<< endl; 
    } 

    for(i = 0; i< numberOfRow; i++) 
     for(j = 0; j< numberOfCol; j++) 
      matrix[i][j] = '*'; 

    print(matrix,numberOfRow, numberOfCol); 

    while(another) 
    { 
     totalsold = 0; 
     totRevenue = 0; 
     cout << "Please enter the row you would like to sit in: " << endl; 
     cin >> row; 
     cout << "Please enter the column you would like to sit in: " << endl; 
     cin >> col; 
     cout << "would you like to purchase more tickets? <y,n>" << endl; 
     cin >> option; 
     matrix[row][col] = '#'; 
     /*totRevenue = totRevenue + ticketprice[row];*/ 


     if(option == 'y' || option == 'Y') 
     { 
      another = true; 
     } 

     else 
     { 
      another = false; 
      print(matrix,numberOfRow, numberOfCol); 
      totRevenue = totRevenue + ticketprice[row]; 
     } 
    } 

    totRevenue = totRevenue + ticketprice[row]; 
    cout << "Total Tickets Sold: " << endl;// << totSold << endl; 
    cout << "Total Revenue: $ " << fixed << setprecision(2)<< totRevenue<< endl; 
    cin >> i; 
    cin.get(); 
    return 0; 

} 
void print(char matrix[][20],int numberOfRow, int numberOfCol) 
{ 
    int row, col, i, j; 

    cout << "seat: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19"<< endl; 
    for(i = 0; i < numberOfRow; i++) 
    { 
     cout << "row" << setw(3)<< i; 
     for(j = 0; numberOfCol > j; j++) 
      cout << setw(3) << matrix[i][j]; 

     cout << endl; 
    } 
} 
+1

計算哪個循環? – 2011-05-18 23:26:05

+0

我不認爲普通的非程序員會理解「請輸入你想要的列:」! – TonyK 2011-05-18 23:26:48

+0

「計數循環」?請解釋。 – wilhelmtell 2011-05-18 23:26:57

回答

0

你確定你的while循環嗎?

這樣的事情,會更有SENS:

//... 

int totalsold = 0; 
float totRevenue = 0.0; 

while(another) 
{ 
    cout << "Please enter the row you would like to sit in: " << endl; 
    cin >> row; 
    cout << "Please enter the column you would like to sit in: " << endl; 
    cin >> col; 
    cout << "would you like to purchase more tickets? <y,n>" << endl; 
    cin >> option; 
    matrix[row][col] = '#'; 

    ++totalsold; // increment the number of tickets sold 
    totRevenue += ticketprice[row]; // increment to total price of the tickets 

    if(option == 'n' || option == 'N') 
    { 
     another = false; // exit the loop 
    } 
} 

print(matrix,numberOfRow, numberOfCol); 
cout << "Total Tickets Sold: " << totalsold << endl; 
cout << "Total Revenue: $ " << fixed << setprecision(2)<< totRevenue<< endl; 

//... 

然而,有很多奇怪的事情中所提供的代碼。但最重要的是要繼續練習,所以繼續玩代碼,那些奇怪的東西就會像魔術一般消失;)

+0

非常感謝! – 2011-05-18 23:58:58

1

如果要算上門票數量買來的,你應該定義包含買了票的數量變量和another = true;之後加一。

if(option == 'y' || option == 'Y') 
{ 
    another = true; 
    ++totSold; 
} 
0

認爲你要找的是什麼樣的一個行:分配#競技場佈局後立即

totSold += 1; 

請注意,在再次出售之前,您應該檢查座位是否已售出。 :)