Sign in to follow this  

Please help with comparing char strings

This topic is 4230 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing a program that requires a login. I have to use a char array to get the users input then compare it to another char array. For some reason, it's not working. Here's what I've got:
bool ClientDatabase::AllowLogon(char userName,char password)
{
     for(int i=0;i<m_NumOfClients;++i)
             if(userName==(m_Clients[0].m_UserName))
                   return true;
     
     return false;
}                     

When I compile, I'm told that "ISO C++ forbids comparison between pointer and integer " How does my compiler get an integer out of this? How do I fix it? Thanks, tlak

Share this post


Link to post
Share on other sites
I was just told that this might be homework. You should look at your string libray functions. In there is the way to compare strings. Also look at your loop to make sure you are doing everything right. We will push you in the right direction but won't do your homework for you.

theTroll

Share this post


Link to post
Share on other sites
you should look into the strcmp() function.
also, is the username one char? because right now it seems you're comparing one char to an array of chars and most likely that's where your error is coming from.

also since this may be homework, i can't give you any concrete, discrete answers, sorry.

you might wanna post the class for your code so we can nudge you in the right direction.

edit: LOGICALLY the code above is right....
edit2: well it WAS there [smile]

Share this post


Link to post
Share on other sites
Baker,

Strings would make this much easier, but I have already written the rest of the program for character arrays, so it would take more work then it's worth to convert everything.

Troll,
That particular source returns false no matter what I try, but I will look into strcmp. Never heard of it before, thanks for the heads up.

Alpha,
No, this is not homework :). userName (in both forms) is an array of characters. Here's the ClientDatabase class:


class ClientDatabase
{
private:
MyClient m_Clients[100];
int m_NumOfClients;
public:
int m_CurrentClient;
ClientDatabase(); //Loads all MyClients
bool AllowLogon(char* userName,char* passWord);
};


And here's the MyClient class:

class MyClient
{
public:
char m_UserName[36];
char m_LitName[36];
char m_Password[36];
int m_ClientNumber;

void SetData();
};

Share this post


Link to post
Share on other sites
Maybe your names in the arrays are not null terminated? In that case it would compare the passed names with the names in the array plus everything else up to the next \0 occurrance.

Share this post


Link to post
Share on other sites
Update:

I cruised through the GameDev articles and found that strcmp returns 0 when the strings are identical, so I just threw a ! into my if statement and it all works. Thank you!

Edit: Just out of curiosity, why did everyone think this was a homework assignment? Is this like a common chapter or something?

Share this post


Link to post
Share on other sites
Quote:
Original post by TLAK1001
Edit: Just out of curiosity, why did everyone think this was a homework assignment? Is this like a common chapter or something?


It just seems like the same kind of assignment given as homework: A simplified pseudo-real world problem. Glad you figured it out!

Share this post


Link to post
Share on other sites

This topic is 4230 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this