I am working on a project to solve the Banker's Algorithm. I have read input from a file that defines an available array, a matrix for allocated resources and maximum resources. The input file is structured as:
3 3 2
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
My code to read in the matrices is:
// get input for available resources
for (int r = 0; r < R; ++r) {
std::string numStr;
char in;
do {
input.get(in);
numStr += in;
} while (in != ' ');
avail_[r] = std::stoi(numStr);
}
// get input for allocated vector
for (int p = 0; p < P; ++p) {
for (int r = 0; r < R; ++r) {
std::string numStr;
char in;
do {
input.get(in);
numStr += in;
} while (in != ' ');
alloc_[p][r] = std::stoi(numStr);
}
}
// get input for max
for (int p = 0; p < P; ++p) {
for (int r = 0; r < R; ++r) {
std::string numStr;
char in;
do {
input.get(in);
numStr += in;
} while (in != ' ');
max_[p][r] = std::stoi(numStr);
}
}
I have loops that output the value of the matrices after they are read. The output is
Available:
3 3 2
Allocated:
1 0 0
0 0 2
1 1 0
2 5 3
2 2 0
It stops after reading the allocation matrix and it doesn't even read it right. I assume I am using std::ifstream.get() wrong, but I don't see how.
I have found that if I put all of my data in 1 line, it reads the allocation matrix correctly, but my program still stalls when trying to read in the max matrix (even though I used the same code I used for the allocation matrix when reading in max).
Please see my comments marked with
// CHANGE HERE