Trying to search for a players info within a score keeping log

I am trying to write code to enter in several Players names and scores, I got that part down. The next part is that I have to be able to search for a player and have their name and scores displayed. This is that part I am having trouble with. I am getting an error of 'iter' was not declared.

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
  #include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>

using namespace std;
class Player
{
    public:
    Player(string name, int scores[])
    {
        this->name = name;
        this->scores[0] = scores[0];
        this->scores[1] = scores[1];
        this->scores[2] = scores[2];
    }

    string getName() {return name;}
    int* getScores() {return scores;}

    private:
    string name;
    int scores[3];
};

Player* createPlayer()
{
    string name;
    int scores[3];
    cout<<"\n Player Name:";
    cin  >> name;
    cout<<"score 1:";
    cin  >> scores[0];
    cout<<"score 2:";
    cin  >> scores[1];
    cout<<"score 3:";
    cin  >> scores[2];
    return new Player(name, scores);
}


int main(void)
{

char selection;
std::vector<Player*> players ;

while (selection != 'E')
{

    cout<<"\n Menu";
    cout<<"\n========";
    cout<<"\n A - Add player information";
    cout<<"\n S - Search for a player";
    cout<<"\n D - Display all info";
    cout<<"\n E - Exit";
    cout<<"\n Enter selection: ";

    cin>>selection;

    switch(selection)
    {
        case 'A' :
        {
            cout<<"\n To add player information\n";
            Player* player = createPlayer();
            players.push_back(player);
            cout<<"\n Player added: size=" << players.size() << endl;
            break;
        }
        case 'S' :
        {
            cout<<"\n To search for a players information";
            if (iter !=Player())
            {
                cout<< "Player found.\n";
            }
            else
            {
                cout<< "Player not found. \n";
            }
            break;
        }
        case 'D' :
        {
            cout<<"\n To display all records";
            break;
        }
        case 'E' :
        {
            cout<<"\n To exit the menu";
            break;
        }
        default : cout<<"\n Invalid selection";
    }

    cout<<"\n";
}

system("pause");
return 0;

}
Use value semantics; have a vector of values (of type player).

Something like this, perhaps:

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
#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <cctype>

struct player
{
    std::string name_ ;

    // https://en.cppreference.com/w/cpp/container/array
    static constexpr std::size_t NUM_SCORES = 3 ;
    std::array< int, NUM_SCORES > scores_ ;

    const std::string& name() const { return name_ ; }
    const std::array< int, NUM_SCORES >& scores() const { return scores_ ; }

    void print() const
    {
        std::cout << "name: " << name() << "  scores: [ " ;
        // range based loop: http://www.stroustrup.com/C++11FAQ.html#for
        for( int s : scores() ) std::cout << s << ' ' ;
        std::cout << "]\n" ;
    }

    // return true if there is a match for the player's name (ignoring case)
    bool has_name( const std::string& required_name ) const
    {
        if( name().size() != required_name.size() ) return false ;

        // check char by char, ignoring case; return false on mismatch
        for( std::size_t i = 0 ; i < name().size() ; ++i )
        {
            // https://en.cppreference.com/w/cpp/string/byte/toupper
            const unsigned char a = name()[i] ;
            const unsigned char b = required_name[i] ;
            if( std::toupper(a) != std::toupper(b) ) return false ;
        }

        return true ;
    }
};

// search for a player by name, print the scores of the player
bool print_scores_of( const std::vector<player>& players, const std::string& player_name )
{
    for( const player& p : players ) // for each player in the vector
    {
        if( p.has_name(player_name) ) // if there is a match for the name
        {
            p.print() ;
            return true ;
        }
    }

    std::cout << "there is no player with name '" << player_name << "'\n" ;
    return false ;
}

int main()
{
    const std::vector<player> players { { "abcd", {{1,2,3}} }, { "efgh", {{4,5,6}} }, { "ijkl", {{7,8,9}} } } ;

    std::string name ;
    while( std::cin >> name )
    {
        std::cout << "\nsearching for name '" << name << "'\n" ;
        print_scores_of( players, name ) ;
    }
}

http://coliru.stacked-crooked.com/
Less errors ;)

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
#include <iostream>
#include <array>
#include <string>
#include <map>

struct Player {
    static constexpr std::size_t NUM_SCORES = 3;
    std::array<int, NUM_SCORES> scores;
};

std::ostream& operator<<(std::ostream& s, const Player& player) {
    for(auto score : player.scores) {
        s << score << " ";
    }
    return s;
}

// search for a player by name, print the scores of the player
bool print_scores_of(const std::map<std::string,Player>& players, const std::string& name) {
    auto res = players.find(name);
    if(res != players.end()) {
        const Player& player = res->second;
        std::cout << "name: '" << res->first << "' score: " << player << "\n";
        return true;
    } else {
        std::cout << "there is no player with name '" << name << "'\n" ;
        return false;
    }
}

int main() {
    std::map<std::string, Player> players;
    players["abcd"] = Player{{1,2,3}};
    players["efgh"] = Player{{4,5,6}};
    players["ijkl"] = Player{{7,8,9}};

    std::cout << "Input Player name: ";
    std::string name ;
    while(std::cin >> name) {
        std::cout << "\nsearching for name '" << name << "'\n" ;
        print_scores_of(players, name);
    }
}
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 <vector>
#include <string>
using namespace std;


struct Player
{
   string name;
   vector<int> scores;
};


Player &operator << ( Player &P, int i )
{
   P.scores.push_back( i );
   return P;
}


bool printScores( string name, vector<Player> &all )
{
   for ( Player &p : all )
   {
      if ( p.name == name ) 
      {
         cout << name << ": ";
         for ( int i : p.scores ) cout << i << " ";
         cout << '\n';
         return true;
      }
   }
   cout << name << " not found\n";
   return false;
}


int main()
{
   vector<Player> countries = { { "England" }, { "Wales" }, { "New Zealand" }, { "South Africa" } };
   countries[0] << 35 << 45 << 39;
   countries[1] << 43 << 29 << 29 << 33;
   countries[2] << 23 << 63 << 71;
   countries[3] << 13 << 57 << 49 << 66;
   string name;

   while ( true )
   {
      cout << "Input a country (or just return): ";   getline( cin, name );
      if ( name == "" ) break;
      printScores( name, countries );
   }

   cout << "\nAdded quarter-final scores\n\n";
   countries[0] << 40;
   countries[1] << 20;
   countries[2] << 41;
   countries[3] << 26;
   while ( true )
   {
      cout << "Input a country (or just return): ";   getline( cin, name );
      if ( name == "" ) break;
      printScores( name, countries );
   }
}


Input a country (or just return): England
England: 35 45 39 
Input a country (or just return): Wales
Wales: 43 29 29 33 
Input a country (or just return): 

Added quarter-final scores

Input a country (or just return): England
England: 35 45 39 40 
Input a country (or just return): Wales
Wales: 43 29 29 33 20 
Input a country (or just return): United States
United States not found
Input a country (or just return): 
Please forgive my ignorance. I am very new to this. I tried to incorporate some of the suggestions into my code, but I am getting errors. I have looked online and it looks like I might be missing a curly brace somewhere, but cannot seem to narrow it down.

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
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <array>
#include <string>
#include <map>

using namespace std;
class Player
{
    public:
    Player(string name, int scores[])
    {
        this->name = name;
        this->scores[0] = scores[0];
        this->scores[1] = scores[1];
        this->scores[2] = scores[2];
    }

    string getName() {return name;}
    int* getScores() {return scores;}

    private:
    string name;
    int scores[3];
};

Player* createPlayer()
{
    string name;
    int scores[3];
    cout<<"\n Player Name:";
    cin  >> name;
    cout<<"score 1:";
    cin  >> scores[0];
    cout<<"score 2:";
    cin  >> scores[1];
    cout<<"score 3:";
    cin  >> scores[2];
    return new Player(name, scores);
}


int main(void)
{

char selection;
std::vector<Player*> players ;

while (selection != 'E')
{

    cout<<"\n Menu";
    cout<<"\n========";
    cout<<"\n A - Add player information";
    cout<<"\n S - Search for a player";
    cout<<"\n D - Display all info";
    cout<<"\n E - Exit";
    cout<<"\n Enter selection: ";

    cin>>selection;

    switch(selection)
    {
        case 'A' :
        {
            cout<<"\n To add player information\n";
            Player* player = createPlayer();
            players.push_back(player);
            cout<<"\n Player added: size=" << players.size() << endl;
            break;
        }
                    // search for a player by name, print the scores of the player
        case 'S' :
        {
            cout<<"\n To search for a players information";
    bool print_scores_of(const std::map<std::string,Player>& players, const std::string& name)
{
    auto res = players.find(name);
    if(res != players.end()) {
        const Player& player = res->second;
        std::cout << "name: '" << res->first << "' score: " << player << "\n";
        return true;
    } else {
        std::cout << "there is no player with name '" << name << "'\n" ;
        return false;
    }
}

int main() {
    std::map<std::string, Player> players;
    players["abcd"] = Player{{1,2,3}};
    players["efgh"] = Player{{4,5,6}};
    players["ijkl"] = Player{{7,8,9}};

    std::cout << "Input Player name: ";
    std::string name ;
    while(std::cin >> name) {
        std::cout << "\nsearching for name '" << name << "'\n" ;
        print_scores_of(players, name);
    }
}
            break;
        }
        case 'D' :
        {
            cout<<"\n To display all records";
            break;
        }
        case 'E' :
        {
            cout<<"\n To exit the menu";
            break;
        }
        default : cout<<"\n Invalid selection";
    }

    cout<<"\n";
}

system("pause");
return 0;
}
The version that you have just pasted has two int main() and also starts trying to define one function inside another on line 79. Is that a copy-paste error or what?

Your indenting (and hence your braces) are inconsistent.
Topic archived. No new replies allowed.