infile? getline? missing?

Hi i've been working on this code.
it works, but there is major problem.
first, it didnt print first line from the file(.txt)
citizen of abc
john paul
1999
5001.11
citizen of def
chris ben
2000
234.112

second, it prints out to be
1. John
2. Paul
3. 1999
4. 5001.11

how can i make it look like this
1. citzen of abc
2. john paul
3. 1999
4. 5001.11

my code, with a lot problem i guess.

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
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
#include <fstream>


using namespace std;

const int maxx = 20;

class Book{
      string title;
      string author;
      int year;
      float dewey;
      int age;
      public:
             void readFile(ifstream& infile)
             {  
                  
                  infile>>title>>author>>year>>dewey;
                  }
            void gradeAge()
                  {
                       int current=2012;
                       age=current-year;
                       }
                       
                         void displayResult()
                         {
                            cout
                               <<"\nTitle:"<<title
                            <<"\nAuthor:"<<author
                            <<"\nYear:"<<year
                            <<"\nDeweyNo:"<<dewey
                            <<"\nAge of Books:"<<age<<"\n";
                            }
             };
             
         

                 
                            
int main()
{
    
    Book book[maxx];
    std::ifstream infile;
    std::string line;
    infile.open("books.dat");
    if( infile.fail() )
        cout<<"\nFile not found!\n";
    else
    {
        while ( std::getline(infile,line))
        {
        for(int i=0;i<maxx;i++)
        {
                
                book[i].readFile(infile);
                book[i].gradeAge();
                book[i].displayResult();
                }
                infile.close();
                system("pause");
                return 0;
                }
    
}
}
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
int main()
{
    
    Book book[maxx];
    std::ifstream infile;
    std::string line;
    infile.open("books.dat");
    if( infile.fail() )
        cout<<"\nFile not found!\n";
    else
    {
        while ( std::getline(infile,line)) /// you read first line here
        {
        for(int i=0;i<maxx;i++)
        {
                
                book[i].readFile(infile); /// then you read second line
                                                    /// the >> operator reads to space
                                                    /// and puts data in first variable if it can
                                                    /// the >> reads next data to space
                                                    /// and puts data into second variable
                                                    /// etc  

                book[i].gradeAge();
                book[i].displayResult();
                }
                infile.close();
                system("pause");
                return 0;
                }
    
}

}
Last edited on
Topic archived. No new replies allowed.