Hayley
Hayley

Reputation: 23

Iterating values into a vector while reading in a file using getline

I am trying to create a vector filled with values of the size of each string line of a file of 'blokus tiles' (tiles of periods or stars that are of dimensions 5x5). The file is read in as a vector of vectors of strings,

vector <std::string> tile 
vector <vector<string>> alltiles

I am trying iterate values into a vector that stores the sizes of the tile strings (each line). I am doing this to later output an error if each line is not the same length, or each line is an incorrect length, or if there are other characters besides stars (*) or periods (.). I did this to print the size of the tiles in the file blokusstatus.txt (which was entered as a command line argument),

if (infile.is_open()) {
   while (std::getline(infile, tileline)) {   
     int actualtilesize = tileline.length();
     cout << actualtilesize << std::endl;

     tile.push_back(tileline);
     alltiles.push_back(tile); 
   }
  }
  infile.close();

//print out the contents of the file
  std::ofstream outfile;
  outfile.open(arginfile); 
  for (auto e: tile) { 
    cout << e << std::endl; 
  }

Here is the result:

ec327@ec327-VirtualBox:~$ ./w5blokus2 5 blokusstatus.txt
5
5
5
5
5
0
5
5
5
5
5
0
5
5
5
5
5
.....
.*...
**...
*....
*....

.....
.....
**...
*....
**...

.....
.....
*....
***..
*....

This looks good. However, I then try to make the list of numbers into a vector this way:

if (infile.is_open()) {   //infile is open only if 3 or 4 arguments
  int i = 0;
  while (std::getline(infile, tileline)) {
    for (int i=0; i <= tileline.end(); i++) {   
      vector <int> sizenums;
      sizenums[i] = tileline.length();
      i++;
      cout << sizenums << std::endl;
    }
    //cout << actualtilesize << std::endl;
  }
  tile.push_back(tileline);
  alltiles.push_back(tile); 
}
infile.close();

std::ofstream outfile;
outfile.open(arginfile);  
for (auto e: tile) { 
  cout << e << std::endl; 
}

This gives quite a lengthy error when compiled, including

^~~~~~~~ /usr/include/c++/6/ostream:497:5: note: template argument /substitution failed:

w5blokus3.cpp:80:15: note: deduced conflicting types for parameter ‘_CharT’ (‘char’ and ‘std::vector’) cout << sizenums << std::endl;"

w5blokus3.cpp:80:15: note: cannot convert ‘sizenums’ (type ‘std::vector’) to type ‘char’

cout << sizenums << std::endl;

and I'm not sure what is wrong. I'm a newbie, thanks for any help or tips.

Upvotes: 2

Views: 775

Answers (2)

jhh
jhh

Reputation: 673

for (int i=0; i <= tileline.end(); i++) {   
   vector <int> sizenums;
   sizenums[i] = tileline.length();

First, you create a new vector each iteration through the loop. You need to create the vector, then loop or iterate through it.

However, after

vector<int> sizenums;

your vector is empty. Direct access by [] won't work. Use push_back to add elements to the end:

vector <int> sizenums;
...
for (int i=0; i <= tileline.end(); i++) {  
    sizenums.push_back(tileline.length());

Also:

 for(int i ...
     ...
     i++;

Don't increase your loop variable manually. The for loop handles that already.

Upvotes: 0

Andria
Andria

Reputation: 5075

Two things, you need to iterate over the vector to print out the contents of it and you can't use sizenums[i] = tileline.length(); because the vector was just initialized, you have to use something like sizenums.push_back(tileline.length()); but then again the way you're using it is wrong anyway, you're reinitializing the variable each time you loop through therefore you've just used it to temporarily store one variable. Instead if you want to actually store each line length, then you need to move it outside the loops. And I don't know why you put i++; there again, it seems unnecessary.

Upvotes: 0

Related Questions