1

I am having trouble with a two-dimensional array comparison. I need to create a pseudo login system that asks the user for a username and password and then compares the input to a predefined list of usernames.

In the function, the predefined usernames are represented by the *s and the user input is *s1. When I try to compile it, this pesky trouble-maker appears:

68 D:\Personal\Dev-Cpp\projects\loginSysTest\main.cpp invalid conversion from char' toconst

regarding the strncmp function in the if statement.

This is the code:

#define nameLenght 30
#define User 10

char usernames[User][User] = {{"student"}, {"admin"}, {"Deus Ex Machina"}};

//=====================================================================
int main(int argc, char *argv[])
{
    char usernameInput[nameLenght + 1] = {0};
    gets(usernameInput);
    int login = compS(*usernames, usernameInput);

    if(login == 0)
        printf("Access Granted! \n");
    else 
        printf("Access Denied!");

    system("PAUSE");    
    return 0;
}
//=====================================================================        

int compS(char *s, char *s1)
{ 
    for(int k = 0 ;k < nameLenght; k++)
    {
        if(strncmp(s[k], s1, strlen(s1)) == 0)
        return 1;  
    }
}

Thank you in advance.

Ziezi
  • 6,375
  • 3
  • 39
  • 49
Yavor Lulchev
  • 75
  • 1
  • 11
  • 3
    `s[k]` is a `char`, not a `char*`. Did you intend to have `char** s` (which you can't use with your two-dimensional array)? (I suspect that you have thrown a cast or two into the mix to stop the compiler from complaining. That is a bad idea in general.) – molbdnilo Dec 22 '15 at 21:39
  • 6
    Unrelated note: "Deus Ex Machina" is 16 characters (including '\0') and you are storing it in an element of a `[10][10]` array – R_Kapp Dec 22 '15 at 21:41
  • 1
    Please show how you ask for the input. If it was `fgets()`, did you clean off the trailing `newline`? – Weather Vane Dec 22 '15 at 21:42
  • Oops - `main.cpp` - this isn't C is it? – Weather Vane Dec 22 '15 at 21:43
  • 1
    What0s `compS` supposed to do again? – Shoe Dec 22 '15 at 21:49
  • 1
    Should be `char usernames[User][nameLenght]` or something. – Ilya Dec 22 '15 at 21:49
  • compS is supposed to compare the user inputed string to the predefined strings. – Yavor Lulchev Dec 22 '15 at 21:53
  • 1
    The line `char usernames[User][User] = {{"student"}, {"admin"}, {"Deus Ex Machina"}};` is an error. Your compiler should tell you about this. It's important that you fix all compiler messages before trying to run your program. Messages should be treated as errors even if they "only" say "warning" – M.M Dec 22 '15 at 22:22
  • It does not come up as an error. – Yavor Lulchev Dec 22 '15 at 22:28
  • Use `getch()` instead of `system("PAUSE")` to be cross platform btw – Czipperz Dec 22 '15 at 23:00

3 Answers3

4

Just use std::vector and std::string along the lines of:

std::vector<std::string> usernames({"student", "admin", "Deus Ex Machina"});
std::string input;
std::cin >> input;
if (std::find(begin(usernames), end(usernames), input) != end(usernames))
    std::cout << "Access Granted!\n";
else std::cout << "Access Denied!\n";

Live demo

Shoe
  • 74,840
  • 36
  • 166
  • 272
1

If you want (or need) to keep your C-style not-pretty-nor-very-safe code, you could write

strncmp(s + (k+nameLenght), s1, strlen(s1))

That would compile and perhaps work with some more efforts, but you'll have to correct the usernames sizes and actually call compS.

See that other answer for a safer C++-styled code.

Community
  • 1
  • 1
Ilya
  • 5,377
  • 2
  • 18
  • 33
  • That means that if the user types `"ad"` then it will match `"admin"` because you limited the match to the length of the string... not sure of this is what OP wants, but if not, it'd be better to use `strcmp` without the length parameter. – M.M Dec 22 '15 at 22:25
  • 1
    @m-m that's right, and the code has other problems. My answer is mainly about the question asked (compile error). – Ilya Dec 22 '15 at 22:28
  • @Ilya Yes, I've noticed that myself, could you elaborate on this particular problem if you could suggest a topic to read it will be wonderful.Sorry for the late response. – Yavor Lulchev Jan 06 '16 at 03:08
0

There are a few ways you can accomplish this. This is the first way that comes to mind.

char* usernames[] = { "student" , "admin", "Deus Ex Machina", NULL };

int compS(char **s, char *s1)
{
    for (int k = 0; s[k] != NULL; k++)
    {
        if (strncmp(s[k], s1, strlen(s1)) == 0)
            return 0;
    }
    return 1;
}

You have to pass a pointer to a string to strncmp() so I changed your array to an array of pointers to strings. The array has a null pointer at the end to allow the loop to know when it has reached the end.

Alex
  • 316
  • 4
  • 12