2016-12-23 58 views
0

這是一個問題,我的工作:無論我作爲輸入使用,輸出全零

  • 提示用戶輸入五個數字,是五個人的權重。將數字存儲在雙打的矢量中。在一行中輸出矢量的數字,每個數字後跟一個空格。
  • 通過對矢量的元素求和,還輸出總權重。

  • 還輸出矢量元素的平均值。

  • 還輸出最大向量元素。 到目前爲止,這是我的代碼有

    #include <iostream> 
    #include <vector> 
    using namespace std; 
    
    int main() { 
        const int NEW_WEIGHT = 5; 
        vector<float> inputWeights(NEW_WEIGHT); 
        int i = 0; 
        float sumWeight = 0.0; 
        float AverageWeight = 1.0; 
        int maxWeight = 0; 
        int temp = 0; 
    
        for (i = 0; i < NEW_WEIGHT; i++){ 
         cout << "Enter weight "<< i+1<< ": "; 
         cout << inputWeights[i]<< endl; 
         cin>> temp; 
         inputWeights.push_back (temp); 
    
        } 
    
    
        cout << "\nYou entered: "; 
    
        for (i =0; i < NEW_WEIGHT- 1; i++) { 
    
         cout << inputWeights.at(i)<< " "; 
        } 
        cout<< inputWeights.at(inputWeights.size() - 1) << endl; 
    
        for (i =0; i < NEW_WEIGHT; i++){ 
    
         sumWeight += inputWeights.at(i); 
    
        } 
        cout <<"Total weight: "<< sumWeight<< endl; 
    
        AverageWeight = sumWeight/inputWeights.size(); 
    
        cout <<"Average weight: "<< AverageWeight<< endl; 
    
        maxWeight= inputWeights.at(0); 
        for (i =0; i < NEW_WEIGHT- 1; i++){ 
         if (inputWeights.at(i) > maxWeight){ 
          maxWeight = inputWeights.at(i); 
         } 
        } 
    
        cout<< "Max weight: "<< maxWeight << endl; 
    
        return 0; 
    } 
    

當我運行這段代碼,我使用什麼輸入(CIN將>>(...)),我得到全零作爲輸出和我不知道爲什麼。請給我一些幫助。

更新 清理代碼一點通過擺脫COUT < < inputWeights [I] < < ENDL的;
並通過調整向量inputWeights;在程序的開始。但輸出仍然不完全是他們應該是。相反,只有前2個輸入值才能作爲輸出。任何理由?謝謝

更新這是正確的或正確的代碼。希望它能幫助未來的人。

#include <iostream> 
#include <vector> 

using namespace std; 

int main() { 
    const int NEW_WEIGHT = 5; 
    vector <float> inputWeights; 
    int i = 0; 
    float sumWeight = 0.0; 
    float AverageWeight = 1.0; 
    float maxWeight = 0.0; 
    float temp = 0.0; 

    for (i = 0; i < NEW_WEIGHT; i++){ 
     cout << "Enter weight "<< i+1<< ": "<< endl; 
     cin>> temp; 
     inputWeights.push_back (temp); 

     } 


     cout << "\nYou entered: "; 

    for (i =0; i < NEW_WEIGHT- 1; i++){ 

     cout << inputWeights.at(i)<< " "; 
    } 
     cout<< inputWeights.at(inputWeights.size() - 1) << endl; 

    for (i =0; i < NEW_WEIGHT; i++){ 

     sumWeight += inputWeights.at(i); 

     } 
    cout <<"Total weight: "<< sumWeight<< endl; 

     AverageWeight = sumWeight/inputWeights.size(); 

    cout <<"Average weight: "<< AverageWeight<< endl; 

     maxWeight= inputWeights.at(0); 
    for (i =0; i < NEW_WEIGHT- 1; i++){ 
     if (inputWeights.at(i) > maxWeight){ 
      maxWeight = inputWeights.at(i); 
      } 
    } 

    cout<< "Max weight: "<< maxWeight << endl; 

    return 0; 
} 
+2

提示:怎麼看待的push_back()的作品 – LWimsey

+0

@LWimsey其追加無論是在臨時的載體inputWeights。 – TINA15

+1

,所以你的矢量從5增加到10,因爲你用5個零來初始化它,而那些仍然存在來自所有反饋的 – LWimsey

回答

2

你正在做一個大小爲5的vector

const int NEW_WEIGHT = 5; 
vector<float> inputWeights(NEW_WEIGHT); 

// == 0, 0, 0, 0, 0 

然後,在你輸入迴路,您要添加新的值到最後:

inputWeights.push_back (42); 

// == 0, 0, 0, 0, 0, 42 

然後你輸出始終爲零的前五個元素。

您需要選擇一個或另一個:或者在程序開始時設置矢量的大小,或者在有輸入的情況下用push_back增加矢量。兩者都是有效的選項。

您可以通過採用現代C++(如C++ 11及更高版本)習慣用法來清理代碼並修復問題。您無需再使用for(int i = 0; i < something; i++)填寫代碼。有一個更簡單的方法。

cout << "You entered: "; 
for (const auto& weight : weights) { 
    cout << weight << " "; 
} 

您還需要:


// Size fixed in advance: 
vector<float> weights(NUM_WEIGHTS); 

for (auto& weight : weights) { // note it's `auto&` 
    cout << "\nEnter next weight: "; 
    cin >> weight; // if it was plain `auto` you'd overwrite a copy of an element of `weight` 
} 

// Size decided by input: 
vector<float> weights; // starts empty this time 

cout << "Enter weights. Enter negative value to stop." << endl; 
float in; 
while (cin >> in) { 
    if(in < 0) { 
    break; 
    } 
    weights.push_back(in); 
} 

在這兩種情況下,你就可以用填補載體使用其他基於範圍for玩從輸入循環中刪除cout << inputWeights[i] << endl;行您在輸入過程中調整了矢量的大小 - 正如您所讀到的那樣,您將讀取尚不存在的元素,並且可能會得到數組索引超出範圍的異常。

+0

@SergeyA你是對的,需要在輸入循環開始時敲擊'cout << inputWeights [i]'行。 –

+0

從所有的反饋,我想問題是在「提示用戶輸入」的水平。對? – TINA15

1

當您創建定義您的inputWeights時,您將使用默認值將5個項目放入其中。

vector<float> inputWeights(NEW_WEIGHT); 

改變它只是

vector<float> inputWeights; 

,擺脫這一行的在你的代碼或將其註釋掉

cout << inputWeights[i]<< endl; 
+0

哦,好的!謝謝我現在有更好的產出。但它仍然不完全是它想要的。 – TINA15

0

這是你從的要求找什麼你的程序。

#include <vector> 
#include <iostream> 

int main() { 
    std::vector<double> weights; 

    double currentWeight = 0.0; 
    const unsigned numberOfWeights = 5; 
    std::cout << "Enter " << numberOfWeights << " weights" << std::endl; 
    unsigned i = 0; 
    for (; i < numberOfWeights; ++i) { 
     std::cin >> currentWeight; 
     weights.push_back(currentWeight); 
    } 

    std::cout << "These are the weights that you entered: " << std::endl; 
    for (i = 0; i < weights.size(); ++i) { 
     std::cout << weights[i] << " "; 
    } 
    std::cout << std::endl; 

    double totalWeight = 0.0; 
    std::cout << "The total of all weights is: "; 
    for (i = 0; i < weights.size(); ++i) { 
     totalWeight += weights[i]; 
    } 
    std::cout << totalWeight << std::endl; 

    std::cout << "The average of all the weights is: " << (totalWeight/numberOfWeights) << std::endl; 

    std::cout << "The max weight is: "; 
    double max = weights[0]; 
    for (i = 0; i < weights.size(); ++i) { 
     if (weights[i] > max) { 
      max = weights[i]; 
     } 
    } 
    std::cout << max << std::endl; 

    return 0; 
} 

罪魁禍首您的問題,爲看到全0作爲輸出是從以下兩行代碼來:

const int NEW_WEIGHT = 5; 
vector<float> inputWeights(NEW_WEIGHT); 

這是一樣的這樣做:

vector<float> inputWeights{ 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; 

你然後使用NEW_WEIGHT循環多達5個元素,當遍歷容器時使用inputWeights.size()會更容易。

編輯 - 濃縮版

#include <vector> 
#include <iostream> 

int main() { 
    std::vector<double> weights; 

    double currentWeight   = 0.0; 
    const unsigned numberOfWeights = 5; 
    unsigned i = 0; 

    std::cout << "Enter " << numberOfWeights << " weights" << std::endl; 
    for (; i < numberOfWeights; ++i) { 
     std::cin >> currentWeight; 
     weights.push_back(currentWeight); 
    } 

    double totalWeight = 0.0; 
    double max   = weights[0]; 
    std::cout << "These are the weights that you entered: " << std::endl; 
    for (i = 0; i < weights.size(); ++i) { 
     std::cout << weights[i] << " "; // Print Each Weight 
     totalWeight += weights[i];  // Sum The Weights 

     // Look For Max Weight 
     if (weights[i] > max) { 
      max = weights[i]; 
     } 
    } 
    std::cout << std::endl; 

    std::cout << "The total of all weights is: " << totalWeight << std::endl; 
    std::cout << "The average of all the weights is: " << (totalWeight/numberOfWeights) << std::endl; 
    std::cout << "The max weight is: " << max << std::endl; 

    return 0; 
} 
+1

謝謝大家。我沒有看到我的Temp變量是int類型而不是double類型。改變給了我什麼作爲輸出。 @Francis Cuglar我很感激。 – TINA15

+0

@ TINA15我通過在'const unsigned'中添加要添加的權重的數量,對程序進行了修改。這樣,所有你需要做的就是改變這個數字,完整的算法或程序工作,不管有多少輸入是可以預期的。所有的循環和計算將基於無符號的'numberOfWeights'。 –

+0

我上面顯示的內容可以縮小爲循環所做的更少,但我選擇這樣做來清楚地顯示每一步。我可以很容易地在2個單獨的for循環中完成它,第一個循環填充矢量,第二個for循環打印矢量的所有元素並計算所有值的總和並找到最大值。平均值不需要任何循環。 –

相關問題