2012-12-17 79 views
-1

好了麻煩,所以我assignment想讓我從用戶4組A公司的信息。當我運行和編譯它,它會提示8個師(就像我說的,我只是想4),也當它顯示聚集到屏幕上的信息並不爲不同的部門顯示的名稱。 Example後,提示輸入太多次與陣列輸出

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

回答

3

它要求雙究其原因,是因爲你叫CreateCorporateFile兩次。

要麼只在main中調用它,保存返回的向量,並將其傳遞給DisplayCorporateSales還是根本就不把它在main

我還建議您使用std::string作爲字符串(名稱)而不是char陣列。

+0

好吧,我一直在這個時間太長了,我不看到它。你能否指出我第二次把它叫做什麼,我已經搜遍了它,但什麼也沒有。 – Kobrien

+1

@Kobrien幾乎你'DisplayCorporateSales'做的第一件事。 –

+0

啊,謝謝。我一直在努力工作太久,只是想完成它。這確實解決了將Create創建爲Main的問題。但是現在輸出結果是[this](http://pastebin.com/EkXV8Vw4)我如何實現字符串而不是char。 – Kobrien