Desiging a set of rules for a match

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Desiging a set of rules for a match

 
0
  #1
Dec 28th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2
Reputation: RaWx is an unknown quantity at this point 
Solved Threads: 0
RaWx's Avatar
RaWx RaWx is offline Offline
Newbie Poster

Re: Desiging a set of rules for a match

 
0
  #2
Dec 28th, 2004
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Desiging a set of rules for a match

 
0
  #3
Dec 28th, 2004
bit like a menu driven structure? ...
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Desiging a set of rules for a match

 
0
  #4
Dec 28th, 2004
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

  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";
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,036
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Desiging a set of rules for a match

 
0
  #5
Dec 28th, 2004
Just as a side question, does C++ support regular expressions and/or regex matching?
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Desiging a set of rules for a match

 
0
  #6
Dec 28th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: Desiging a set of rules for a match

 
0
  #7
Dec 28th, 2004
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.
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,603
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Desiging a set of rules for a match

 
0
  #8
Dec 28th, 2004
>does C++ support regular expressions and/or regex matching?
Not natively or through the standard library. However, boost has a regex library for C++.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Desiging a set of rules for a match

 
0
  #9
Dec 28th, 2004
sorry to be rude or blunt here but does anyone have any suggestions for my post that was made earlier?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,603
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Desiging a set of rules for a match

 
0
  #10
Dec 28th, 2004
>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:
  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. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC