Opened csv and added functions, but unsure how to incorporate them into the code

Pages: 123
Hello, I had some users on here help w me with some things on here, if you're reading this and helped already or are planning to, thank you very much.

I got the file to open, now i'm trying to actually insert the headers and sort it so that the user can search the goalie data. I left some comments in main where i'm having trouble(49-60). I'll also add my .h file and .cpp, and i'm getting a bunch of warnings so maybe fixing those first is a good idea? I'm just unsure as to what needs fixing.

The professor also requested that we use 3 files in the program in case you're wondering why (main, .cpp and .h)

All errors/warnings at the bottom

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
 #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 **unsorted and in a vertical fashion, headers are at very top of the running program.
    while(myFile.good()) {
        string line;
        getline(myFile, line, ',');
        cout << line << endl;
    }

    // insert headers, "Player, Team, Wins, Losses, Goals Allowed, Shots Against, Save Percentage"


    // show contents again but sorted under headers

    // add save percentage statement, **double savePct = (double)numberOfSaves / shotsAgainst; **numberOfSaves = shotsAgainst – goalsAgainst;


    // use a sort function to sort by 1)wins, 2)losses, and 3)save percentage


    // prompt user to search the sorted values, 1) = wins, 2) losses, and 3) save percentage then rinse n repeat


    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->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& 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);
      }
   }
}

int showName(Goalies *w_ptr, int count)
{
    /* Display the headings for each column, with spacing and justification flags */
    printf("%s%15s%25s%8s%20%15\n", "Name", "Team", "Wins", "Losses", "Goals Allowed", "Shots Against");

    // Read widget data from file using ',' as a delimiter.
    for (int i = 0; i < count; i++) {

        if (!w_ptr->getName().empty()) {
            printf("%-8d%-30s%-10d%-2.2f%-15%-20,\n",
                    w_ptr->getName().c_str(),
                    w_ptr->getTeam().c_str(),
                    w_ptr->getWins(),
                    w_ptr->getLosses());

            w_ptr++;
        }
        /* If the Name field is zero, then we reached the last player, break
         * out of the for loop.
         */
        else
            break;
    }
}
Last edited on
output:


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.636 s
Press any key to continue.
.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
103
#include "Goalies.h"

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

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

}

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;
}
.h

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
#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 
Last edited on
Errors:

||=== Build: Debug in prog3 (compiler: GNU GCC Compiler) ===|
C:\Users\Chris \Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\Goalies.cpp||In constructor 'Goalies::Goalies()':|
C:\Users\Chris \Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\Goalies.cpp|11|warning: statement has no effect [-Wunused-value]|
C:\Users\Chris \Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\Goalies.cpp|12|warning: statement has no effect [-Wunused-value]|
C:\Users\Chris \Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp||In function 'void split(const string&, char, std::vector<std::__cxx11::basic_string<char> >&)':|
C:\Users\Chris \Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|142|warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long long unsigned int'} [-Wsign-compare]|
C:\Users\Chris \Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|160|warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long long unsigned int'} [-Wsign-compare]|
C:\Users\Chris \Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp||In function 'int showName(Goalies*, int)':|
C:\Users\Chris \Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|174|warning: conversion lacks type at end of format [-Wformat=]|
C:\Users\Chris \Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|174|warning: unknown conversion type character '\x0a' in format [-Wformat=]|
C:\Users\Chris \Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|174|warning: too many arguments for format [-Wformat-extra-args]|
C:\Users\Chris \Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|180|warning: format '%d' expects argument of type 'int', but argument 2 has type 'const char*' [-Wformat=]|
C:\Users\Chris \Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|180|warning: format '%f' expects argument of type 'double', but argument 5 has type 'int' [-Wformat=]|
C:\Users\Chris \Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|180|warning: conversion lacks type at end of format [-Wformat=]|
C:\Users\Chris \Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|180|warning: unknown conversion type character ',' in format [-Wformat=]|
C:\Users\Chris \Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|194|warning: no return statement in function returning non-void [-Wreturn-type]|
||=== Build finished: 0 error(s), 12 warning(s) (0 minute(s), 1 second(s)) ===|
||=== Run: Debug in prog3 (compiler: GNU GCC Compiler) ===|
||=== Run: Debug in prog3 (compiler: GNU GCC Compiler) ===|
Last edited on
i'm getting a bunch of warnings so maybe fixing those first is a good idea?

Yes, a very good idea. In fact until you're very familiar with the language you should never run a program that has warnings, always fix them first.

Also #including a source file is usually a very bad idea, you should add the source files to your project, not #include them.


Yes, a very good idea. In fact until you're very familiar with the language you should never run a program that has warnings, always fix them first.

Also #including a source file is usually a very bad idea, you should add the source files to your project, not #include them.


I was able to fix one warning so far (194- no return type)

Well starting from the beginning, the first warning is in my .cpp file(line 11-12) in my no args constructor, why does GoalsAllowed & ShotsAgainst have no effect, but the ones right before them do?
Last edited on
I also just added line 8 for the headers in my program, but i feel like this isn't gonna work or is very inefficient since i already made functions, i just didn't know how to use em, so i just used cout to add 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
int main()
{
    // declaration for file
    ifstream myFile;        // data file
    myFile.open("goalie_stats.csv");

    // insert headers, "Player, Team, Wins, Losses, Goals Allowed, Shots Against, Save Percentage"
    cout << "\t\t"  << "Players"  << "\t"  << " Team"  << "\t" << "Wins" << "\t" << "Losses"  << "\t" << "Goals Allowed" << "\t"  << "Shots Against"  << "\t" << "Save Percentage" << "\t" <<endl;

    // Displays contents **unsorted and in a vertical fashion, headers are at very top of the running program.
    while(myFile.good()) {
        string line;
        getline(myFile, line, ',');
        cout << line << endl;
    }



    // show contents again but sorted under headers

    // add save percentage statement, **double savePct = (double)numberOfSaves / shotsAgainst; **numberOfSaves = shotsAgainst – goalsAgainst;


    // use a sort function to sort by 1)wins, 2)losses, and 3)save percentage


    // prompt user to search the sorted values, 1) = wins, 2) losses, and 3) save percentage then rinse n repeat


    myFile.close();

}
Last edited on
This is what that adjustment does ^^


 Players  Team   Wins    Losses  Goals Allowed   Shots Against   Save Percentage
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 : 2.739 s
Press any key to continue.
i'm getting a bunch of warnings so maybe fixing those first is a good idea?


Probably the best idea you've had all day. Never run a program that contains warnings, until you are much more familiar with the language.

You should also never use the "using" statement inside a header file, learn to properly scope the items instead, ie:std::string.

Also #including a source file is a horribly bad practice. You should be adding the source file to your project/build line instead.

Several of your warnings have to do with mistreating the C function printf(), I recommend you stick with C++ streams instead of playing with the C-stdio functions.

Be careful to insure that you are using the correct types in your comparisons/assignments. In some places you are tying to compare/assign a signed int to an unsigned type which could cause overflows.

By the way a std::string::npos is a std::string::size_type, which is an implementation defined unsigned type, usually either an unsigned long or an unsigned int.

Your error messages tell you the lines of the problem codes so you should be able to see where the problems are detected.

C:\Users\Chris\Dropbox\My PC (DESKTOP-1NVD3NT)\Documents\c++\prog3\main.cpp|152|warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long long unsigned int'} [-Wsign-compare]|

line:152 - so how do i go about fixing these errors, i understand it says the data typed don't match in signedness, but i'm not sure what to change it too
Last edited on
You need to change the type of the smaller type to match the type of the larger type. Since a signed int the smaller size you need to change it to the same type as the larger (a std::string::size_type).

Now let's look at one of the lines in question:
int delimPosition = s.find(delim);
You need to look up the std::string find function to see what type of variable is being returned.

Since std::string::find() function returns a std::string::size_type you will need to change that int to a std::string::size_type.

The error message does provides an answer.
comparison of integer expressions of different signedness: 'int' and std::__cxx11::basic_string<char>::size_type'

(std::__cxx11::basic_string<char> is the base for std::string)

The message also provides some information on the size type:
std::string::size_type is an alias for an unsigned long long with your compiler.

Better to use std::string::size_type instead of unsigned long long:

1. an update to your compiler could change what std::string::size_type is.

2. other compilers might implement std::string::size_type differently.
auto is your friend when defining and initialising variables.

 
auto delimPosition = s.find(delim);


here delimPosition will automatically have the correct type as returned by .find()

Thanks for the help guys, i fixed all warnings
Last edited on
So this is my code now, my column names are being displayed at the end of my program, instead of the top, and the names, team, other stats etc... are just not organizing my info, i'll show and example, can someone explain why the player stats are not getting sorted under the columms?

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
#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);
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>& );
void showGoalies(Goalies *NHL, int count);
void sortWins(Goalies NHL[], int size);
void sortLosses(Goalies NHL[], int size);
void sortSavePct(Goalies NHL[], int size);

int main()
{
    // declaration for file
    Goalies *NHL = new Goalies[MAX_GOALIES];
    int count = 0;
    ifstream myFile;        // data file
    myFile.open("goalie_stats.csv");




    // Displays contents **unsorted and in a vertical fashion, headers are at very top of the running program.
    while(myFile.good()) {
        string line;
        getline(myFile, line, ',');
        cout << line << endl;
    }



    // show contents again but sorted under headers

    showGoalies(NHL, count);
    // add save percentage statement, **double savePct = (double)numberOfSaves / shotsAgainst; **numberOfSaves = shotsAgainst – goalsAgainst;


    // use a sort function to sort by 1)wins, 2)losses, and 3)save percentage
    sortWins(NHL, count);

    // prompt user to search the sorted values, 1) = wins, 2) losses, and 3) save percentage then rinse n repeat


    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;
}

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.
   auto 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);
      }
   }
}

void showGoalies(Goalies *NHL, int count)
{
    cout << left;

    cout << setw(15) << "Name" << setw(10) << "Team" << setw(10) << "Wins" << setw(10) << "Losses"
     << setw(10) << "Goals allowed" << setw(10) << "  Shots against" << setw(10) << "  Save Percentage" << endl;
    for(int i = 0; i < count; i++) {
        cout << setw(15) << NHL->getName();
        cout << setw(10) << NHL->getTeam();
        cout << setw(10) << NHL->getWins();
        cout << setw(10) << NHL->getLosses();
        cout << setw(10) << NHL->getGoalsAllowed();
        cout << setw(10) << NHL->getShotsAgainst();
        cout << endl;
        NHL++;
    }

    cout << setprecision(3) << (double)1796/1958 << endl;
}

// The sortWins function sorts an InventoryItem array in descending order. *
//*********************************************************************************
void sortWins(Goalies NHL[], int size)
{
   int maxIndex, maxValue;

   for (int start = 0; start < (size - 1); start++)
   {
      maxIndex = start;
      maxValue = NHL[start].getWins();
      for (int index = start + 1; index < size; index++)
      {
         if (NHL[index].getWins() > maxValue)
         {
            maxValue = NHL[index].getWins();
            maxIndex = index;
         }
      }
      swap(NHL[maxIndex], NHL[start]);
   }
}

void sortLosses(Goalies NHL[], int size)
{
   int maxIndex, maxValue;

   for (int start = 0; start < (size - 1); start++)
   {
      maxIndex = start;
      maxValue = NHL[start].getLosses();
      for (int index = start + 1; index < size; index++)
      {
         if (NHL[index].getLosses() > maxValue)
         {
            maxValue = NHL[index].getLosses();
            maxIndex = index;
         }
      }
      swap(NHL[maxIndex], NHL[start]);
   }
}
Here's a portion of it, so you don't have to scroll all the way through lol



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

Name           Team      Wins      Losses    Goals allowed  Shots against  Save Percentage
0.917

Process returned 0 (0x0)   execution time : 2.005 s
Press any key to continue.
Last edited on
Please post a small sample of your input file.

can someone explain why the player stats are not getting sorted under the columms?

Probably because you're not storing the information from the file into your array?

By the way you seem to be aware of std::vector, why aren't you using it instead of the array?

Also be careful, you have created a function named swap(), a function with this name already exists in the standard name space. And since you're using a lot of standard headers, and the horrible "using namespace std;" clause it is hard to tell what function the compiler is picking for the calls.

1
2
3
4
5
6
 // Displays contents **unsorted and in a vertical fashion, headers are at very top of the running program.
    while(myFile.good()) {
        string line;
        getline(myFile, line, ',');
        cout << line << endl;
    }


This is the only code that attempts to read the file. However, this just displays it and doesn't store or otherwise process it. It's also using getline() incorrectly as the status could be good before but fail afterwards.

1
2
for (string line; getline(myFile, line,  ','); )
    cout << line << '\n';


However you need something like (not tried):

1
2
3
4
5
6
7
8
9
10
for (string line; getline(myFile, line); ) {
    vector<string> vs;
    Goalies g;

    split(line, ',', vs);

    // update g from vs here

   NHL[count++] = g;
}

Pages: 123