Just as a side question, does C++ support regular expressions and/or regex matching?
cscgal
The Queen of DaniWeb
19,422 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
Just as a side question, does C++ support regular expressions and/or regex matching?
Just by searching Google with the terms "c++" + "regexp", I came up with a header called , for whatever that's worth.
alc6379
Cookie... That's it
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
>does C++ support regular expressions and/or regex matching?
Not natively or through the standard library. However, boost has a regex library for C++.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>does anyone have any suggestions for my post that was made earlier?
There's no way to control what input you get, you're restricted to validation:
#include <iostream>
#include <string>
#include <set>
using namespace std;
namespace {
string init[] = {
"james", "holly", "mat", "harry",
};
set<string> db(init, init + 4);
}
bool valid_name(const string& name)
{
return db.find(name) != db.end();
}
int main()
{
cout<< boolalpha << valid_name("john") <<endl;
cout<< boolalpha << valid_name("mat") <<endl;
cout<< boolalpha << valid_name("tom") <<endl;
cout<< boolalpha << valid_name("harry") <<endl;
cout<< boolalpha << valid_name("james") <<endl;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>return db.find(name) != db.end();
The find member function of set will either return an iterator to the item if it's found, or end(). This simply says "return true if name is found, otherwise return false".
>set db(init, init + 4);
This constructs a set of strings using the contents of the init array. It's easier than creating a default constructed set and then inserting the strings one by one.
>can i also ask why have delcared global varible string?
Random choice. It was either that or declare the variables as static in valid_name. Do as your beliefs dictate.
>I've always been taught that creating a global is considered illegal in the eyes of a programmer
Your teachers were mindless cattle. The biggest problem with global variables is that they are visible everywhere by default. Because most people don't restrict that visibility, globals can cause untold problems all over a project. However, by nesting the globals in an unnamed namespace, you force them to be local to the file in which they're declared. My globals can't be seen outside of the file, so the biggest argument against them is nullified (provided you don't use monolithic files). As long as you use them intelligently, global variables are okay. But if you use them intelligently, you'll find that you end up not using them most of the time. :)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>is it possible for a user to enter the number ...ie 1 to return mon?
Just about anything is possible. Whether you want to do it or not is the question. What are you trying to accomplish? It sounds like you're stuck on the wrong path, but it's hard to suggest alternatives if I don't know what you're trying to do.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>try the IsDigit() function or the IsAlpha() function
isdigit and isalpha. C++ is case sensitive.
>I have forgotten the library these functions are in...
#include
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>VERY confused what you want to do here!
Basically the same thing we've been discussing from the start: matching input strings with a validation list.
>Whilst sleeping i did think about this:
Yes, that's a valid solution, but it's not as efficient as the set solution I gave you. The reason is that search and insertion into a set is guaranteed to be logarithmic, while searching an array for every new item is linear. It's also harder to implement because the set is already there waiting to be used.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401