I'm writing an assessment tool that cross checks the name entered by a person with a list of names of known people blacklisted by me. So far I can only figure out how to compare a users entry with one name, effectively limiting my blacklist to one name. I'm new to C++ and would appreciate any assistance anyone can provide on the matter.

Here is my currently working code, which only lets me compare one name. Now I know I can continue adding if statements, but this can grow confusing when I have to add 30+ names to the list, and I'm sure there is a simple way of cross checking with a list in C++.

#include
#include
using namespace std;

int main()
{

char person[80];
cout << "Please enter your name: ";
cin >> person;

if (strcmp (person, "Merlin") == 0)
{
cout << "Your name has been blacklisted.";
}
else

cout << "\nYou have been accepted:" << person << endl;
cin.get();
cin.get();
return 0;
}

P.S. I have also been informed that I can add the blacklisted names to a text file, which would be called into the program and then crosschecked to see if any of the names in the text file match the user input, regardless on if they use capitals or not. I am so far not able to figure out how though, so any assistance you can provide would be greatly appreciated.

Recommended Answers

All 3 Replies

Keep the blacklist in a vector of strings, then use a loop to compare the name entered by the user to each of the names in the blacklist. vector<string> blacklist; If you want to ignore case, then convert all names to either upper or lower case, whichever you want, so that you can make case insensitive comparisons easier. There is no function that converts entire strings, so you have to loop through the string one character at a time and convert them using either toupper() or tolower()

I'm a little confused, could you explain with some example code?

The best thing to use is std::map here. That will give you very good performance.

Here is an example :

#include <iostream>
#include <map>
#include <string>

using namespace std;

typedef pair<string,int> NamePair;

int main(){
	std::map<string,int> listOfNames;

	listOfNames.insert( NamePair("jeff",0) );
	listOfNames.insert( NamePair("Don",1) );
	listOfNames.insert( NamePair("Calvin",2) );

	//or read from a file with names and insert it into the map

	string input;
	cin >> input;

	if(listOfNames.find(input) != listOfNames.end()){
		cout << "Found in list\n";
	}
	else cout << input << " is not in my list\n";
}

You could read from a file that contains names, and insert it into the listOfNames,
then use that for guidance. Here is some reference
for std::map.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.