[英]I keep getting zeros when after my original inputs
so im creating a vector with unlimted inputs but i want it so that when the user wants to stop inputing data, they just enter a non numerical key and then just move on to the next vector which is just the first one but reversed and multipled by 8 and 100 added to it but when it should move on, it just adds a bunch of zeros as inputs 所以我用一个没有限制的输入创建一个矢量,但是我想要它,以便当用户想要停止输入数据时,他们只是输入一个非数字键,然后继续前进到下一个矢量,它只是第一个,但是反向并乘以向其中添加了8和100,但是当它继续前进时,它只是添加了一些零作为输入
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
vector<int> (myVec1)(n);
vector<int> (myVec2)(n);
int i;
int sum;
for(i=0;i<n;++i){
cout<<"please enter an integer ";
cin>>myVec1.at(i);
cout<<endl;
if(cin.fail()){
for(i=0; i<n; i++){
cout<<myVec1.at(i)<<" ";
}
}
}
cout<<endl;
for(i=0; i<n; i++){
sum=(myVec1.back()*8)+100;
myVec1.pop_back();
cout<<sum<<" ";
}
return 0;
}
this is the result and as you see theres just a bunch of zeros after i tried to stop the inputs if any one can help me id really appreciate it :) 这是结果,如您所见,如果有任何人可以帮助我,那么我尝试停止输入后只会出现一堆零。id真的很感激:)
please enter an integer 435 请输入整数435
please enter an integer 412 请输入整数412
please enter an integer 43 请输入整数43
please enter an integer 56 请输入整数56
please enter an integer 2 请输入整数2
please enter an integer q 请输入整数q
435 412 43 56 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 116 548 444 3396 3580 435 412 43 56 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100100100100100100100100100100100100100116548 444 3396 3580
int n
has undefined value (not initialized) int n
具有未定义的值(未初始化)
Instead, ignore defining n
and initialize your vector like this: vector<int> myVec1;
而是忽略定义n
并像这样初始化向量: vector<int> myVec1;
and when you read values, append them in the vector using myVec1.push_back(valueFromKeyboard);
读取值时,请使用myVec1.push_back(valueFromKeyboard);
将其附加到向量中myVec1.push_back(valueFromKeyboard);
#include <iostream>
#include <vector>
using namespace std;
int main () {
vector<int> myVec1;
vector<int> myVec2;
int valueFromKeyboard;
cout << "please enter an integer ";
while (cin >> valueFromKeyboard) {
if (!cin.fail()) {
myVec1.push_back(valueFromKeyboard);
}
cout << "please enter an integer ";
}
for (int i = 0; i < myVec1.size(); i++) {
cout << myVec1[i] << " ";
}
cout << endl << endl;
for (int i = 1; i < myVec1.size()+1; i++) {
myVec2.push_back(myVec1.end()[-i]*8+100);
}
for (int i = 0; i < myVec2.size(); i++) {
cout << myVec2[i] << " ";
}
return 0;
}
myVec1.end()[-1]
is the last element of the vector, myVec1.end()[-2]
is the second to last etc. That's why I created this loop for (int i = 1; i < myVec1.size()+1; i++)
to go from 1
to myVec1.size()+1
myVec1.end()[-1]
是向量的最后一个元素, myVec1.end()[-2]
是倒数第二个, myVec1.end()[-2]
。这就是为什么我for (int i = 1; i < myVec1.size()+1; i++)
从1
转到myVec1.size()+1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.