| | |
Desiging a set of rules for a match
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
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
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
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:
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
For example:
C++ Syntax (Toggle Plain Text)
enum Days { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY };
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
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
something along the lines of
C++ Syntax (Toggle Plain Text)
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";
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
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.
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.
>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:
There's no way to control what input you get, you're restricted to validation:
C++ Syntax (Toggle Plain Text)
#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; }
I'm here to prove you wrong.
![]() |
Other Threads in the C++ Forum
- Previous Thread: a multi function
- Next Thread: Visual c++ libraries
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets







