Reading string till you find '|' then compare and then do something after

Hey everyone.

So i have a function which gets a string from a function which accepts user input

username as std::string.
password: only as int (restricts user input to to int only)
so i expect only numbers for password.

How i do i check for "|" and when found, compares the input to the one the user entered. If its not correct it jumps to the next line and does the same. else if it correct it jumps the "|" and compares the next 4 numbers and if the are correct it allows the user.

Would be alot of help, since its a project for college :)

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
void UserAuthorize::getAuth(std::string * Identity, int *authorizer)
{
	std::cout << "Logging in...";
	std::ifstream LoadF_F;
	LoadF_F.open("Data.pcf", std::ios::in | std::ios::app | std::ios::binary);
	if (LoadF_F.is_open()) 
	{
		//We Create a stringstream to store data
		std::stringstream buffer;
		buffer << LoadF_F.rdbuf(); //Load the Data into the string
		std::string Loaded = buffer.str();


		//*******************FOR DEBUG ONLY********************************//
  /*Print the File*/std::cout << Loaded << std::endl << std::endl; /*Print the file*/
		//*******************FOR DEBUG ONLY********************************//
		
		//Variables That Act as "actors"

		std::string pos1 = "|";

		std::size_t found = Loaded.find(pos1);

		if (found != std::string::npos)
		{
			
		}

		LoadF_F.close();
	}

	ToSetConcoleName(Identity);

	//Clears and deletes the Variable
	Identity = NULL;
	delete Identity;
        authorizer = NULL;
        delete = authorizer;
}


File:
1
2
3
4
5
kelvin|1445|3;
mac|9011|1;
james|1333|2;
tom|9112|1;
cai|1900|3;

Last edited on
thanks for the reply i'll have a play around with that and see how it goes
You can pass an "offset" to it so that is starts its searching later on in the string. Once you find the first one, remember where it was and use that as an offset to that it'll start searching the next time after the first '|'.
An alternative:
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
#include <fstream>
#include <iostream>
#include <regex>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

bool verify_identity(const std::string& name, const std::string& pin)
{
    std::ostringstream buffer;
    buffer << std::ifstream("data.pcf").rdbuf();
 
    std::regex ex(name+ "\\|" + pin + "\\|");

    return std::regex_search(buffer.str(), ex);
}

int main()
{
    std::vector<std::pair<std::string, std::string>> identities =
    {
        {"kelvin", "1445"},
        {"james", "3333"},
        {"tom", "9112"},
        {"mary", "n21m"}
    };

    for (auto& ident : identities)
    {
        if (verify_identity(ident.first, ident.second))
            std::cout << "Verified " << ident.first << '\n';
        else
            std::cout << "Unable to verify " << ident.first << '\n';
    }
}


Of course, string::find would work equally well here, but I figure you'll probably be interested in the added capabilities of regex here at some point.
Last edited on
Topic archived. No new replies allowed.