This is the definition of my class:
class BPP
{
unsigned n; /* nº de instancias */
vector<string> nombre_instancia; /* nombre de la instancia*/
vector<unsigned> n_objetos; /* nº de objetos a almacenar en cada instancia */
vector<unsigned> C; /* capacidad máxima de cada contenedor */
vector<unsigned> mejor; /* mejor nº mínimo de contenedores usados (m*) */
vector< vector<unsigned> > tam_objeto;
That’s my complete constructor:
BPP :: BPP(char fich1[])
{
ifstream file1; string str; unsigned a, b, c, d;
file1.open(fich1);
if (file1.is_open()){
file1 >> (unsigned &) n;
for (unsigned k = 0 ; k < n ; ++k){
getline(file1, str); nombre_instancia.push_back(str);
file1 >> a; n_objetos.push_back(a);
file1 >> b; C.push_back(b);
file1 >> c; mejor.push_back(c);
for (unsigned h = 0 ; h < a ; ++h){
file1 >> d; tam_objeto[k].push_back(d);
}
}
}
file1.close();
}
The 5 first lines in fich1 are:
10
P_0 /*Notice the space in the beginning of the line, and in the end*/
150 120 52
30
34
And the output is all values readed are 0 (if unsigned) or “” (if string)
The problem is that the new-line character after the
10is not consumed by:this means that the
getline(file1, str)call reads the new-line character only, an empty line. The next input operation is:which fails because
P_0is not a validunsigned int. This failure prevents any further reads from thefile1stream (its failbit is set).To resolve, you could just simply call
getline()twice, ignoring the result of the first call.Check the result of all reads to detect failure. Doing so would have alerted you to the read failures.