Ioakim Helen
Ioakim Helen

Reputation: 73

Adding an integer to a struct

I'm having an error like this one below

"incompatible types in assignment of int to int [10000]"

I can't understand what's wrong. Here is my code:

#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++;
    }

}

I would really appreciate it if someone helps me. :) Thanks

Upvotes: 1

Views: 1031

Answers (1)

Ziezi
Ziezi

Reputation: 6457

Avoid using variables with names same as the standard library, in your case rename the two file streams cin and cout, for example to, my_cin and my_cout.

If you want to read multiple strings or ints use std::vector instead of array, i.e. instead of your struct words you could use:

vector<string> words;

and then to read from file you could do something like:

// 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);
    }
}

Upvotes: 1

Related Questions