无论我使用什么作为输入,输出都是零

whatever i use as inputs, outputs are all zeros

本文关键字:输入 输出 什么      更新时间:2023-10-16

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

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

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

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

    #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来清理代码;
并通过调整矢量输入权重;在程序开始时。但输出仍然不完全是它们应该的样子。相反,只有前 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;
}

您正在制作大小为 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;行 - 正如所写的那样,您将读取尚不存在的元素,并且可能会得到数组索引越界异常。

当您创建定义输入权重时,您将使用默认值将 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 };

然后,您可以使用NEW_WEIGHT遍历容器时更容易使用inputWeights.size(),从而循环访问多达 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;
}