1

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;
   }
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
Reaperr
  • 21
  • 8
  • I believe your question is well answered here: http://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input – Maxim Busel Mar 22 '15 at 20:20

1 Answers1

0

I think that another approach could be taking advantage of operator overloading and standard containers to create a safer and more "c++ -ish" code if I might say. In my opinion it's clearer what is the format we expect. Anither thing that you should notice is that your code has a memory leak (games is never deleted) using a vector is the first place avoids this risk.

For example:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

struct GameInfo
{
    std::string name;
    int home;
    int away;
    friend std::istream &operator>>(std::istream &is, GameInfo &g);
    friend std::ostream &operator<<(std::ostream &os, GameInfo &g);
};

std::istream &operator>>(std::istream &is, GameInfo &g) {
    std::getline(is, g.name);
    is >> g.home;
    is >> g.away;
    is.ignore();
    return is;
}

std::ostream &operator<<(std::ostream &os, GameInfo &g) {
    os << "Name: " << g.name << "\n" << g.home << " " << g.away << std::endl;
    return os;
}


int main()
{
    std::string str = "3\nSD Lancers\n33   55\nND Cats\n34   67\nSD Big Horn\n67   68\n";
    std::cout << str;
    std::istringstream stream(str); // you could use ifstream instead
    size_t size(0);
    stream >> size; // not really nescesasry with vectors but still...
    stream.ignore();
    std::vector<GameInfo> gv;
    for (unsigned int i = 0; i<size; ++i){
        GameInfo g;
        stream >> g;
        gv.push_back(g);
    }

    std::cout << "\n-----------------\n";

    for (auto& el : gv)
    {
        std::cout << el << "\n-----------------\n";
    }

    return 0;
}

(coliru)

Scis
  • 2,934
  • 3
  • 23
  • 37