用于存储数据的结构和外部二进制文件

时间:2012-12-17 10:37:58

标签: c++

好吧,我有一个assignment,我已经被完全难倒了。到目前为止我的代码如下

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

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();
       Division CreateCorporateFile();
       Division 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";
}


Division CreateCorporateFile()
{
     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";
               }
          }
          x++;
     } while(x < 4);

     return div;
}


Division DisplayCorporateSales()
{
         Division test;

         Division CreateCorporateFile();

         test = CreateCorporateFile();

         for(int i = 0; i < 4; i++)
         {
             cout << "Here are the quarterly sales for " << test.divName
              << ": ";
             for(int i = 0, quarter = 1; i < 4; ++i, ++quarter)
             {
                  cout << "Quarter "<< quarter << " sales: $"<< test.sales[i]
                   <<"\n";
             }
         }

         return test;

}

我遇到的一个问题是它最终会提示8个分区(应该只执行4次),另一个是它最终只显示第8个分区数据。所以有人看到我哪里出错了?因为我没有,而且我已经工作了几个小时,所以我可能会忽略明显的细节。

2 个答案:

答案 0 :(得分:1)

您仅显示最终Division的数据的原因是CreateCorporateFile仅返回单个Division实例。每个do-while循环都会覆盖前一个循环的内容。

对此的一个解决方案是返回数组

std::vector<Division> CreateCorporateFile()
{
    std::vector<Division> divArray;
    Division div;
    int x = 0;
    do {
        ...
        divArray.push_back(div);
        x++;
    } while(x < 4);

    return divArray;
}

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

答案 1 :(得分:0)

第一个问题是由于您声明要在main中使用的函数而不是在全局范围之前使用的函数:        void Intro();        Division CreateCorporateFile();        Division DisplayCorporateSales();

第二个问题是由于您为每个循环使用了两次变量i。您需要使用不同的变量名称(或者只使用quarter作为循环变量)。