Please help with comparing char strings

Started by
6 comments, last by Sr_Guapo 17 years, 11 months ago
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
Advertisement
have you tried looking into using strings? i think it would make things much easier.
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
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]

Beginner in Game Development?  Read here. And read here.

 

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();};
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.
Writing errors since 10/25/2003 2:25:56 AM
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?
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!
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute

This topic is closed to new replies.

Advertisement