简体   繁体   English

当用户输入“ enter”键时停止

[英]stopping when user enters the 'enter' key

i've been writing a program that takes input from the user in two different "vectors" and stoping when enter key is hit.The code is given below 我一直在编写一个程序,该程序从两个不同的“向量”中接受用户的输入,并在按下Enter键时停止。代码如下所示

 #include <iostream>
 #include <vector>
 #include <string>

using namespace std;

int main()
{
    vector<string> a,c;
    string b;
    int m=0;//,words = 0;
    do
    {
        cin>>a[m];
        m++;
    }
    while(cin.get()!= '\n');

    int n=0;
    do
    {
        cin>>c[n];
        n++;
    }
    while(cin.get() != '\n');

    int mida=a.size()/2, midc=c.size()/2;
    int count;
    int i,j;

    for(i=0;i<n;i++)
        for(j=0;j<m;j++)
            {
                if(a[i] == c[j])
                count++;
            }

    if(count >= mida  || count >= midc)
        cout<<"similar"<<endl;
    else
        cout<<"dissimilar"<<endl;
    return 0;
}

Now the problem is that when i run the code after entering the words i need to put into the vector something like 'mango' 'orange' seperated by spaces, however as soon as i hit enter i get a segmentation fault. 现在的问题是,当我在输入单词后运行代码时,我需要将向量放入类似“ mango”,“ orange”的空格之间,但是一旦我按下Enter,就会遇到分割错误。 Could anyone tell me whats could possibly going wrong. 谁能告诉我可能出什么问题了。

When you create a std::vector object, it starts out empty , its size will be zero and all indexing in it will be out of bounds and lead to undefined behavior (and probable crashes). 当您创建一个std::vector对象时,它开始为empty ,其size将为零,并且其中的所有索引都将超出范围,并导致未定义的行为 (并可能崩溃)。

You can use push_back to append elements to the vectors. 您可以使用push_back将元素附加到向量。 Like 喜欢

std::string s;
std::cin >> s;
a.push_back(s);

While the above is the most likely reason for the crash you're having, it's not your only problem. 尽管以上是导致崩溃的最可能原因,但这并不是您的唯一问题。 Another is that each string of input might be ended with a newline. 另一个是每个输入字符串都可以以换行符结尾。 If you want to read a full line and then separate it into "words" then I suggest you use std::getline to read the full line, then use an std::istringstream to get the separate words from the line. 如果您想读完整的一行然后将其分成“单词”,那么我建议您使用std::getline读取完整的行,然后使用std::istringstream从该行中获取单独的单词。

You can also use std::istream_iterator to easily append strings from an input stream to a vector. 您还可以使用std::istream_iterator轻松字符串从输入流追加到向量。

You could do something like 你可以做类似的事情

std::string line;
std::getline(std::cin, line);
std::istringstream iss(line);

a = std::vector<std::string>(std::istream_iterator<std::string>(iss),
                std::string>(std::istream_iterator<std::string>());

cin>>a[m];

You are indexing past the end of the vector a . 您正在索引超过向量a的末尾。 Because the vector hasn't been given a size, it has no space for even a[0] to be assigned. 由于没有给向量指定大小,因此甚至没有空间可以分配a[0]

A possible soution is to use a temporary variable to get the user input and then a.push_back[input] . 可能的解决方法是使用临时变量获取用户输入,然后使用a.push_back[input]

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

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