繁体   English   中英

无论我用作输入,输出全为零

[英]whatever i use as inputs, outputs are all zeros

这是我正在研究的一个问题:

  • 提示用户输入五个数字,即五个人的体重。 将数字存储在双精度向量中。 在一行上输出矢量的数字,每个数字后跟一个空格。
  • 通过对向量的元素求和,还输出总权重。

  • 还输出向量元素的平均值。

  • 同时输出最大向量元素。 到目前为止,这是我拥有的代码

     #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; 在程序开始时。但是输出仍然不完全是应该的。 取而代之的是,只有前两个输入值将其作为输出。 有什么原因吗? 谢谢

更新这是正确或正确的代码。 希望以后能对某人有所帮助。

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

您正在制作大小为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++)填充代码。 有一种更简单的方法。


// 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 << "You entered: ";
for (const auto& weight : weights) {
  cout << weight << " ";
}

您还需要删除cout << inputWeights[i] << endl; 如果在输入过程中调整矢量的大小,则从输入循环中移出一行-如所写,您将读取尚不存在的元素,并且可能会得到array-index-out-of-bounds异常。

创建定义输入权重时,您将使用默认值将5个项目放入其中。

vector<float> inputWeights(NEW_WEIGHT);

改变它只是

vector<float> inputWeights;

并摆脱代码中的这一行或将其注释掉

cout << inputWeights[i]<< endl; 

这是您从程序需求中寻找的东西。

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

然后,当遍历容器时更容易使用inputWeights.size()时,您将使用NEW_WEIGHT循环访问多达5个元素。

编辑-压缩版

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM