Monday, 11 April 2016

c++ - 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  tile 
vector > 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 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.


Answer



for (int i=0; i <= tileline.end(); i++) {   
vector 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 sizenums;


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




vector  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.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...