I am almost completely brand new to programming (been in my first programming class for about a month), and I am trying to learn stuff that my professor isn't teaching yet so that I don't get behind later, and I am doing this by writing simply programs and learning all the stuff I need to learn to make them.

With this program below I want it to have a saved username and password and basically all I want is for someone to enter username and password, and for it to check if they are correct, and if so print a little message, if not print a message showing they weren't right.

The errors I am getting for this bit of code are

NE.c: In function `main':
NE.c:16: error: assignment of read-only location
NE.c:16: error: incompatible types in assignment
NE.c:19: error: incompatible types in assignment

Thanks for any help,

Tyler

#include <stdio.h>

#define username_1 "greg"
#define password_1 "going"

int main(void) {

char usercheck[256];
char password[256];

printf("What is your username? ");
gets(usercheck);

if (username_1=usercheck) {
        printf("What is your password? ");
        gets(password);
        if (password = password_1) {
                printf("You May Enter!\n");
}
}
else {
        printf("access denied.\n");
}

return 0;
}

Use strcmp to compare strings.
Dont use gets.

Also, for future reference, the = sign signifies assignment. Later on, if you're comparing, for instance, numeric values then a double equals sign == is used to check for equality.

But, as the poster above me mentioned, used strcmp to compare strings.

To expand on the cryptic "Dont use gets" statement, see this.

Caveat:
Though I agree wholeheartedly about not using gets() , I would let it go in this and only this program
1) because you declared the arrays as 256
2) this is simply to test the concept of comparing strings
3) after this program you forget you ever heard of gets() Do we have a deal? :icon_wink:

if (username_1=usercheck) {
........
}
 if (password = password_1) {
..........
  }

You have misplaced Assignment operator(=)! You must be using equality check operator(==). Try this:

if (username_1 == usercheck) {
........
}
 if (password  ==  password_1) {
..........
  }
commented: Bad information, and too late anyway. -2
commented: Hey, cool. You feel the need to show off the fact that you don't understand the answers already posted. :rolleyes: -2

@latszer
Hello,
LINE 17 - You cannot assign a array to another array
I think you were trying to use the == sign but it is not allowed for strings, i'e, username_1==usercheck . It will not give any error but it will always be evaluated to false.
Use strcmp as Dave has pointed out
Also, don't use gets . Use his/her cousin fgets . I've demonstrated in the program below. Also read the link by WaltP

# include <stdio.h>
# include <string.h>

# define username_1 "greg"
# define password_1 "going"

int main(void)
{
    char usercheck[256];
    char password[256];

    printf("What is your username? ");
    fgets(usercheck,256,stdin);
    usercheck[strlen(usercheck) - 1] = '\0';    //  REMOVING NEWLINE FROM 
                                                //  THE END OF THE STRING
                                                
    if ( strcmp(username_1,usercheck) == 0 )
    {
        printf("What is your password? ");
        fgets(password,256,stdin);
        password[strlen(password) - 1] = '\0';  //  REMOVING NEWLINE FROM 
                                                //  THE END OF THE STRING

        if ( strcmp(password,password_1) == 0 )
        {
            printf("You May Enter!\n");
        }
    }
    else
    {
        printf("access denied.\n");
    }

    return 0;
}

@csmgsarma
Please clear your concepts of C
It's good that you're trying to help but please don't spread wrong knowledge

>fgets(usercheck,256,stdin);
Don't forget to check for failure. The last thing you want to do is dereference a null pointer on the next line.

>usercheck[strlen(usercheck) - 1] = '\0';
While this covers the average case, it's possible for fgets to succeed yet not end with a newline. Unless you want an edge case bug where a legitimate character is deleted, you should check for the presence of a newline first:

if (fgets(usercheck, sizeof usercheck, stdin) != NULL) {
  char *newline = strchr(usercheck, '\n');

  if (newline != NULL)
    *newline = '\0';

  /* ... */
}

On a side note, though it's a less efficient method and more obscure than the one above, I like the one-liner of strcspn:

if (fgets(usercheck, sizeof usercheck, stdin) != NULL) {
  usercheck[strcspn(usercheck, "\n")] = '\0';

  /* ... */
}

>usercheck[strlen(usercheck) - 1] = '\0';
While this covers the average case, it's possible for fgets to succeed yet not end with a newline. Unless you want an edge case bug where a legitimate character is deleted, you should check for the presence of a newline first:

I had completely forgotten about that
Thanks

>fgets(usercheck,256,stdin);
Don't forget to check for failure. The last thing you want to do is dereference a null pointer on the next line.

Can you give me an example as to where this error occurs?

>Can you give me an example as to where this error occurs?
Actual stream errors are rare, but end-of-file isn't, even for interactive input. Both are possible, and your code should take that into account.

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.