Passing input file into function

I have posted the code I am working on below. Right now everything is working except for the printing function called 'onetile'. I am trying to pass in the input 'in' into the function like I did in the function get_tileset. The way it is right now the input file 'in' is not passing to 'onetile'. Any thoughts?

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

using std::vector;
using std::make_pair;
using std::cout;

typedef std::pair<int, int> CellType;

typedef vector<CellType> PieceType;

const int DefaultTSize = 5;

// Error codes
const int BADCHAR = 1;
const int BADSIZE = 2;
const int NOTILE = 3;

void onetile(std::istream* in, int TSize) {
std::string row;

vector<std::string> filelines;
while (getline(*in, row)) {
filelines.push_back(row);
}

for (auto line : filelines)
cout << line << '\n';
}

vector<PieceType> get_tileset(std::istream* in, int TSize, int * errorcode) {
vector<PieceType> pieces;
std::string row;
PieceType p;

*errorcode = 0;
int npieces;

vector<std::string> filelines;
while (getline(*in, row)) {
filelines.push_back(row);
}

if (filelines.size() % (TSize + 1) != 0) {
*errorcode = BADSIZE;
return pieces;
}

for (int i = 0 ; i < filelines.size() ; i++) {
if (i % (TSize + 1) == TSize) {
if (filelines[i].size() != 0)
*errorcode = BADSIZE;
} else {
if (filelines[i].size() != TSize)
*errorcode = BADSIZE;
}
}
if (*errorcode)
return pieces;

npieces = filelines.size() / (TSize + 1);

for (int i = 0; i < npieces; i++) {
p.clear();
for (int y = 0; y < TSize ; y++) {
row = filelines[i * (TSize + 1) + y];

for (int x = 0; x < TSize; x++)
if (row[x] == '*') {
p.push_back(make_pair(x, TSize - 1 - y));
} else if (row[x] != '.') {
*errorcode = BADCHAR;
return pieces;
}
}
if (p.empty()) {
*errorcode = NOTILE;
return pieces;
}
pieces.push_back(p);
}

return pieces;

}

int main(int argc, char const *argv[]) {
int TSize;
std::istream * in = &std::cin;
std::ostream * out = &std::cout;
std::ifstream infile;
std::ofstream outfile;
TSize = (argc == 1) ? DefaultTSize : std::stoi(argv[1]);

vector<std::string> contents;

if (argc > 2) {
infile.open(argv[2]);
in = &infile;
}

if (argc > 3) {
outfile.open(argv[3]);
out = &outfile;
}

int errorcode;

vector<PieceType> p = get_tileset(in, TSize, &errorcode);
if (errorcode)
return errorcode;

onetile(in, TSize);


return 0;
}

use the variable Tsize to manage the loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
#include <string>

void onetile(std::istream* in, size_t Tsize) {
std::string row;
size_t i{};

std::vector<std::string> filelines;
while (i < Tsize && getline(*in, row)) {
filelines.push_back(row);
++i;
}

for (const auto& line : filelines)
std::cout << line << '\n';
}

int main()
{
    onetile(&std::cin, 3);

}

edit: for the expression in the while() loop i < Tsize must be written first, otherwise you'll enter 4 lines of data but print out only 3 as the 4th call to getline() gets evaluated before reaching if i < Tsize
you need the counter to stop reading from std::cin but if you were reading a file then the counter is not necessary as the program will stop reading the file once its eofbit is set
Last edited on
Topic archived. No new replies allowed.