943,931 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7460
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 28th, 2004
0

Desiging a set of rules for a match

Expand Post »
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
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Dec 28th, 2004
0

Re: Desiging a set of rules for a match

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:
C++ Syntax (Toggle Plain Text)
  1. 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
RaWx is offline Offline
2 posts
since Dec 2004
Dec 28th, 2004
0

Re: Desiging a set of rules for a match

bit like a menu driven structure? ...
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Dec 28th, 2004
0

Re: Desiging a set of rules for a match

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

C++ Syntax (Toggle Plain Text)
  1. string str;
  2. string names [] = { "james", "matt", "holly" };
  3. bool found;
  4.  
  5. cin >> str;
  6.  
  7. for(int i = 0; i < number_of_names; i++)
  8. {
  9. if(str == names[i])
  10. {
  11. found = 1;
  12. }
  13. }
  14.  
  15. if(found)
  16. cout << "Name matches\n";
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Dec 28th, 2004
0

Re: Desiging a set of rules for a match

Just as a side question, does C++ support regular expressions and/or regex matching?
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Dec 28th, 2004
0

Re: Desiging a set of rules for a match

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 <iostream>

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.
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Dec 28th, 2004
0

Re: Desiging a set of rules for a match

Quote originally posted by cscgal ...
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 <regexp.h> , for whatever that's worth.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Dec 28th, 2004
0

Re: Desiging a set of rules for a match

>does C++ support regular expressions and/or regex matching?
Not natively or through the standard library. However, boost has a regex library for C++.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 28th, 2004
0

Re: Desiging a set of rules for a match

sorry to be rude or blunt here but does anyone have any suggestions for my post that was made earlier?
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Dec 28th, 2004
0

Re: Desiging a set of rules for a match

>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:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4.  
  5. using namespace std;
  6.  
  7. namespace {
  8. string init[] = {
  9. "james", "holly", "mat", "harry",
  10. };
  11. set<string> db(init, init + 4);
  12. }
  13.  
  14. bool valid_name(const string& name)
  15. {
  16. return db.find(name) != db.end();
  17. }
  18.  
  19. int main()
  20. {
  21. cout<< boolalpha << valid_name("john") <<endl;
  22. cout<< boolalpha << valid_name("mat") <<endl;
  23. cout<< boolalpha << valid_name("tom") <<endl;
  24. cout<< boolalpha << valid_name("harry") <<endl;
  25. cout<< boolalpha << valid_name("james") <<endl;
  26. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: a multi function
Next Thread in C++ Forum Timeline: Visual c++ libraries





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC