im currently a college student and im studying c++ at the moment. i need to create a multi array for name and password (but it will be from user input) not to mention i cant write to this array until i confirm it doesn't already exist in this array. Im totally confused, i absolutly dont know where to start. i already wrote the program up to this point <main menu/new member> with all cin's and cout's. but the next step is to create this array but yet search in the array before i can save. any help will surfice. i know the rules say post the code but honestly the code i have has no weight on this problem, and i also believe i have to change password to string for the array. please help


btw this is my first post :)

Recommended Answers

All 11 Replies

i guess i can post what i have even though it is probably in the wrong direction!

this is my declaration:

int arraySearch(const string&,int&,const int[][2]);

this is my started search:

//search array function
int arraySearch(const string& name,int& password,const int secArray[][2])
{
	//declare variable
	char passStrg[10];
	string passwordStrg = "";
	

	//convert password from int to string
	//itoa(password,passStrg,10);
	//passwordStrg = passStrg;
	while((!found)&&(index < 
return index;
}

I doubt you're supposed to search for the password to look for duplicates. The common case is where you refuse to add a user (name) because it already exists.

So a function that took the array and a name to search for that returned where the name was found (or -1 for not found) would meet your needs.

After the user enters a name / password (or maybe even just after the name) you would search for the name and only add the new name if you didn't find it.

The prototype might look something like:

// The first argument should match your array declaration
// The second argument is the number of names already in the array
// The third argument is the name to search for
// The return values should be -1 for not found and the array index for the name if found
int findName(string names[], int numNames, const string & name);

If you use an STL collection class in place of the array in the call above, the numNames shouldn't be necessary, the STL classes can tell you how many items are in the collection.

You make an effort at writing the function and we can help you if you get stuck. If you get stuck, post your code along with a problem statement (i.e. "I expected ___ but I'm getting ___")

PS- When posting c++ code, please use c++ code tags
[code=c++] // Your code here

[/code]

thx, ill work on that and repost, and sorry for not using the c++ code tags

int arraySearch(const string& name,int& numNames,const string secArray[][1])
{   
    int index = 0;
    bool found = false;

    while((!found)&&(index < numNames))
    {
    if(name == secArray[index])
        {
            found = true;
        }
        else
        {
            index ++;
        }
    }   
        if(found)
        {
            return index;
        }
        else
        {
            return - 1;
        }
}

i highlighted where i am getting issues. the complier is returning this error message.....error

C2678:binary'==' no operator found which takes a left-handed operand of type 'const std::string'(or there is no acceptable conversion). 

please help

i figured that one out, if i take away the second [] away it will solve the problem, but would that stop me from validating both fields if im searching name/password

Sorry for the delay, I forgot to check mail. Can you show me the declaration for the array (from main?)

Alternatively, based on the parameter declaration, you could use if(name == secArray[index][0]) in the test to see if the error will go away.

(and in reference to your post while I was posting -- this routine was never intended to compare the password, it was designed to be a NAME comparison)

int result = arraySearch(name,password);

thats main.

this the new function, it works with on array[] but with 2 array[][] it doesnt

int arraySearch(const string& name,const string& callSign)
{	
	int index = 0;
	bool found = false;
	int numNames = 10;
	const string secArray[10][2];
	secArray[0][0] = "fred","arab"
	while((!found)&&(index < numNames))
	{
		if((secArray[index][2]) == (name,callSign))
		{
			cout<<"found"<<endl;
			system("pause");
			found = true;
			break;
		}
		else
		{
			index++;
		}
	}	
		if(found)
		{
			return index;
		}
		else
		{
			return - 1;
		}
}

i had the option between password or call sign, i figure callsign would be better since it was another string datatype


(sorry i didnt see your in reference statement until now. my professor want me to verify both fields, i guess because lets say the password would go with that user, but i switch it to callsigns (basically the username) because it made more sense than the password + password would have had to be converted to string datatype and back later on.) but maybe you know something i dont, and by this i mean maybe the sencond field is implied somehow :)

Ouch.

Why are you declaring and filling the array inside arraySearch()?

The array should either be passed in (in which case arraySearch needs more parameters) or arraySearch should reference the 'global' array. (Note that I don't like the global array answer as a matter of principle, but it would work.)

Ok, so if you have defined the array as const string secArray[10][2]; (oops, don't declare the global array const, we could declare the parameter const because we don't plan to change it, but the global one will need to be updated.)

Ok, so if you have defined the array as string secArray[10][2]; I presume that is an array of 10 users. Is secArray[ii][0] the name and secArray[ii][1] the callsign?

Why are you referencing secArray[index][2]?

Does this even compile?

if((secArray[index][2]) == (name,callSign))

Did you want to do something like:

if (secArray[index][0] == name && secArray[index][1] == callSign)

Are you really sure you want to search for both?

With the above test, if either one doesn't match, you don't find it.

Another slight modification to the 2 dimensional array would be an array of a class or struct. (I favor classes personally.)

You could do something like:

class RegisteredUser
{
    public:
        RegisteredUser();
        RegisteredUser(string name, string callsign);

        const string & getName() const;
        const string & getCallsign() const;

        void setName(string name);
        void setCallsign(string callsign);

    private:
        string mName;
        string mCallsign;
};

Then your array of users would be something like:

RegisteredUser secArray[10];

And comparisons would look more like:

if (secArray[index].getName() == name && secArray[index].getCallsign() == callSign)

thank you murtan, sorry for all the trouble you are a big help. im such a noob :)

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.