Calling player stats from vector

Okay, so I have a player name vector within a class as well as a functioning register and login system that reads from files. (Ex: Allura.txt, or Bob.txt). Usernames and passwords are saved seperated by a ";", which I use as a delimiter with the getline function:

1
2
getline(ReadFile, filename, ';');
getline(ReadFile, filepass, ';');


Now I'm stuck. I've created a command for whenever a player enters 'stats'. What is the best solution for C++ to know which player is typing stats, and to print those statistics of that player from the vector without prompting the player to define their name to search through the vector every time? I could use temporary variables but if more than one player enters stats, that variable will be overwritten to the next player.
I will explain more clearly if needed; I've been confused with this for days.
Last edited on
I will explain more clearly if needed;
... it is needed ... such as:

1. the class declaration if not the full definitions (you can post your code at pastie, etc if the file is too large) and copy a link here
2. couple of sample lines from filename, filepass
3. the int main() program
Main:

http://pastebin.com/61WkGFuM

Character.h Class File:

http://pastebin.com/v0Aa4hrz

Character.cpp Source File:

http://pastebin.com/y7HWZ9cY

I've used comments under the 'stats' command in main to explain more thoroughly. Thanks! =)
Last edited on
By design, in your program, each class object has the entire vector of all the players and so it is difficult to distinguish between them. Take a step back and consider what you're trying to do and you'll see that you don't even need a class to achieve your goals, the file design can handle all the functionalities of the class. Your goals are to:
(a) to set up a file to save players' names and passwords
(b) add a player (unique names only)
(c) search a player and, if found, return name and password
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <limits>//for cin.ignore() limits;
using namespace std;

int main()
{
	bool fQuit = false;
	while (!fQuit)
	{
		cout<<"1. Add player \n2. Search player \n3. Quit \n";
		int choice;
		if (!(cin >> choice))//input validation; 
        {
            cout << "Incorrect entry, try again\n";
            cin.clear();
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}
		else
        {
            switch(choice)
            {
                case 1:
                {
                    cout<<"Enter player's name: \n";
                    string pname;
                    cin>>pname;
                    bool match = false;
                    ifstream file_in("F:\\test.txt");
                    if(file_in)
                    {
                        string line;
                        static const auto delimiter = ';';
                        string dummy;
                        while(getline(file_in, line, delimiter) && getline(file_in, dummy))
                        {
                            if(line == pname)
                            {
                                cout<<"Name already exists \n";
                                match = true;
                                break;
                            }
                        }
                    }
                    if (match == false)
                    {
                        cout<<"Enter password \n";
                        string pword;
                        cin>>pword;
                        ofstream file_out("F:\\test.txt", ios::app);
                        file_out << pname << "; "<<pword<<"\n";
                    }
                }
                break;
                case 2:
                {
                    ifstream file_in("F:\\test.txt");
                    cout<<"Enter player's name: \n";
                    string pname;
                    cin>>pname;
                    if(file_in)
                    {
                        string line;
                        static const auto delimiter = ';';
                        bool match = false;
                        while(getline(file_in, line))
                        {
                            stringstream stream(line);
                            string name_check, pword;
                            getline(stream, name_check, delimiter) && getline(stream, pword);
                            {
                                if(name_check == pname)
                                {
                                    cout<<"Name: "<<pname<<" exists in the file and password is:"<<pword<<"\n";
                                    match = true;
                                    break;
                                }
                            }
                        }
                        if(match == false)
                        {
                            cout<<"Name doesn't exist \n";
                        }
                    }
                }
                break;
                case 3:
                    fQuit = true;
                    cout<<"Goodbye \n";
                    break;
                default:
                    cout << "Error: Invalid input. \n";
                    break;
             }
        }
    }
}

Once the file is set-up, and you still wish to, it is very simple to define a class/struct with name and password, overload insertion/extraction operators and do any kind of further processing
Last edited on
Topic archived. No new replies allowed.