problem in read data from txt file

i have try to read data from txt file and store it in my link list

here's my read file function
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
.
.
.
void readFile()
{
  ifstream input("list.txt");
  
  if(input.fail())
  {
    cout << "\n     Files does not exist."
         << "\n     Program Exit.";
    return;
  }
  
  string data, ic, name, cNum, email;  //read data
  int c = 0;                           //count data
  while(!input.eof())                  //continue if not end of the file  
  {
    getline(input, data, '\n');
    c++;
    
    switch(c)
    {
      case 1:
           id = data;
           data = "";
           break;
      case 2:
           name = data;
           data = "";
           break;
      case 3:
           cNum = data;
           data = "";
           break;
      case 4:
           email = data;
           data = "";
           c = 0;
           break;
      default:
           cout << "\n     Count error";
    }
   
    addData(id, name, cNum, email); //add data to the link list
  }
  cout << "\n     All applicants data added successful.";
}
.
.
.


here's my "list.txt" file

111111111111
name name
1111111111
email@email
2222222222
name name
1212121212
email@email
939393939393
name name
2323232323
email@email


my problems here there are 4 copies of data added to my link list, it mean there are 4 same data added to my link list... i can't find what is the problem... can someone help me please...
You call addData for each line you read. You have to read all 4 rows and then add the data to the list.
thx a lot! i get it now!
1
2
3
4
5
6
case 4:
           email = data;
           data = "";
           c = 0;
           addData(id, name, cNum, email);
           break;


and t i thk mine code here is not so good, it seems longer, is there another shorter way to do the same job?
A little shorter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void readFile()
{
   ifstream input("list.txt");
  
   if(input.fail())
   {
      cout << "\n     Files does not exist."
              << "\n     Program Exit.";
      return;
   }
  
  string data, id, name, cNum, email;  //read data

  while ( getline(input, id) )
  {
       getline(input, name) ;
       getline(input, cNum) ;
       getline(input, email) ;

       if (input)
            addData(id, name, cNum, email) ;
  }
}
Last edited on
thx ^.^
Topic archived. No new replies allowed.