I have a program that I am starting and have hit an early roadblock. This program opens up a .txt file with basketball info. The first number in the .txt is the size(of the games array) then the next line is the name with the away and home score under it. The program should store all the information into the newly created array of struct Gameinfo games. Ex. of .txt file
3
SD Lancers
33 55
ND Cats
34 67
SD Big Horn
67 68
The program gets the size and dynamically allocates it correctly but It does not get the rest. My insides of the loop must not be correct? My cout at the top does not give anything for the name[0].name. I think I am not understanding the getline correctly? Any help would be greatly appreciated! Thanks. Also Please ignore the spacing. I never get them formatted write on here.
CODE:
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cctype>
#include <stdlib.h>
using namespace std;
struct GameInfo
{
string name;
int home;
int away;
};
void testpara(ifstream& in,struct GameInfo * & games, int & size);
int main()
{
int size;
GameInfo * games;
ifstream in;
in.open ("games.txt"); //Here is my problem
if (!in)
{
cout<<"Could not open file";
}
testpara(in,games, size);
in.close();
cout << "size of games is: " << size << endl;
cout<<games[1].name<<endl;
return 0;
}
void testpara(ifstream& in,struct GameInfo * & games, int & size)
{
int test;
int count = 0;
in>>size; //trying to get the first number for the size
// dynamically allocate games array
games = new GameInfo [size];
while(count<size && !in.eof())
{
getline(in,games[count].name);
in>>games[count].away>>games[count].home;
count++;
}
return;
}