Can't open csv file and dsiplay it

Pages: 12
Hello all, I'm trying to display my csv file, and i just can't seem to get it to open. I've tried a few methods but they all displayed my error prompt so i'm stuck on this assignment, help would be appreciated, the assignment is making an object containing goalie stats.
I'm not sure if im declaring something wrong off what i'm just confused lol

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
 #include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <stdio.h>
#include "Goalies.h"
#include "Goalies.cpp"
#include <vector>
#include <iomanip>



using namespace std;

#define MAX_GOALIES 1

bool openFileIn(fstream &, string);
class Goalies NHL;
string getName(fstream &, Goalies *);
string getTeam(fstream &, Goalies *);
int getWins(fstream &, Goalies *);
int getLosses(fstream &, Goalies *);
int getGoalsAllowed(fstream &, Goalies *);
int getShotsAgainst(fstream &, Goalies *);
void swap(Goalies &a, Goalies &b);
void sortWins(Goalies wins[], int size);
void sortLosses(Goalies losses[], int size);
void sortGoalsAllowed(Goalies goalsAllowed[], int size);
void sortShotsAgainst(Goalies shotsAgainst[], int size);
int binarySearch(const int array[], int size, int value);
int getNHL(fstream &, Goalies *);
int getGoalies(fstream &, Goalies *);
int showGoalies(fstream &, Goalies *);
void split(const string&, char , vector<string>& );

int main()
{
    // declaration for file
    fstream dataFile;           // data file
    Goalies NHL[MAX_GOALIES];   // makes NHL the goalies class
    int count;

    if(openFileIn(dataFile, "goalie_stats.csv"))
    {
       // get goalie data from file into array


        dataFile.close();
    }


    return 0;


}


More info on the .h and .cpp file below


Last edited on
.h file

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
#ifndef GOALIES_H
#define GOALIES_H

#include<string>

using namespace std;

class Goalies 
{
    public:
        Goalies();
        Goalies(string n, string t);
        Goalies(string n, string t, int w, int l, int ga, int sa);

        // constructors
         ~Goalies();

         // implement setters

         void setName(string n);
         void setTeam(string t);
         void setWins(int w);
         void setLosses(int l);
         void setGoalsAllowed(int ga);
         void setShotsAgainst(int sa);

         // implement getters

         string getName();
         string getTeam();
         int getWins();
         int getLosses();
         int getGoalsAllowed();
         int getShotsAgainst();
         double getSavePct();

    private:

        //attributes
        string name;
        string team;
        int wins;
        int losses;
        int goalsAllowed;
        int shotsAgainst;
};

#endif // GOALIES_H

.cpp file

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "Goalies.h"

// no args constructor
Goalies::Goalies()
{

    name = "";
    team = "";
    wins = 0;
    losses = 0;
    goalsAllowed;
    shotsAgainst;

}

Goalies::Goalies(string n, string t)
{
   name = "";
    team = "";
    wins = 0;
    losses = 0;
    goalsAllowed = 0;
    shotsAgainst = 0;
}

Goalies::Goalies(string n, string t, int w, int l, int ga, int sa)
{
    name = "";
    team = "";
    wins = w;
    losses = l;
    goalsAllowed = ga;
    shotsAgainst = sa;
}


Goalies::~Goalies()
{

}

void Goalies::setName(string n)
{
    name = n;
}

void Goalies::setTeam(string t)
{
    team = t;
}

void Goalies::setWins(int w)
{
    wins = w;
}

void Goalies::setLosses(int l)
{
    losses = l;
}

void Goalies::setGoalsAllowed(int ga)
{
    goalsAllowed = ga;
}

void Goalies::setShotsAgainst(int sa)
{
    shotsAgainst = sa;
}



string Goalies::getName()
{
    return name;
}

string Goalies::getTeam()
{
    return team;
}

int Goalies::getWins()
{
    return wins;
}

int Goalies::getLosses()
{
    return losses;
}

int Goalies::getGoalsAllowed()
{
    return goalsAllowed;
}

int Goalies::getShotsAgainst()
{
    return shotsAgainst;
}
This is the style i'm going for (teachers example code) but what do i substitute in for getWidgets?? I feel like it's in practical to write get...(all the goalies data individually), am i suppose to put getGoalies where it says count??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
	fstream dataFile;                   // data file
    Widget *widgets = new Widget[MAX_WIDGETS];     // dynamically allocate widget records
    int count;

	if (openFileIn(dataFile, "widgets.dat"))
	{
        // get widget data from file into array
    	count = getWidgets(dataFile, widgets);

    	dataFile.close();

#if DEBUG
        showWidgets(widgets, count);
#endif
        writeWidgetsOfstream(widgets, count);
	}
	else
       cout << "File open error!" << endl;

	return 0;
}
Also had these functions in my main, but they r unusable since i can't even open the file lol
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
// binary search function
int binarySearch(const int array[], int size, int value)
{
   int first = 0,             // First array element
       last = size - 1,       // Last array element
       middle,                // Mid point of search
       position = -1;         // Position of search value
   bool found = false;        // Flag

   while (!found && first <= last)
   {
      middle = (first + last) / 2;     // Calculate mid point
      if (array[middle] == value)      // If value is found at mid
      {
         found = true;
         position = middle;
      }
      else if (array[middle] > value)  // If value is in lower half
         last = middle - 1;
      else
         first = middle + 1;           // If value is in upper half
   }
   return position;
}

// swap function
void swap(Goalies &a, Goalies &b)
{
   Goalies temp = a;
   a = b;
   b = temp;
}

bool openFileIn(fstream &file, string name)
{
   file.open(name.c_str(), ios::in);
   if (file.fail())
      return false;
   else
      return true;
}


Make sure you understand the location where your program is executing from. It's called your program's "working directory".

If you don't know where your program is executing from, one hackish way to check is to do
system("cd"); on Windows, or system("pwd"); on *nix.
That will print a directory. Make sure your file is in that directory.

You also don't need to call file.close() if it's the end the scope of the file stream. close will b called automatically. You should let the user know if the program was unable to open a file.

1
2
3
4
5
6
7
8
    if(openFileIn(dataFile, "goalie_stats.csv"))
    {
        // get goalie data from file into array
    }
    else
    {
        cout << "Error: could not open goalie_stats.csv\n";
    }
Last edited on
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
int main()
{
    ifstream dataFile;                   // Changed to ifstream as file is read only

    if (openFileIn(dataFile, "widgets.dat"))
    {
        Widget *widgets = new Widget[MAX_WIDGETS];     // dynamically allocate widget records
        // get widget data from file into array
    	int count = getWidgets(dataFile, widgets);

    	dataFile.close();

#if DEBUG
        showWidgets(widgets, count);
#endif
        writeWidgetsOfstream(widgets, count);
    }
    else
        cout << "File open error!" << endl;

    return 0;
}


bool openFileIn(ifstream &file, const string& name)  // Changed
{
    file.open(name);   // You can use std::string with .open()
    return file.is_open();  // is_open() returns true if open or false if not
}

Last edited on
Okay so i've made some little adjustments, the main thing i'm confused about tho is line 46(error message) "Goalies does not name a type" and what to put in at line 48(after get) and 53 (after show)

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
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <stdio.h>
#include "Goalies.h"
#include "Goalies.cpp"
#include <vector>
#include <iomanip>



using namespace std;

#define MAX_GOALIES 100

bool openFileIn(ifstream &, string);
class Goalies NHL;
string getName(fstream &, Goalies *);
string getTeam(fstream &, Goalies *);
int getWins(fstream &, Goalies *);
int getLosses(fstream &, Goalies *);
int getGoalsAllowed(fstream &, Goalies *);
int getShotsAgainst(fstream &, Goalies *);
void swap(Goalies &a, Goalies &b);
void sortWins(Goalies wins[], int size);
void sortLosses(Goalies losses[], int size);
void sortGoalsAllowed(Goalies goalsAllowed[], int size);
void sortShotsAgainst(Goalies shotsAgainst[], int size);
int binarySearch(const int array[], int size, int value);
int getNHL(fstream &, Goalies *);
int getGoalies(fstream &, Goalies *);
int showGoalies(fstream &, Goalies *);
void split(const string&, char , vector<string>& );

int main()
{
    // declaration for file
    ifstream dataFile;           // data file
    Goalies NHL[MAX_GOALIES];   // makes NHL the goalies class
    int count;

    if(openFileIn(dataFile, "goalie_stats"))
    {
       // get goalie data from file into array
    Goalies *Goalies = new Goalies[MAX_GOALIES];

    int count = get(dataFile, Goalies);


        dataFile.close();

        show (Goalies,count);

        writeGoaliesofstream(Goalies, count);
    }

    else
        cout << "Could not open goalie_stats\n" << endl;

    return 0;


}
Last edited on
So i put NHL in instead of Goalies and now my next line gives me and error, first i'm unsure if that correction i made is right and secondly the int count line gives me the error message "expected primary expression before ')' tokem"?

1
2
3
4
5
6
  

// get goalie data from file into array
    Goalies *NHL = new Goalies[MAX_GOALIES];

    int count = get(dataFile, Goalies);
Your line 18 is odd:

class Goalies NHL;

Goalies is a type you've already defined as a class in Goalies.h, which you've #included at line 6. So this declaration should simply be:

Goalies NHL;

I'll note that you're creating a global variable here, and global variables should be avoided as much as possible. There's absolutely no need for it to be global, and you don't seem to be using it anywhere but main.

Also, in your latest code, you're shadowing it with a local variable of the same name, although with a different type:

Goalies *NHL = new Goalies[MAX_GOALIES];

EDIT: It looks to me like Goalies represents data for a single goalie, rather than multiple goalies, so "Goalies" is a bad name for the class.

EDIT 2: I've just noticed that you're #including a cpp file. Don't do that. It's likely to cause you problems with multiple redefinitions, and even if you do jump through hoops, it's inefficient. Just compile it independantly, and use the linker to link the object code into your executable.
Last edited on
You don't need line 18 from your previous post since you're trying to create a instance with the same name inside the function. Also global variables are usually a bad practice, try to avoid them whenever possible.

Second since this is C++ line 15 should really be "const MAX_GOALIES = 100;" since it is more type safe.

Third, you should not be #including the Goalies.cpp file, source files should be added to your project/build line to be compiled, not #included.

In your last post there is no need for the horrible dynamic memory since MAX_GOALIES is a compile time constant static allocation is the way to go.

Lastly for now, if you are getting compile/link errors post your warning/error messages, all of them, exactly as they appear in your development environment. Don't just post a partial message, those messages have important information embedded within them to aid in locating and fixing the problems (ie they have the line number where the compiler detects the problems). And realize that one error/warning may generate several different but related messages.



Errors are below, It has to keep the .ccp and .h file since my professor requested, i still don't understand how to open the file. My main problem right now is: line 41-63, where i'm trying to insert the csv file -goalie_stats. On line 48, i'm not sure what gets inserted right after the get and in the parenthesis, keep mixing and matching different terms but i just don't know

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <stdio.h>
#include "Goalies.h"
#include "Goalies.cpp"
#include <vector>
#include <iomanip>



using namespace std;

#define MAX_GOALIES 100

bool openFileIn(ifstream &, string);
Goalies NHL;
string getName(fstream &, Goalies *);
string getTeam(fstream &, Goalies *);
int getWins(fstream &, Goalies *);
int getLosses(fstream &, Goalies *);
int getGoalsAllowed(fstream &, Goalies *);
int getShotsAgainst(fstream &, Goalies *);
void swap(Goalies &a, Goalies &b);
void sortWins(Goalies wins[], int size);
void sortLosses(Goalies losses[], int size);
void sortGoalsAllowed(Goalies goalsAllowed[], int size);
void sortShotsAgainst(Goalies shotsAgainst[], int size);
int binarySearch(const int array[], int size, int value);
int getNHL(fstream &, Goalies *);
int getGoalies(fstream &, Goalies *);
int showGoalies(fstream &, Goalies *);
void split(const string&, char , vector<string>& );

int main()
{
    // declaration for file
    ifstream dataFile;           // data file
    Goalies NHL[MAX_GOALIES];   // makes NHL the goalies class
    int count;

    if(openFileIn(dataFile, "goalie_stats"))
    {
       // get goalie data from file into array
    Goalies *NHL = new Goalies[MAX_GOALIES];

    int count = get(dataFile, Goalies);


        dataFile.close();

        show (Goalies,count);

        writeGoaliesofstream(Goalies, count);
    }

    else
        cout << "Could not open goalie_stats\n" << endl;

    return 0;


}



// binary search function
int binarySearch(const int array[], int size, int value)
{
   int first = 0,             // First array element
       last = size - 1,       // Last array element
       middle,                // Mid point of search
       position = -1;         // Position of search value
   bool found = false;        // Flag

   while (!found && first <= last)
   {
      middle = (first + last) / 2;     // Calculate mid point
      if (array[middle] == value)      // If value is found at mid
      {
         found = true;
         position = middle;
      }
      else if (array[middle] > value)  // If value is in lower half
         last = middle - 1;
      else
         first = middle + 1;           // If value is in upper half
   }
   return position;
}

// swap function
void swap(Goalies &a, Goalies &b)
{
   Goalies temp = a;
   a = b;
   b = temp;
}

bool openFileIn(fstream &file, string name)
{
   file.open(name.c_str(), ios::in);
   if (file.fail())
      return false;
   else
      return true;
}




||=== Build: Debug in prog3 (compiler: GNU GCC Compiler) ===|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\Goalies.cpp||In constructor 'Goalies::Goalies()':|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\Goalies.cpp|11|warning: statement has no effect [-Wunused-value]|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\Goalies.cpp|12|warning: statement has no effect [-Wunused-value]|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp||In function 'int main()':|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|48|error: expected primary-expression before ')' token|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|53|error: expected primary-expression before ',' token|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|53|error: 'show' was not declared in this scope|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|53|note: suggested alternative: 'short'|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|55|error: expected primary-expression before ',' token|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|55|error: 'writeGoaliesofstream' was not declared in this scope|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|46|warning: unused variable 'NHL' [-Wunused-variable]|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|41|warning: unused variable 'count' [-Wunused-variable]|
||=== Build failed: 5 error(s), 4 warning(s) (0 minute(s), 0 second(s)) ===|

Last edited on
Lines 48, 53, 55: "Goalies" is the name of a class, not a variable name. Read what MikeyBoy said.
You also never use your NHL variable (the pointer to your dynamic array).
It's not even clear what is supposed to be passed into your 'get' function, because you have not shown us even its signature.
Last edited on
Okay so i fixed the problem by youtubing another method to open the file, but now i'm trying to organize the code using the function starting at line 87. I'm receiving an error message saying :

||=== Build: Debug in prog3 (compiler: GNU GCC Compiler) ===|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\Goalies.cpp||In constructor 'Goalies::Goalies()':|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\Goalies.cpp|11|warning: statement has no effect [-Wunused-value]|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\Goalies.cpp|12|warning: statement has no effect [-Wunused-value]|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp||In function 'int getName(std::ifstream&, Goalies*)':|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|108|error: invalid use of member 'std::__cxx11::string Goalies::getName()' (did you forget the '&' ?)|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|109|error: invalid use of member 'std::__cxx11::string Goalies::getTeam()' (did you forget the '&' ?)|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|110|error: invalid use of member 'int Goalies::getWins()' (did you forget the '&' ?)|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|111|error: invalid use of member 'int Goalies::getLosses()' (did you forget the '&' ?)|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|112|error: invalid use of member 'int Goalies::getGoalsAllowed()' (did you forget the '&' ?)|
C:\Users\Chris Valvo\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|113|error: invalid use of member 'int Goalies::getShotsAgainst()' (did you forget the '&' ?)|
||=== Build failed: 6 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <stdio.h>
#include "Goalies.h"
#include "Goalies.cpp"
#include <vector>
#include <iomanip>



using namespace std;

#define MAX_GOALIES 100

bool openFileIn(ifstream &, string);
Goalies NHL;
int getName(ifstream &, Goalies *);
int getTeam(ifstream &, Goalies *);
int getWins(ifstream &, Goalies *);
int getLosses(ifstream &, Goalies *);
int getGoalsAllowed(ifstream &, Goalies *);
int getShotsAgainst(ifstream &, Goalies *);
void swap(Goalies &a, Goalies &b);
void sortWins(Goalies wins[], int size);
void sortLosses(Goalies losses[], int size);
void sortGoalsAllowed(Goalies goalsAllowed[], int size);
void sortShotsAgainst(Goalies shotsAgainst[], int size);
int binarySearch(const int array[], int size, int value);
int getNHL(ifstream &, Goalies *);
int getGoalies(ifstream &, Goalies *);
int showGoalies(ifstream &, Goalies *);
void split(const string&, char , vector<string>& );

int main()
{
    // declaration for file
    ifstream myFile;        // data file
    myFile.open("goalie_stats.csv");

    while(myFile.good()) {
        string line;
        getline(myFile, line, ',');
        cout << line << endl;
    }

    myFile.close();

}



// binary search function
int binarySearch(const int array[], int size, int value)
{
   int first = 0,             // First array element
       last = size - 1,       // Last array element
       middle,                // Mid point of search
       position = -1;         // Position of search value
   bool found = false;        // Flag

   while (!found && first <= last)
   {
      middle = (first + last) / 2;     // Calculate mid point
      if (array[middle] == value)      // If value is found at mid
      {
         found = true;
         position = middle;
      }
      else if (array[middle] > value)  // If value is in lower half
         last = middle - 1;
      else
         first = middle + 1;           // If value is in upper half
   }
   return position;
}

// swap function
void swap(Goalies &a, Goalies &b)
{
   Goalies temp = a;
   a = b;
   b = temp;
}

int getName(ifstream &file, Goalies *w_ptr)
{
    string input;	// To hold file input
    int count = 0;

    // get the header info from the first line in the file
    // This can be saved to a new string or just discarded
    getline(file, input);

    // Read widget data from file using ',' as a delimiter.
    while (getline(file, input)) {

        // vector to hold the tokens.
        vector<string> tokens;

        // Tokenize str1, using ' ' as the delimiter.
        split(input, ',', tokens);

        /*
         * copy the tokens to the widget record
         */
        w_ptr->getName = tokens.at(0);
        w_ptr->getTeam =  tokens.at(1);
        w_ptr->getWins =  atoi(tokens.at(2).c_str());
        w_ptr->getLosses =  atoi(tokens.at(3).c_str());
        w_ptr->getGoalsAllowed = atoi(tokens.at(4).c_str());
        w_ptr->getShotsAgainst = atoi(tokens.at(5).c_str());

        count++;
        w_ptr++;
        cout << endl;
    }
#if DEBUG
    printf("Read %d widgets from file into records\n", count);
#endif

    return count;
}
I'm trying to model it after this code, but i'm confused what to do next after successfully opening the file. I'll show my output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

fstream dataFile;                   // data file
    Widget *widgets = new Widget[MAX_WIDGETS];     // dynamically allocate widget records
    int count;

	if (openFileIn(dataFile, "widgets.dat"))
	{
        // get widget data from file into array
    	count = getWidgets(dataFile, widgets);

    	dataFile.close();

#if DEBUG
        showWidgets(widgets, count);
#endif
        writeWidgetsOfstream(widgets, count);
	}
	else
       cout << "File open error!" << endl;

	return 0;
}
Last edited on
I'm basically trying to sort it by header into columns, (player name, team, wins, losses, goals allowed, shots against)


Player
Tm
W
L
GA
SA
Jake Allen
STL
19
17
121
1277
Frederik Andersen
TOR
36
16
162
1958
Craig Anderson
OTT
17
27
163
1676
Richard Bachman
VAN
0
1
6
29
Jonathan Bernier
DET
9
18
98
1025
Jordan Binnington
STL
24
5
59
807
Ben Bishop
DAL
27
15
87
1323
Mackenzie Blackwood
NJD
10
10
55
669
Sergei Bobrovsky
CBJ
37
24
153
1756
Landon Bow
DAL
0
0
1
19
Kevin Boyle
ANA
1
3
10
139
Laurent Brossoit
WPG
13
6
49
652
Peter Budaj
LAK
0
1
6
33
Jack Campbell
LAK
10
14
61
845
Eric Comrie
WPG
0
1
5
28
Mike Condon
OTT
0
2
8
40
Pheonix Copley
WSH
16
7
74
776
Corey Crawford
CHI
14
18
108
1176
Joey Daccord
OTT
0
1
5
40
Scott Darling
CAR
2
4
27
233
Collin Delia
CHI
6
4
50
545
Aaron Dell
SJS
10
8
70
613
Thatcher Demko
VAN
4
3
25
288
Casey DeSmith
PIT
15
11
89
1060
Michael Dipietro
VAN
0
1
7
24
Louis Domingue
TBL
21
5
75
812
Devan Dubnyk
MIN
31
28
163
1877
Brian Elliott
PHI
11
11
69
741
Marc-Andre Fleury
VEG
35
21
152
1745
Pavel Francouz
COL
0
2
2
35
Kaden Fulcher
DET
0
0
2
11
Alexandar Georgiev
NYR
14
13
91
1057
Christopher Gibson
NYI
0
0
1
17
John Gibson
ANA
26
22
153
1838
Thomas Greiss
NYI
23
14
87
1185
Philipp Grubauer
COL
18
9
89
1071
Jaroslav Halak
BOS
22
11
90
1158
Carter Hart
PHI
16
13
81
976
Connor Hellebuyck
WPG
34
23
179
2051
Adin Hill
ARI
7
5
32
322
Marcus Hogberg
OTT
0
2
14
121
Braden Holtby
WSH
32
19
160
1795
Jimmy Howard
DET
23
22
156
1709
Michael Hutchinson
TOT
3
4
27
239
Carter Hutton
BUF
18
25
142
1541
Tristan Jarry
PIT
0
1
7
62
Chad Johnson
TOT
2
11
49
406
Martin Jones
SJS
36
19
176
1699
Anton Khudobin
DAL
16
17
95
1232
Keith Kinkaid
NJD
15
18
129
1188
Joonas Korpisalo
CBJ
10
7
67
651
Mikko Koskinen
EDM
25
21
146
1551
Darcy Kuemper
ARI
27
20
126
1676
Maxime Lagace
VEG
0
1
4
31
Robin Lehner
NYI
25
13
93
1323
Charlie Lindgren
MTL
1
0
5
49
Henrik Lundqvist
NYR
18
23
158
1699
Roberto Luongo
FLA
18
16
122
1205
Alex Lyon
PHI
0
1
6
31
Jacob Markstrom
VAN
28
23
166
1896
Curtis McElhinney
CAR
20
11
85
968
Mike McKenna
TOT
1
5
34
316
Ryan Miller
ANA
8
7
51
578
Hunter Miska
ARI
0
0
1
9
Sam Montembeault
FLA
4
3
30
282
Petr Mrazek
CAR
23
14
95
1104
Matt Murray
PIT
29
14
129
1594
Alex Nedeljkovic
CAR
1
0
2
26
Michal Neuvirth
PHI
1
4
26
184
Antti Niemi
MTL
8
6
61
539
Anders Nilsson
TOT
14
19
101
1098
Eddie Pasquale
TBL
2
1
12
102
Calvin Petersen
LAK
5
4
27
355
Calvin Pickard
TOT
4
6
48
384
Carey Price
MTL
35
24
161
1952
Jonathan Quick
LAK
16
23
149
1329
Antti Raanta
ARI
5
6
33
351
Tuukka Rask
BOS
27
13
109
1245
James Reimer
FLA
13
12
93
929
Pekka Rinne
NSH
30
19
130
1581
David Rittich
CGY
27
9
109
1228
Juuse Saros
NSH
17
10
74
870
Cory Schneider
NJD
6
13
70
718
Mike Smith
CGY
23
16
109
1069
Garret Sparks
TOR
8
9
58
592
Alex Stalock
MIN
6
8
53
525
Anthony Stolarz
TOT
4
5
50
503
Malcolm Subban
VEG
8
10
60
612
Cam Talbot
TOT
11
17
108
997
Linus Ullmark
BUF
15
14
109
1146
Semyon Varlamov
COL
20
19
136
1496
Andrei Vasilevskiy
TBL
39
10
128
1713
Cam Ward
CHI
16
12
115
1113


Process returned 0 (0x0)   execution time : 0.406 s
Press any key to continue.
Last edited on
You haven't provided the code for spilt(), so can't test. The compiler errors are due to using getName rather than setName() etc - lines 108 - 113.

The compiles OK - code for split() needs to be provided:

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <string>

using namespace std;

class Goalies
{
public:
	Goalies();
	Goalies(string n, string t);
	Goalies(string n, string t, int w, int l, int ga, int sa);

	// constructors
	~Goalies();

	// implement setters

	void setName(string n);
	void setTeam(string t);
	void setWins(int w);
	void setLosses(int l);
	void setGoalsAllowed(int ga);
	void setShotsAgainst(int sa);

	// implement getters

	string getName();
	string getTeam();
	int getWins();
	int getLosses();
	int getGoalsAllowed();
	int getShotsAgainst();
	double getSavePct();

private:

	//attributes
	string name;
	string team;
	int wins;
	int losses;
	int goalsAllowed;
	int shotsAgainst;
};

// no args constructor
Goalies::Goalies()
{

	name = "";
	team = "";
	wins = 0;
	losses = 0;
	goalsAllowed;
	shotsAgainst;

}

Goalies::Goalies(string n, string t)
{
	name = "";
	team = "";
	wins = 0;
	losses = 0;
	goalsAllowed = 0;
	shotsAgainst = 0;
}

Goalies::Goalies(string n, string t, int w, int l, int ga, int sa)
{
	name = "";
	team = "";
	wins = w;
	losses = l;
	goalsAllowed = ga;
	shotsAgainst = sa;
}


Goalies::~Goalies()
{

}

void Goalies::setName(string n)
{
	name = n;
}

void Goalies::setTeam(string t)
{
	team = t;
}

void Goalies::setWins(int w)
{
	wins = w;
}

void Goalies::setLosses(int l)
{
	losses = l;
}

void Goalies::setGoalsAllowed(int ga)
{
	goalsAllowed = ga;
}

void Goalies::setShotsAgainst(int sa)
{
	shotsAgainst = sa;
}



string Goalies::getName()
{
	return name;
}

string Goalies::getTeam()
{
	return team;
}

int Goalies::getWins()
{
	return wins;
}

int Goalies::getLosses()
{
	return losses;
}

int Goalies::getGoalsAllowed()
{
	return goalsAllowed;
}

int Goalies::getShotsAgainst()
{
	return shotsAgainst;
}


#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <stdio.h>
//#include "Goalies.h"
//#include "Goalies.cpp"
#include <vector>
#include <iomanip>


using namespace std;

#define MAX_GOALIES 100

bool openFileIn(ifstream&, string);
Goalies NHL;
int getName(ifstream&, Goalies*);
int getTeam(ifstream&, Goalies*);
int getWins(ifstream&, Goalies*);
int getLosses(ifstream&, Goalies*);
int getGoalsAllowed(ifstream&, Goalies*);
int getShotsAgainst(ifstream&, Goalies*);
void swap(Goalies& a, Goalies& b);
void sortWins(Goalies wins[], int size);
void sortLosses(Goalies losses[], int size);
void sortGoalsAllowed(Goalies goalsAllowed[], int size);
void sortShotsAgainst(Goalies shotsAgainst[], int size);
int binarySearch(const int array[], int size, int value);
int getNHL(ifstream&, Goalies*);
int getGoalies(ifstream&, Goalies*);
int showGoalies(ifstream&, Goalies*);
void split(const string&, char, vector<string>&);

int main()
{
	// declaration for file
	ifstream myFile;        // data file
	myFile.open("goalie_stats.csv");

	while (myFile.good()) {
		string line;
		getline(myFile, line, ',');
		cout << line << endl;
	}

	myFile.close();

}



// binary search function
int binarySearch(const int array[], int size, int value)
{
	int first = 0,             // First array element
		last = size - 1,       // Last array element
		middle,                // Mid point of search
		position = -1;         // Position of search value
	bool found = false;        // Flag

	while (!found && first <= last)
	{
		middle = (first + last) / 2;     // Calculate mid point
		if (array[middle] == value)      // If value is found at mid
		{
			found = true;
			position = middle;
		} else if (array[middle] > value)  // If value is in lower half
			last = middle - 1;
		else
			first = middle + 1;           // If value is in upper half
	}
	return position;
}

// swap function
void swap(Goalies& a, Goalies& b)
{
	Goalies temp = a;
	a = b;
	b = temp;
}

int getName(ifstream& file, Goalies* w_ptr)
{
	string input;	// To hold file input
	int count = 0;

	// get the header info from the first line in the file
	// This can be saved to a new string or just discarded
	getline(file, input);

	// Read widget data from file using ',' as a delimiter.
	while (getline(file, input)) {

		// vector to hold the tokens.
		vector<string> tokens;

		// Tokenize str1, using ' ' as the delimiter.
		split(input, ',', tokens);

		/*
		 * copy the tokens to the widget record
		 */
		w_ptr->setName(tokens.at(0));
		w_ptr->setTeam(tokens.at(1));
		w_ptr->setWins(atoi(tokens.at(2).c_str()));
		w_ptr->setLosses(atoi(tokens.at(3).c_str()));
		w_ptr->setGoalsAllowed(atoi(tokens.at(4).c_str()));
		w_ptr->setShotsAgainst(atoi(tokens.at(5).c_str()));

		count++;
		w_ptr++;
		cout << endl;
	}
#if DEBUG
	printf("Read %d widgets from file into records\n", count);
#endif

	return count;
}

void split(const string&, char, vector<string>&)
{
	// Function code goes here
}

Here's the code w my split function, sorry i forgot to include 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <stdio.h>
#include "Goalies.h"
#include "Goalies.cpp"
#include <vector>
#include <iomanip>



using namespace std;

#define MAX_GOALIES 100

bool openFileIn(ifstream &, string);
Goalies NHL;
int getName(ifstream &, Goalies *);
int getTeam(ifstream &, Goalies *);
int getWins(ifstream &, Goalies *);
int getLosses(ifstream &, Goalies *);
int getGoalsAllowed(ifstream &, Goalies *);
int getShotsAgainst(ifstream &, Goalies *);
void swap(Goalies &a, Goalies &b);
void sortWins(Goalies wins[], int size);
void sortLosses(Goalies losses[], int size);
void sortGoalsAllowed(Goalies goalsAllowed[], int size);
void sortShotsAgainst(Goalies shotsAgainst[], int size);
int binarySearch(const int array[], int size, int value);
int getNHL(ifstream &, Goalies *);
int getGoalies(ifstream &, Goalies *);
int showGoalies(ifstream &, Goalies *);
void split(const string&, char , vector<string>& );

int main()
{
    // declaration for file
    ifstream myFile;        // data file
    myFile.open("goalie_stats.csv");

    // Displays contents
    while(myFile.good()) {
        string line;
        getline(myFile, line, ',');
        cout << line << endl;
    }

    // get contents & put them into an array


    // show contents


    myFile.close();

}



// binary search function
int binarySearch(const int array[], int size, int value)
{
   int first = 0,             // First array element
       last = size - 1,       // Last array element
       middle,                // Mid point of search
       position = -1;         // Position of search value
   bool found = false;        // Flag

   while (!found && first <= last)
   {
      middle = (first + last) / 2;     // Calculate mid point
      if (array[middle] == value)      // If value is found at mid
      {
         found = true;
         position = middle;
      }
      else if (array[middle] > value)  // If value is in lower half
         last = middle - 1;
      else
         first = middle + 1;           // If value is in upper half
   }
   return position;
}

// swap function
void swap(Goalies &a, Goalies &b)
{
   Goalies temp = a;
   a = b;
   b = temp;
}

int getName(ifstream &file, Goalies *w_ptr)
{
    string input;	// To hold file input
    int count = 0;


    // get the header info from the first line in the file
    // This can be saved to a new string or just discarded
    getline(file, input);

    // Read widget data from file using ',' as a delimiter.
    while (count < MAX_GOALIES && getline(file, input)) {

        // vector to hold the tokens.
        vector<string> tokens;

        // Tokenize str1, using ' ' as the delimiter.
        split(input, ',', tokens);

        /*
         * copy the tokens to the widget record
         */
        w_ptr->getName = tokens.at(0);
        w_ptr->getTeam =  tokens.at(1);
        w_ptr->getWins =  atoi(tokens.at(2).c_str());
        w_ptr->getLosses =  atoi(tokens.at(3).c_str());
        w_ptr->getGoalsAllowed = atoi(tokens.at(4).c_str());
        w_ptr->getShotsAgainst = atoi(tokens.at(5).c_str());

        count++;
        w_ptr++;
        cout << endl;
    }
#if DEBUG
    printf("Read %d widgets from file into records\n", count);
#endif

    return count;
}

void split(const string& s, char delim, vector<string>& tokens)
{
   int tokenStart = 0;  // Starting position of the next token

   // Find the first occurrence of the delimiter.
   int delimPosition = s.find(delim);

   // While we haven't run out of delimiters...
   while (delimPosition != string::npos)
   {
      // Extract the token.
      string tok = s.substr(tokenStart, delimPosition - tokenStart);

      // Push the token onto the tokens vector.
      tokens.push_back(tok);

      // Move delimPosition to the next character position.
      delimPosition++;

      // Move tokenStart to delimPosition.
      tokenStart = delimPosition;

      // Find the next occurrence of the delimiter.
      delimPosition = s.find(delim, delimPosition);

      // If no more delimiters, extract the last token.
      if (delimPosition == string::npos)
      {
         // Extract the token.
         string tok = s.substr(tokenStart, delimPosition - tokenStart);

         // Push the token onto the vector.
         tokens.push_back(tok);
      }
   }
}
i tried set alternatively and received the same error, i tried putting set and get at the top where int is?

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
int setName(ifstream &file, Goalies *w_ptr)
{
    string input;	// To hold file input
    int count = 0;


    // get the header info from the first line in the file
    // This can be saved to a new string or just discarded
    getline(file, input);

    // Read widget data from file using ',' as a delimiter.
    while (count < MAX_GOALIES && getline(file, input)) {

        // vector to hold the tokens.
        vector<string> tokens;

        // Tokenize str1, using ' ' as the delimiter.
        split(input, ',', tokens);

        /*
         * copy the tokens to the widget record
         */
        w_ptr->setName = tokens.at(0);
        w_ptr->setTeam =  tokens.at(1);
        w_ptr->setWins =  atoi(tokens.at(2).c_str());
        w_ptr->setLosses =  atoi(tokens.at(3).c_str());
        w_ptr->setGoalsAllowed = atoi(tokens.at(4).c_str());
        w_ptr->setShotsAgainst = atoi(tokens.at(5).c_str());

        count++;
        w_ptr++;
        cout << endl;
    }
#if DEBUG
    printf("Read %d widgets from file into records\n", count);
#endif

    return count;
}

Last edited on
You're using set wrong. See my previous post code lines 252-257.
Last edited on
Pages: 12