不匹配'运营商>>'在std :: cin>>

时间:2014-04-23 01:14:57

标签: c++ string

我似乎遇到了尝试从用户输入字符串的问题。我之前成功完成了这项工作。但现在它在这里抛出了错误。

error: no match for 'operator>>' in 'std::cin >> Pat1'

此处参考此代码。

#include <iostream>
#include <cmath>
#include <cctype>
#include <sstream>

using namespace std;

string PatConvert(string myString);

int main(){
    string Pat1[5],Pat2[5];
    cout<<"Please give the two five charter patterns";
    cin>>Pat1;//where the error occurs.
    cin>>Pat2;
    Pat1=PatConvert(Pat1);
    Pat2=PatConvert(Pat2);
    if (Pat1==Pat2){
        cout<<"The patterns match!";
        return 0;
    }else {
        cout<<"The patterns don't match!";
    }
}

string PatConvert(string myString){
    string filler[5];
    int fillerCount=1;
    for (int i=0; i<myString.length(); i++){
        for (int i2=0; i2<filler.length(); i2++){
            if (myString[i]==filler[i2])){
                break;
            }else if(myString[i]!=filler[i2]andi2==5){
                filler[fillerCount]=myString[i];
                myString[i]=fillerCount;
                return myString;
            };
        }
    }
}

我想知道是什么导致了这个问题,因为我已经查看了这个错误的其他实例,并且它们似乎发生在新的变量类型没有让它们“重载”的代码时我觉得它是。

但看到这是一个字符串,我使用了“cin&gt;&gt;”在获取用户输入字符串之前我不知道该怎么做。

作为旁注,有很多更多构建错误信息(~200行)。如果需要,我会添加它,但它似乎没有直接与此问题相关。

1 个答案:

答案 0 :(得分:2)

没有operator>>用于读取std::string的数组,您将需要定义自己的或使用循环。

for (auto& s : Pat1)
  std::cin >> s;

然后,您可能无意在第一时间定义字符串数组,将字符串重新定义为string Pat1, Pat2;

其他错误包括不在PatConvert中返回字符串,if (myString[i] == filler[i2]))中的额外括号,此} else if (myString[i] != filler[i2]andi2 == 5) {可能应为} else if ((myString[i] != filler[i2]) && (i2 == 5)) {

相关问题