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

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

bit like a menu driven structure? ...

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";

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

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:

#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;

}

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.

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.

>does C++ support regular expressions and/or regex matching?
Not natively or through the standard library. However, boost has a regex library for C++.

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

>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;
}

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

>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<string> 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. :)

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?

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

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... :(

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

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

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

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....?

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

Basically the same thing we've been discussing from the start: matching input strings with a validation list

Ok then:

string test_array[] = { your strings here... };
string valid_strings[] = { strings you want here... };
bool valid = false;

for(int i = 0; i < number of strings in the test array; i++)
{
    string str = test_array[i];
    valid = false;
    for(int j = 0; j < number of validation strings; j++)
    {
        if(str == valid_strings[j])
            valid = true;
    }

    if(valid)
        cout << "String " << i + 1 << " is valid\n";
    else
        cout << "String " << i + 1 << " is not valid\n";
}

Hope that is useful :)

>Hope that is useful
Sequential search is probably the worst possible choice, but it's still a valid one...

true, i forgot about binary searches. esp when there is a library function to do it! whoops ;)

>true, i forgot about binary searches
Well, since the search array would have to be sorted, it's actually better to organize the data in a more appropriate structure. Hence, why I chose a set for my first example. Not only is it efficient, it's also shorter and easier to understand than any hand written search algorithm.

a set? either ive called it a different name or i dont know what you are going on about...

>either ive called it a different name or i dont know what you are going on about...
You just weren't paying attention. My first example used a set, which in C++ is a standard container class most likely implemented with a binary search tree:

#include <set>
...
set<string> db(init, init + 4);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.