954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Desiging a set of rules for a match

Hello, I've got a big query ... I wold like my code to do something but I don't think I'm doing it right. Basically it checks 1 string with another and prints true or false.... However I want to restrict what is inputted into the string.

So you'd have string = { james, holly, mat, holly}

etc

is they anyway of making that into a set of rules so that if the user enters a name on that list its excetped and if it isnt its not accepted?

I have thought of enum but then again I don't understand what it is

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

Enum isn't exactly what you're looking for. Enum is used for creation of new types that have named integers as their members.

For example:

enum Days { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY };

And MONDAY would equal 0, TUESDAY would equal 1, etc.

Now, for your purposes, there isn't something that "automatically" denies any other string but those that you want to allow. You'd have to do this by either error checking, or creating a struct or class with operator overloading. As you don't even know what an enum is, I doubt you'll be able to grasp operator overloading, or OOP (Object Oriented Programming) to begin with.

In short, just check the char array/string's value against your predefined data sets, if they match, continue, if they don't, ask for input again.

Hope this helps,
RaWx

RaWx
Newbie Poster
2 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

bit like a menu driven structure? ...

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

what about getting the input, looping through the string array checking if it matches any entries - setting a found flag to 1 or whatever? then if it is found the user entered a string in the list, otherwise he/she/it didnt....

something along the lines of

string str;
string names [] = { "james", "matt", "holly" };
bool found;

cin >> str;

for(int i = 0; i < number_of_names; i++)
{
    if(str == names[i])
    {
        found = 1;
    }
}

if(found)
    cout << "Name matches\n";
1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

Just as a side question, does C++ support regular expressions and/or regex matching?

cscgal
The Queen of DaniWeb
Administrator
19,422 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

Hello interesting design 1000 BHP whilst awaiting reply to this topic I gave my head a little thought... Dah it hurts when you do that!

But I came up with something like this:


[php]

#include

using namespace std;

int main()
{

char namea[12];
char nameb[12];
int age1;
int age2;


cout << "Enter the name and age to compare:\n\n";

cin >> age1 >> namea ;

cout << "please enter the 2nd name and age to compare: \n\n";

cin >> age2 >> nameb ;



if (age1==age2 && (age1>1 && age2<149))
{
if (strcmp ( namea, nameb) == 0)

cout << "They Match!!\n";
}

else if

(age1==age2 && (age1<1 || age2>150))
{
cout << "error in age";
}

else if

(age1 != age2 || (age1 > 1 && age2 <149 ))
{
cout << "your strings don't match\n\n";
}

return 0;

}

[/php]

Has you can see there’s no control has to what is entered into the string numbers along with letters can be amounts the combination of the many. I thought an enum would do it but perhaps not...

Could anyone advise further?

1000 BPH I get really bad compile errors when trying to compile that code, yes I know I don’t want spoon feeding... Has you can see a little frustrated with the code...

But thanks for all your replies so far most appreciated.

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 
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
Team Colleague
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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

sorry to be rude or blunt here but does anyone have any suggestions for my post that was made earlier?

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

>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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

ouch thats pretty good! ...dont suppose you could explain...

return db.find(name) != db.end();

since i learn by knowing what im doing and

set db(init, init + 4);


can i also ask why have delcared global varible string? I've always been taught that creating a global is considered illegal in the eyes of a programmer and must be used has last resort...

Sorry if the statement offends poeple, just what ive been taught

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

>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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

intresting I like this forum - People give you expainations rather than giving you the code and send you on your way :D...

I have another idea...

if you use the enum command ... ie

enum {mon = 1, tue...and so on}

is it possible for a user to enter the number ...ie 1 to return mon?

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

>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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

acid burn you said about worrying if someone typed letters in the age string...

try the IsDigit() function or the IsAlpha() function. I have posted this before so you could search for it as I have forgotten the library these functions are in... :(

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

>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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

ah yes thats the one cctype! ive just done a bit of VB programming hence the capital letters! how is the program going acidburn?

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

thanks 1000 will take a look,

Whilst sleeping i did think about this:

What if i had 2 arrays, 1 of the array is where the user enteres its data, and the other is used to verify if the data entered in in the 2nd array? If its not then its not valid data ...

ie

arraya[] - used for gathering data
arrayb []- used to verify data

so the user will enter data and i assume some sort of loop would be used to verify the order of the characters

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 
What if i had 2 arrays, 1 of the array is where the user enteres its data, and the other is used to verify if the data entered in in the 2nd array? If its not then its not valid data ...

VERY confused what you want to do here! if anything it seems like the second array should ba an array of bool data types so that you flag it true if the data in array one is ok, or false if it isnt. and INSTEAD of two arrays it might be better to pack it together in a struct/class/typedef like so

struct entry
{
    string data; // c++ ansi string <cstring>, you can use char * for c strings
    bool valid; // true (1) = ok, false (0) = invalid
}

entry array[10]; // the array


though i see no reason to store the validity as surely if it is invalid you would deal with it right away....?

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

>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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You