无论我使用什么作为输入,输出都是零
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来清理代码;
并通过调整矢量输入权重;在程序开始时。但输出仍然不完全是它们应该的样子。相反,只有前 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;
}
- 字符的输入/输出操作
- C++ 中文件输入输出中的长长 v/s int
- 要求用户输入C++输出文件名
- 在C++中重载输入/输出运算符
- 从用户获取输入/输出
- 如何选择waveIn/waveOut函数C++使用的输入/输出设备
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- 从 2 位输入输出 4 位数字年份
- 标准输入输出操作如何C++完成
- Windows 中应用程序的每秒输入/输出操作数
- 如何初始化可以设置为默认数组并将输入/输出作为数组的类函数?
- 输入/输出和类
- 使用 Octave GNU 访问输入/输出文件
- (C++)分解这个简单的输入/输出代码:
- TensorFlow模型:如何从原始爱好文件中识别输入/输出节点名称
- 干净取消在输入/输出调用中被阻止的 std::线程
- 将线程之间的标准输入/输出重定向
- 输入/输出异或字符串的 C++ 问题
- C++输入/输出错误
- C++输入输出