Input Error

When I am entering the file into the program, I am getting an output like this (Ignore the "..."'s, they are there so the 0's are positioned as they are in the output):

2 MasonCrosby 0 0 0 0

........................ 0 0 0 0
(Continued for 51 more lines)

However, the data file contains this:

2
Mason Crosby
K
73
207
28
6
Colorado
6 (player 2 starts here)
Graham Harrell
QB
74
215
28
6
Texas Tech

(continued for 51 more players)

What exactly is going wrong, and how do I fix it?
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <fstream>
#include <iomanip>
#define FILE_IN "packers.dat"
using namespace std;

struct playerinfo
{
       string playernum, playername, playerpos, playercol;
       int height, weight, age, exp;
}team [53];

void findaverages (int, playerinfo[], float*, float*);

void writeplayerdata (int, playerinfo[], float, float);

int main()
{
    char junk, exit;
    float avheight, avweight;
    int teamsize = 53, counter;
    cout << "This is the 2013-2014 Green Bay Packers team roster:";
    cout << endl;
    cout << endl;
    ifstream input;
    input.open (FILE_IN, ios::in);
    if (!input)
    {
        cout<<"Error accessing packers.dat";
        return 1;
    }
    else
    {
          for (counter = 0; counter < 53; counter ++)
          {
              input >> team[counter].playernum;
              input.get (junk);
              input >> team[counter].playername;
              input.get (junk);
              input >> team[counter].playerpos;
              input.get (junk);
              input >> team[counter].height;
              input.get (junk);
              input >> team[counter].weight;
              input.get (junk);
              input >> team[counter].age;
              input.get (junk);
              input >> team[counter].exp;
              input.get (junk);
              input >> team[counter].playercol;
              input.get (junk);
          }
    }
    input.close();
    
    findaverages (teamsize, team, &avheight, &avweight); //no flaw here, this works
    
    writeplayerdata (teamsize, team, avheight, avweight); //This may be the problem, so I'll include this function
    cout << "f";
    
    cout << endl;
    cout << "Press Enter to end the program. ";
    cin >> exit;
}

void writeplayerdata (int teamsize, playerinfo team[], float avgheight, float avgweight)
{
     int teamcounter;
     for (teamcounter = 0; teamcounter < teamsize; ++teamcounter)
     {
         cout << setw(3) << team[teamcounter].playernum << setw(10); 
         cout << team[teamcounter].playername << setw(6);
         cout << team[teamcounter].playerpos << setw(5); 
         cout << team[teamcounter].height << setw(6);  
         cout << team[teamcounter].weight << setw(5) << team[teamcounter].age; 
         cout << setw(4) << team[teamcounter].playercol << setw(5);
         cout << team[teamcounter].exp;
         cout << endl;
         cout << endl;
     }
     
     cout << "The average height of the team is " << avgheight << "inches.";
     cout << endl;
     cout << "The average weight of the team is " << avgweight << "pounds.";
}
Line 36 will read "2".
Line 38 will read "Mason".
Line 40 will read "Crosby".
Line 42 will fail to read anything because "K" is not a number.

If you want to read the whole line as a string you better use std::getline.
Last edited on
Is there a way to ignore the spaces between the two words without simply separating them into two different variables? I know I can do that for names, because there is always a first and last name, but I can't do that for colleges, because sometimes it is one word (like "Kentucky") or two (like"Texas Tech").
As I said use std::getline to read the whole line.
Topic archived. No new replies allowed.