I'm writing a program that reads a text file and outputs the data. For example, here's what one "entry" of the data looks like:
Alexander, Maurice
DB
1
0
0
0
0
I've written some code to read this first entry and output it.
struct playerType{
string name;
string position;
int touchdowns;
int catches;
int yardsPass;
int yardsRsh;
int yardsRcv;
};
int main(){
ifstream inData;
inData.open("data.txt");
playerType players[10];
getline(inData >> ws, players[0].name, '\n');
inData.clear();
cout << players[0].name << endl;
inData >> players[0].position;
cout << players[0].position << endl;
inData >> players[0].touchdowns;
cout << players[0].touchdowns << endl;
inData >> players[0].catches;
cout << players[0].catches << endl;
inData >> players[0].yardsPass;
cout << players[0].yardsPass << endl;
inData >> players[0].yardsRsh;
cout << players[0].yardsRsh << endl;
inData >> players[0].yardsRcv;
cout << players[0].yardsRcv << endl;
cout << endl << "After:" << endl;
cout << players[0].name << players[0].position << players[0].touchdowns
<< players[0].catches << players[0].yardsPass << players[0].yardsRsh
<< players[0].yardsRcv << endl;
}
I'm not sure why, but when I output each part of the struct on separate lines, the proper values are outputted. However, when I output everything on a single line, the name
gets overlapped by subsequent variables:
I'm sure I'm just making a beginner's mistake somewhere, but I can't seem to figure out why the output is like this. I was under the impression the variables would output one after another, instead of overlapping each other.