Input from file (loop?)

I'm trying to input information from a file in the format

example:

EG
Owner
Name1
Adress1
PostalCode1
1 1 1 1 1 1 1 1 1 1 1 1
10
//emptyline
PD
Name1
Adress1
PostalCode1
1 1 1 1 1 1 1 1 1 1 1 1
10
//so on so forth

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
void Consumidor :: fonteFile(string nome, vector<Consumidor*> &consumi)
{
     ifstream file (nome.c_str()); 
	 if(!file){ cout << "Ficheiro não encontrado." << endl; return; }
     
     string tipo, name, morada, codpost, dono;
     vector <double> con;
     float taxa;
     char lixo;
	 
     cout << "A ler de ficheiro..." << endl;
	 while(!file.eof())
     {
       getline(cin, tipo);
       if(tipo == "PD" || tipo == "PC")
        { 
          getline(file, name);
          getline(file, morada);
          getline(file, codpost);
          file >> con.at(0) >> con.at(1)>> con.at(2)>> con.at(3)>> con.at(4)>> con.at(5)>> con.at(6)>> con.at(7)>> con.at(8)>> con.at(9)>> con.at(10)>> con.at(11);
          file >> taxa;
          file >> lixo;
          if(tipo == "PD") 
             {
                   Domestico *pd = new Domestico(tipo, name, morada, codpost, con, taxa); 
                   consumi.push_back(pd);
                   return;
             }
          else if(tipo == "PC") 
             { 
                   Comercial *pc = new Comercial(tipo, name, morada, codpost, con, taxa); 
                   consumi.push_back(pc);
                   return;
             }
          }
       else if(tipo == "EMP" || tipo == "EG")
        { 
          getline(file, dono);
          getline(file, name);
          getline(file, morada);
          getline(file, codpost);
          cin >> con.at(0) >> con.at(1)>> con.at(2)>> con.at(3)>> con.at(4)>> con.at(5)>> con.at(6)>> con.at(7)>> con.at(8)>> con.at(9)>> con.at(10)>> con.at(11);
          file >> taxa;
          file >> lixo;
          if(tipo == "EG")  
          { 
                  Grande *eg = new Grande(tipo, name, morada, codpost, con, dono, taxa); 
                  consumi.push_back(eg); 
                  return;
          }
          else if(tipo == "EMP") 
          {
                  PequenaMedia *emp = new PequenaMedia(tipo, name, morada, codpost, con, dono, taxa); 
                  consumi.push_back(emp);
                  return;
          }
        }
     }     
} 


I think the while doesn't end as it just stops at
 
    cout << "A ler de ficheiro..." << endl;
I suspect your line 14 is wrong:

 
getline(cin, tipo);


Shouldn't this be:


 
getline(file, tipo); // file rather than cin? 

Also, I will give you a tip.

It may be better to delete line 14 and move the statement into the while loop at line 12 like this:

1
2
3
4
while(getline(file, tipo))
{
   //...
}


The reason this works well is because getline() returns the stream, so the while statement tests the state of the stream AFTER it has attempted to read in a new line.

The problem with testing the stream BEFORE reading in the new line is that the stream may fail while reading that line.
Last edited on
Ok, that's one down, thanks.

Now the next problem.

After correctly reading the first record, I have this line
 
cout << "New Record of Type EG" << endl;


It prints fine, but it only reads the first set of lines (the first record) and ends the while cicle, even after taking out the
 
return;
In line 7 you need to reserve enough space for the values you want to use:

 
vector <double> con(12);


Again at line 42 you have cin rather than file:

 
cin >> con.at(0) >> con.at(1)>> con.at(2)>> con.at(3)>> con.at(4)>> con.at(5)>> con.at(6)>> con.at(7)>> con.at(8)>> con.at(9)>> con.at(10)>> con.at(11);


I think this should be more like:

 
file >> con.at(0) >> con.at(1)>> con.at(2)>> con.at(3)>> con.at(4)>> con.at(5)>> con.at(6)>> con.at(7)>> con.at(8)>> con.at(9)>> con.at(10)>> con.at(11);
I think it doesnt read the next set because it is waiting for the emptyline

example:

PC
Nome1
Morada1
Codigo1
1 1 1 1 1 1 1 1 1 1 1 1
10

EMP
Dono1
Nome2
Morada2
Codigo2
2 2 2 2 2 2 2 2 2 2 2 2
20

EG
Dono2
Nome3
Morada3
CodPostal3
3 3 3 3 3 3 3 3 3 3 3 3
30

It reads the first two, but not the third.

How can I correct this?
Last edited on
Okay. Your next problem is lixo.

You are trying to use it to skip a line. But the >> operator skips all white-space anyway. So it has already absorbed the blank line.

So I would simply remove lixo at line 9, and remove the lines where you read it at 22 and 44.

I think it might work them.

I didn't know it skiped white space...

It's at 100% now.

Thanls Galik.
Topic archived. No new replies allowed.