计数循环?

时间:2011-05-18 23:23:57

标签: c++

我正在创建一个座位计划,我很想知道是否有办法计算循环并将其放在变量中。我试图让用户知道他购买了多少张门票

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

3 个答案:

答案 0 :(得分:1)

如果您想计算购买的门票数量,您应该定义一个包含已购买门票数量的变量,并在another = true;之后立即增加。

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

答案 1 :(得分:0)

认为您正在寻找的是:

totSold += 1;
在将#分配到竞技场布局后立即

请注意,在再次出售之前,您应该检查座椅是否已经售出。 :)

答案 2 :(得分:0)

你确定你的while循环吗?

这样的事情会更加明显:

//...

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;

//...

但是,提供的代码中有很多奇怪的东西。但最重要的是继续练习,所以继续玩代码,那些奇怪的东西会像经验中的魔法一样消失;)