-1

好的,所以我的任务要我从用户那里获取有关公司 4 个部门的信息。当我运行并编译它时,它会提示 8 个分区(就像我说的,我只想要 4 个),当它在屏幕上显示收集到的信息时,它不会显示不同分区的名称。例子

#include<iostream>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include <vector>

using namespace std;

const int SIZE = 12;

struct Division
{
      char divName[SIZE]; // Division name
      double sales[4]; // Quarterly sales stored as an array
};



int main()
{
       void Intro();
       std::vector<Division> CreateCorporateFile();
       void DisplayCorporateSales();

       Intro();
       CreateCorporateFile();
       DisplayCorporateSales();


       system("PAUSE");
       return 0;
}

void Intro()
{
     cout<<"This program will prompt you to enter in quarterly sales for "
           "four different\ndivisions of a company.\n\n";
}


std::vector<Division> CreateCorporateFile()
{
    std::vector<Division> divArray;
    Division div;
    int x = 0;
    for(int x = 0; x < 4; x++)
    {
    /*do {*/int quarter = 1;
          cout << "Enter the name of the division: ";
          cin >> div.divName;
          for(int i = 0; i < 4; i++)
          {
               cout << "Enter in the sales for quarter "<< quarter <<": ";
               cin >> div.sales[i];
               if(div.sales[i] > 0)
               {
                    quarter++;
               }
               else
               {
                    cout << "Sales are not allowed to be negative.\n";
               }
          }

        divArray.push_back(div);
        /*x++;
    } while(x < 4);*/
}
    return divArray;
}




void DisplayCorporateSales()
{
     Division div;

         std::vector<Division> divisions = CreateCorporateFile();
    for (size_t i=0; i<divisions.size(); i++)
    {
        cout << "Here are the quarterly sales for " << div.divName
        << ": \n";
        Division div = divisions[i];
        for(int j = 0, quarter = 1; j < 4; ++j, ++quarter)
        {
            cout << "Quarter "<< quarter << " sales: $"<< div.sales[j]
            <<"\n";
        }
    }         
}
4

1 回答 1

3

它要求双倍的原因是因为您调用CreateCorporateFile了两次。

要么只在 中调用它main,保存返回的向量,然后将其传递给DisplayCorporateSales. 或者根本不调用它main

我还建议您使用std::string字符串(名称)而不是char数组。

于 2012-12-17T12:40:55.983 回答