向结构添加整数

时间:2015-12-03 09:36:10

标签: c++ struct int

我有一个像下面这样的错误

  

" intint [10000]"

的分配不兼容

我无法理解错误。这是我的代码:

#include<fstream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

struct words{

    string lexis;
    int sizes[10000];

} a[10000];

bool s(const words& a,const words& b);

//==========================================
int main() {
    int i,x;
    string word;

    //input-output with files
    ifstream cin("wordin.txt");
    ofstream cout("wordout.txt");

    i = 0;

    //reading until the end of the file
    while(!cin.eof()){
        cin >> word;

        x = word.size();

        a[i].sizes = x; //the problem is here

        a[i].lexis = word;
        i++;
    }

}

如果有人帮助我,我会非常感激。 :) 感谢

1 个答案:

答案 0 :(得分:1)

避免使用名称与标准库相同的变量,在您的情况下,重命名两个文件流cincout,例如,my_cinmy_cout

如果您想阅读多个stringint使用std::vector而不是数组,而不是您可以使用的struct words

vector<string> words;

然后从文件中读取你可以做的事情:

// attach stream to file
ifstream my_cin("wordin.txt");

// check if successfully opened
if (!my_cin) cerr << "Can't open input file!\n";

// read file line by line
string line;
while (getline(my_cin, line)) {

    // extract every word from a line
    stringstream ss(line);
    string word;
    while (ss >> word) {

        // save word in vector
        words.push_back(word);
    }
}