Need help with read from and write to a .txt file.

Hey i'm new to programming. I am coding in c++ and have to make a program that reads names and their age, add the sum of all ages then display it. Which works. But the second part of the task is to code it so you can add the names to the text file and the program reads that aswell.
The names are in a text file like this:

Marie Sten
29
Alexander Lind
35
Olle Ibrahimovic
30

Kajsa Persson

35
Peter Andersson
27
James Andersson
30

Now Peter and James Andersson are the names i've added but when i try to read the file those two are not counted or displayed.

Here is my code
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
  #include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string namn;
    int age;
    ifstream fil("elevinfo.txt");
    getline(fil, namn);

    fil>>age;
    int sum=0;
    int antal=0;

    while(fil.good())
    {
        antal++;
        sum+=age;
        cout<<namn<<" : "<<age<< endl;
        fil.ignore();
        getline(fil, namn);
        fil>>age;
    }

    fil.close();

    cout<<sum/antal<<endl;

    return 0;
}

When i run the program it looks like this:
Marie Sten : 29
Alexander Lind : 35
Olle Ibrahimovic : 30
Kajsa Persson : 35
Average age:32

Please help.
Empty lines in "elevinfo.txt" break your code.
Let's look all the iteration together:

1)
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
#include <fstream>
#include <iostream>
#include <string>

int main()
{
    std::string namn;
    int age;
    std::ifstream fil("elevinfo.txt");
    std::getline(fil, namn);        // namn --> Marie Sten

    fil >> age;                     // age --> 29
    int sum = 0;
    int antal = 0;

    while(fil.good())
    {
        antal++;                    // antal --> 1
        sum += age;                 // sum --> 29
        std::cout << namn << ": " << age << '\n';   // Marie Sten: 29
        fil.ignore();
        std::getline(fil, namn);    // namn --> Alexander Lind
        fil >> age;                 // age --> 35
    }

    fil.close();

    std::cout << sum/antal << '\n';

    return 0;
}


2)
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
#include <fstream>
#include <iostream>
#include <string>

int main()
{
    std::string namn;
    int age;
    std::ifstream fil("elevinfo.txt");
    std::getline(fil, namn);

    fil >> age;
    int sum = 0;
    int antal = 0;

    while(fil.good())
    {
        antal++;                    // antal --> 2
        sum += age;                 // sum --> 64
        std::cout << namn << ": " << age << '\n';   // Alexander Lind: 35
        fil.ignore();
        std::getline(fil, namn);    // namn --> Olle Ibrahimovic
        fil >> age;                 // age --> 30
    }

    fil.close();

    std::cout << sum/antal << '\n';

    return 0;
}


3)
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
#include <fstream>
#include <iostream>
#include <string>

int main()
{
    std::string namn;
    int age;
    std::ifstream fil("elevinfo.txt");
    std::getline(fil, namn);

    fil >> age;
    int sum = 0;
    int antal = 0;

    while(fil.good())
    {
        antal++;                    // antal --> 3
        sum += age;                 // sum --> 94
        std::cout << namn << ": " << age << '\n';   // Olle Ibrahimovic: 30
        fil.ignore();
        std::getline(fil, namn);    // empy line!! namn --> ""
                                    // now fil.good() == false

        fil >> age;                 // this line is no more executed
                                    // age --> 35
    }

    fil.close();

    std::cout << "Average age: " << sum/antal << '\n';

    return 0;
}


4)
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
#include <fstream>
#include <iostream>
#include <string>

int main()
{
    std::string namn;
    int age;
    std::ifstream fil("elevinfo.txt");
    std::getline(fil, namn);

    fil >> age;
    int sum = 0;
    int antal = 0;

    while(fil.good())               // loop isn't executed any more
    {
        antal++;
        sum += age;
        std::cout << namn << ": " << age << '\n';
        fil.ignore();
        std::getline(fil, namn);
        fil >> age;
    }

    fil.close();
    //                              94  / 3 in integer division is 31
    std::cout << "Average age: " << sum/antal << '\n';

    return 0;
}

Sorry, I gave an inaccurate answer: is reading an empty line by >> that breaks the code:
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
#include <fstream>
#include <iostream>
#include <string>

int main()
{
    std::string namn;
    int age;
    std::ifstream fil("elevinfo.txt");
    std::getline(fil, namn);

    fil >> age;
    int sum = 0;
    int antal = 0;

    while(fil.good())
    {
        antal++;
        sum += age;
        std::cout << namn << " : " << age << '\n';
        fil.ignore();
        std::getline(fil, namn);
        if(!fil.good()) { std::cout << "\"std::getline(fil, namn);\" failed.\n"; }
        fil >> age;
        if(!fil.good()) { std::cout << "\"fil >> age;\" failed.\n"; }
    }
    fil.close();
    std::cout << "Average age: " << sum/antal << '\n';
    return 0;
}


Output:
Marie Sten : 29
Alexander Lind : 35
Olle Ibrahimovic : 30
"fil >> age;" failed.
Average age: 31


You can also simplify your code.
The reading can be simpler:
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

int main()
{
  string namn;
  int age;
  ifstream fil("elevinfo.txt");
  int sum = 0;
  int antal = 0;
  while (getline(fil, namn))
  {
    if(namn.empty())
      continue;
    fil >> age;    
    antal++;
    sum += age;
    cout  << left << setw(16) << namn << " : " << age << endl;
  }
  cout << "Count: " << antal << "\n";
  cout << "Sum: " << sum << "\n";
  cout << "Average: " << double(sum) / antal << endl;
}


Output:

Marie Sten       : 29
Alexander Lind   : 35
Olle Ibrahimovic : 30
Kajsa Persson    : 35
Peter Andersson  : 27
James Andersson  : 30
Count: 6
Sum: 186
Average: 31
Thank you soo much for you help. Sorry for the late answer, i've been working. But i'm still having some issues. Thomas1965, i tried you code just out of curiosity but it still didn't work.

just said:

Marie Sten : 29
Alexander Lind : 35
Olle Ibrahimovic : 30
Kajsa Persson : 35
: 0
Count: 5
Sum: 129
Average: 25.8

Am i missing something? Maybe a plugin?
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

bool getData( istream &strm, string &name, int &age )
{
   if ( !getline( strm, name ) ) return false;             // attempt to read a name (may be blank line)
   while( name.find_first_not_of( " " ) == string::npos )  // repeat if line is blank (or newline not fed after >> operation)
   {
      if ( !getline( strm, name ) ) return false;
   }
   strm >> age;                                            // presumes that successful read of name means age is OK
   return true;
}


int main()
{
   string name;
   int age;
   int sum = 0, antal = 0;

   ifstream fil( "elevinfo.txt" );
   while( getData( fil, name, age ) )
   {
      antal++;
      sum += age;
      cout << name << ": " << age << endl;
   }
   fil.close();

   cout << "Count:   " << antal               << endl;
   cout << "Sum:     " << sum                 << endl;
   cout << "Average: " << (double)sum / antal << endl;
   return 0;
}


Marie Sten: 29
Alexander Lind: 35
Olle Ibrahimovic: 30
Kajsa Persson: 35
Peter Andersson: 27
James Andersson: 30
Count:   6
Sum:     186
Average: 31





It would be useful to put name and age in a struct, then overload >> for this struct.
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct Person
{
   string name;
   int age;
};


istream & operator >> ( istream &strm, Person &p )
{
   if ( !getline( strm, p.name ) ) return strm;              // attempt to read a name (may be blank line)
   while( p.name.find_first_not_of( " " ) == string::npos )  // repeat if line is blank (or newline not fed after >> operation)
   {
      if ( !getline( strm, p.name ) ) return strm;
   }
   strm >> p.age;                                           
   return strm;
}


ostream & operator << ( ostream &strm, const Person &p ) { return strm << p.name << ": " << p.age; }


int main()
{
   Person p;
   int sum = 0, count = 0;

   ifstream fil( "elevinfo.txt" );
   while( fil >> p )
   {
      count++;
      sum += p.age;
      cout << p << endl;
   }
   fil.close();

   cout << "Count:   " << count               << endl;
   cout << "Sum:     " << sum                 << endl;
   cout << "Average: " << (double)sum / count << endl;
}
Last edited on
Topic archived. No new replies allowed.