简体   繁体   English

原始输入后,我一直得到零

[英]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.

相关问题 即使“用户”输入数字||,我在运行程序时仍会得到0。 C ++绘画工作 - I keep getting 0's when I run my program even though the “user” inputs numbers || c++ painting job 在 fork() 之后,我的程序中不断获得相同的 pid - I keep getting the same pid in my program after fork() 无论我用作输入,输出全为零 - whatever i use as inputs, outputs are all zeros 当我运行我的代码时,我不断收到此错误“在抛出 'std::bad_alloc' what(): std::bad_alloc 实例后调用终止” - I keep getting this error "terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc" when I run my code 如何让我的向量在输入后显示 8 个整数? 当我输入 8 个整数时,它只显示零而不是我输入的内容 - How do I get my vector to display 8 integers after entering them? When I do enter 8 integers, it only displays zeros and not what I entered 我是编程新手。 编译包含 ah 文件中包含的 class 定义的代码后,我不断收到错误消息 - I am new in programming. I keep getting an error message after compiling my code that contains a class definition included in a .h file 为什么我在使用 memset 用零填充 object 后在 dynamic_cast 出现异常 - Why am i getting an exception at dynamic_cast after filling object with zeros using memset 打印时继续为我创建的阵列获得不同的输出 - Keep Getting A Different Output For My Created Array When Printing 为什么我的程序始终无法获取外部信息? - Why do I keep getting unresolved externals for my program? 我不断从cin.get()中获取程序空间 - I keep getting a space in my program from cin.get()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM