Been working on this program which requires the use of a function that compares a string input by the user and gives the user the opportunity to leave the characters that he/she doesn't know out of the input, replacing them with * . The input represents a license-plate of a car that has 6 characters (for instance ABC123) and the user is allowed to leave any of those characters out (for instance AB** 23 or ** C12* etc.). So the function needs to return all objects that match the characters in the right position, but it cannot return if, say, A is in the right position but any of the other characters are not. The user is, however, allowed to only enter A* * * * *, for instance, and the function should return all objects that have A in the first position. What I did was use a function to remove all the asterisks from the input string, then create sub-strings and send them to the function as a vector.
string removeAsterisk(string &rStr)// Function to remove asterisks from the string, if any.
{
stringstream strStream;
string delimiters = "*";
size_t current;
size_t next = -1;
do
{
current = next + 1;
next = rStr.find_first_of( delimiters, current );
strStream << rStr.substr( current, next - current ) << " ";
}
while (next != string::npos);
return strStream.str();
}
int main()
{
string newLicensePlateIn;
newLicensePlateIn = removeAsterisk(licensePlateIn);
string buf; // Have a buffer string
stringstream ss(newLicensePlateIn); // Insert the string into a stream
vector<string> tokens; // Create vector to hold our words
while (ss >> buf)
tokens.push_back(buf);
myRegister.showAllLicense(tokens);
}
The class function that receives the vector currently looks something like this:
void VehicleRegister::showAllLicense(vector<string>& tokens)//NOT FUNCTIONAL
{
cout << "\nShowing all matching vehicles: " << endl;
for (int i = 0; i < nrOfVehicles; i++)
{
if(tokens[i].compare(vehicles[i]->getLicensePlate()) == 0)
{
cout << vehicles[i]->toString() << endl;
}
}
}
If anyone understand what I'm trying to do and might have some ideas, please feel free to reply, I would appreciate any advice. Thanks for reading this/ A.