Thread: Login code help

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    17

    Login code help

    Need help, the warnings are: "initilization makes integer from pointer without cast" "comparison between pointer and integer" "comparison with string literal results in unspecified behaviour" If you could also tell me what those errors mean so I can fix it myself next time that would be great.

    Code:
    #include <stdio.h>
    
    
    int main() {
    char log_status = "";
    char username = "";
    char password = "";
    char password_r = "H";
    printf("Hello and welcome to Visualizer Creator! If you have an account, please type login. If not, please type register.");
    scanf(" %s ", &log_status);
    
    
    if (log_status == "login") {
        printf("Input Username: \n");
        scanf(" %s ", &username);
        printf("Input Password: \n");
        scanf(" %s ", &password);
        printf("Welcome to the program!");
    }
    else if (log_status == "register") {
        printf("Input Username: \n");
        scanf(" %s ", &username);
        printf("Input Password: \n");
        scanf(" %s ", &password);
        printf("Please input your Password again: \n");
        scanf(" %s ", &password_r);
    
    
        if (password == password_r) {
            printf("Thank you for registering!");
        }
    
    
        else {
            printf("Error, password must be the same both times");
        }
    
    
    
    
    }
    
    
    
    
    return 0;
    }

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Code:
    char log_status = "";
    char username = "";
    char password = "";
    char password_r = "H";
    You are not allocating space for the strings.

    Code:
    char log_status[256] = "";
    char username[256] = "";
    char password[256] = "";
    char password_r[256] = "H";
    Code:
    if (log_status == "login") {
    You cannot compare strings using the '==' operator, you need strcmp();

    You should use fgets() rather than scanf() to input strings, but be aware that fgets() brings in the newline at the end of the string which you will need to remove. (Please don't use gets())!!!

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    17
    OK, I fixed some of the things, this is the code now. There are still errors: "warning, passing argument 2 of fgets makes integer from pointer without a cast" "note: expected int but argument is of type char (*) [10]" "error: too few arguments to function fgets" and more. Please help IDK exactly how fgets works!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int main() {
    char log_status[10] = "";
    char username[10] = "";
    char password[10] = "";
    char password_r[10] = "H";
    printf("Hello and welcome to Visualizer Creator! If you have an account, please type login. If not, please type register.");
    fgets(" %s ", &log_status);
    
    
    if (strcmp(log_status, "login") == 0) {
        printf("Input Username:");
        fegets(" %s ", &username);
        printf("Input Password: \n");
        fgets(" %s ", &password);
        printf("Welcome to the program!");
    }
    else if (strcmp(log_status, "register") == 0) {
        printf("Input Username: \n");
        fgets(" %s ", &username);
        printf("Input Password: \n");
        fgets(" %s ", &password);
        printf("Please input your Password again: \n");
        fgets(" %s ", &password_r);
    
    
        if (password == password_r) {
            printf("Thank you for registering!");
        }
    
    
        else {
            printf("Error, password must be the same both times");
        }
    
    
    
    
    }
    
    
    
    
    return 0;
    }

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Before you use a Standard Library function, you need to look at how the function should be used. Most of your errors are related to fgets().

    If you are using gcc, then run this command:
    Code:
    $ man fgets
    Or else look at the same command online. The same with any other function.

    You don't need the '&' before "username", and other char arrays. "username" rather than "&username" in the fgets() calls. An array name is the address of the first element of the array.

  5. #5
    Registered User
    Join Date
    Apr 2017
    Posts
    17
    ok i remove the & sign but im still getting a bunch of errors, like "passing argument 2 of fgets creates integer from pointer without a cast" and for line 11 "too few arguments in fgets" as well as, somehow on line 354 (which doesn't exist???) "note: declared here" i have no idead how/what those errors mean and whati have to do to fix them. Please help! (code is the same as above, but the & signs have been removed from fgets)

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    So, what did the 'man' page for "fgets" tell you how to call fgets()?
    Code:
    char log_status[10] = "";
    char username[10] = "";
    char password[10] = "";
    char password_r[10] = "H";
    10 chars for a name or password is too small. I suggested 256:

    Given:
    Code:
    char log_status[256] = "";
    char username[256] = "";
    char password[256] = "";
    char password_r[256] = "H";
    The call to the first fgets() would be:
    Code:
    fgets(log_status, 256, stdin);
    This will input a maximum 255 chars, including the newline, from stdin, usually the keyboard, and place them in the char array, log_status.

    You will need to remove the newline from the end of the input line, before you compare with the string, "login".

    I will leave it to you to do the other calls to fgets().

    Again, please read the man page for fgets()!
    Last edited by rstanley; 05-11-2017 at 03:47 PM.

  7. #7
    Registered User
    Join Date
    Apr 2017
    Posts
    17
    OK, thank you very much. I didn't see the link the first time I read your reply lol. I changed the fgets(), and now there are no more errors. Unfortunately, nothing is being printed after I type login or register when I execute the program. DO you know how I can fix this?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int main() {
    char log_status[256] = "";
    char username[256] = "";
    char password[256] = "";
    char password_r[256] = "H";
    printf("Hello and welcome to Visualizer Creator! If you have an account, please type login. If not, please type register.");
    fgets(log_status, 256, stdin);
    
    
    if (strcmp(log_status, "login") == 0) {
        printf("Input Username: ");
        fgets(username, 256, stdin);
        printf("Input Password: ");
        fgets(password, 256, stdin);
        printf("Welcome to the program!");
    }
    else if (strcmp(log_status, "register") == 0) {
        printf("Input Username: ");
        fgets(username, 256, stdin);
        printf("Input Password: ");
        fgets(password, 256, stdin);
        printf("Please input your Password again: ");
        fgets(password_r, 256, stdin);
    
    
        if (strcmp(password, password_r) == 0) {
            printf("Thank you for registering!");
        }
    
    
        else {
            printf("Error, password must be the same both times");
        }
    
    
    
    
    }
    
    
    
    
    return 0;
    }

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by Joeyfish_13 View Post
    OK, thank you very much. I didn't see the link the first time I read your reply lol. I changed the fgets(), and now there are no more errors. Unfortunately, nothing is being printed after I type login or register when I execute the program. DO you know how I can fix this?
    Yes, as I said before, fgets() will bring in the newline at the input string. You will need to remove the newline BEFORE you compare the input string against a constant string.

  9. #9
    Registered User
    Join Date
    Apr 2017
    Posts
    17
    ok how do i remove the newline

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Find it and replace it with a '\0'. One way of doing this would be:
    Code:
    log_status[strcspn(log_status, "\n")] = '\0';
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Apr 2017
    Posts
    17
    Thank you so much! It works perfectly now! One last thing, would you be able to explain what that command means/does so I can now how to use it later? Thanks!

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C login code
    By Loj Buchanan in forum C Programming
    Replies: 6
    Last Post: 10-20-2015, 10:00 PM
  2. Login
    By Leandro Sousa in forum C Programming
    Replies: 3
    Last Post: 05-30-2014, 10:15 AM
  3. "simple" login authorization code
    By jkgoose937 in forum C Programming
    Replies: 44
    Last Post: 12-08-2013, 11:09 PM
  4. Replies: 8
    Last Post: 12-08-2009, 12:55 PM
  5. login
    By xlordt in forum Linux Programming
    Replies: 9
    Last Post: 02-25-2002, 10:31 AM

Tags for this Thread