const char

I'm trying to make a whitelist sort of thing, just a very simple one with cin and stuff. However, to check if the people are on the whitelist I do this:

1
2
3
  if (username == "test") {
			cout << "success";
		}


Doing this is sort of annoying... I've seen people use something like
const char* [10]
or something to store the usernames...

How would I do this?


Thanks!
I don't recommend that you use const char*[10], is suggest you use std::string instead, like you are in your code snippet.

+1 On using string, defininitely much better, I'll add an example, it might not be exactly what you want but you can learn from it and change it around to work for you.

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

int main()
{
	std::string names[3];
	std::string whitelistNames[3] = { "Peter", "Jakob", "Amanda" };

	for (int i = 0; i < 3; i++)
	{
		std::cout << "Enter name #" << i + 1 << ": ";
		std::cin >> names[i];
		std::cout << "\n";
	}

	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			if (names[i] == whitelistNames[j]) // checks if each name you entered exists in the whitlisted names
			{
				std::cout << "\n";
				std::cout << names[i] << " is white-listed";
			}
		}
	}
}
one good method is to store all the names in a vector , that is a array that can store any object type , first you have to include <vector> library

the declaration that you are looking for is as follows (assuming that you are using namespace std)

 
vector<const char*> myNames;


this creates a resizable vector that stores only const char*
there are more initialisers list for vectors like vector<const char*> myNames(10, "names"); which initialises 10 identically elements which all hold "names"
for more information google is your friend

to add more elements to the vector use myNames.push_back("some string"); , this adds some string to the back of the vector
to pop elements you use myNames.pop_back();

to access elements you use subscription operator [] just like with arrays
for example cout << myNames[3]; outputs the 4th element of the vector
^
/s/vector<const char*>/vector<string>/
Topic archived. No new replies allowed.