动态数组元素计数器用于循环故障

时间:2014-11-27 03:21:09

标签: c++ arrays dynamic

您好我试图使用for循环和计数器将一些数据存储到动态数组中,以便将一堆数据放入一个元素中,如下所示:

int arry;

cout << "how many Revenue tiers do you want?: "; cin >> arry;
Revenue*  rev = new Revenue[arry];//dynamic array
for (int i = 0, Track_Num_Divisions = 1;Track_Num_Divisions, i  < arry; i++,Track_Num_Divisions++ )
{
    cout << " " << endl;
    cout << "Revenue #"<<Track_Num_Divisions << endl;
    cout << "===========" << endl;
    cout << "<< Ok what is your division name?: " << endl; cin >> D;
    string set_Division_name(D);
    cout << "<< What is your division number?: " << endl;  cin >> DN;
    string set_Division_number(DN);
    while (DN.size() != 4)
    {
        cout << "<< Sorry! Your Division Number cannot exceed or be short of 4. " << endl; cin >> DN;
    }
    cout << "<< What is your number of employees?: " << endl; cin >> N;

    //This is where the error is:
    Revenue*  rev = new Revenue[i].Set_Number_employee(N);

    cout << "<< What is the Total sales?: " << endl; cin >> TS;
    double Set_Total_sales(TS);
    cout << "<< What is the total cost?: " << endl; cin >> TC;
    double Set_Total_cost(TC);
    cout << "<< What is the total Profit?: " << endl; cin >> P;
    double Set_Profit(P);

    //and here :
     cout << "<< The total cost per employee is: " << Revenue* rev = new Revenue[i].Get_CPE() << endl;
}

我该如何解决这些问题。这两个都说我需要一个班级类型。

2 个答案:

答案 0 :(得分:0)

以下可能会有所帮助(假设类Revenue具有给定的方法):

int arry;

cout << "how many Revenue tiers do you want?: "; cin >> arry;
Revenue* revs = new Revenue[arry];//dynamic array
for (int i = 0, Track_Num_Divisions = 1; i < arry; i++, Track_Num_Divisions++)
{
    Revenue& rev = *revs[i];
    cout << " " << endl;
    cout << "Revenue #"<<Track_Num_Divisions << endl;
    cout << "===========" << endl;
    cout << "<< Ok what is your division name?: " << endl; cin >> D;
    rev.set_Division_name(D);
    cout << "<< What is your division number?: " << endl;  cin >> DN;
    while (DN != 4)
    {
        cout << "<< Sorry! Your Division Number cannot exceed or be short of 4. " << endl; cin >> DN;
    }
    rev.set_Division_number(DN);
    cout << "<< What is your number of employees?: " << endl; cin >> N;
    rev.Set_Number_employee(N);

    cout << "<< What is the Total sales?: " << endl; cin >> TS;
    rev.Set_Total_sales(TS);
    cout << "<< What is the total cost?: " << endl; cin >> TC;
    rev.Set_Total_cost(TC);
    cout << "<< What is the total Profit?: " << endl; cin >> P;
    rev.Set_Profit(P);

    //and here :
     cout << "<< The total cost per employee is: " << rev.Get_CPE() << endl;
}
delete [] revs; // free memory.

答案 1 :(得分:0)

原来我试图在制作动态数组时不必要地创建对象。我必须添加的是rev[i].Set_Profit(P);并删除此Revenue& rev = *revs[i];