The file "Athlete info.txt" looks like this:
Peter Gab 2653 Kenya 127
Usain Bolt 6534 Jamaica 128
Bla Bla 2973 Bangladesh -1
Some Name 5182 India 129
What I expect my code to do is read the first string and assign it to the firstName
array (for eg Peter is stored in firstName[0]
), read the second string and assign it to lastName
array ( for eg Gab is stored in lastName[0]
) and so on..I've tried many different ways and even tried making it all string arrays but it doesn't work. If anyone can tell me what's wrong in the code or how to go about it, that would be great!
Thanks in advance!
void readInputFromFile()
{
ifstream inputData;
inputData.open("Athlete info.txt");
const int SIZE=50;
char firstName[SIZE],
lastName[SIZE],
athleteNumber[SIZE],
country[SIZE];
int athleteTime[SIZE];
int numOfCharacters=0;
if (inputData.is_open())
{
int i=0;
while(!inputData.eof())
{
inputData >> firstName[i];
inputData >> lastName[i];
inputData >> athleteNumber[i];
inputData >> country[i];
inputData >> athleteTime[i];
i++;
numOfCharacters++;
}
for (int i=0; i < numOfCharacters; i++ )
{
cout << "First Name: " << firstName[i];
cout << "Last name: " << lastName[i];
cout << "AthleteNumber: " << athleteNumber[i];
cout << "Country: " << country[i];
cout << "Time taken: " << athleteTime[i];
cout << endl;
}
}
else
{
cout << "ERROR" << endl;
}
inputData.close();
}