-3

I'm trying to scan the input from terminal and I'm trying to scan the initial white space but the program just skip it. I tried using this method before in a different program but it doesn't work in my new one. Plz help!!!

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

#define ADMIN_PASS "ABC123"
#define MAX_ARR_LEN 20
#define debug

void getinput(char inp[], int n);
void password(char passUser[]);

int main(void)
{      
    char passUser[MAX_ARR_LEN+1];
    int i=1;
    while (i==1)
    {
        password(passUser);
        printf("Try again?(1/0)>");
        scanf("%d",&i);
        if (i == 1)
        printf("\n");
    }
    return 0;
}

void getinput(char inp[], int n)
{
    scanf("%[^\n]c", &inp[n-1]);
    #ifdef debug
        printf("\nThe entered code in function>%s\n",inp);
        printf("The 1st character of entered code in function>%c\n",inp[0]);
    #endif
}

void password(char passUser[])
{
    char admin[MAX_ARR_LEN+1] = ADMIN_PASS;
    do
    {
        printf("\nPlease enter the Administrator password to Login:\n");
        getchar();
        getinput(passUser);
        #ifdef debug
            printf("\nThe input password in main is>%s\n", passUser);
            printf("The 1st character in main is>%c\n", passUser[0]);           
        #endif
        if (strcmp(passUser, admin) != 0)
        {
            printf("The password entered is incorrect, try again\n");
        }
    } while (!(strcmp(passUser, admin) == 0));   
}
hry0918
  • 35
  • 4
  • 3
    it doesn't compile at all. getinput requires two args instead of one! I think you should start from this point – Nick S Oct 05 '18 at 11:06
  • 1
    Copy/paste the actual code that you tested. If you do not, typos and other transcription errors will waste time and effort:( – Martin James Oct 05 '18 at 11:10

1 Answers1

0

You should pass the string with fgets(inp, sizeof(ADMIN_PASS), stdin) like this:

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

#define ADMIN_PASS "ABC123"
#define MAX_ARR_LEN 20
#define debug

void getinput(char * inp);
void password(char * passUser);

int main(void)
{
    char passUser[MAX_ARR_LEN+1];
    int i=1;
    while (i==1)
    {
        password(passUser);
        printf("Try again?(1/0)>");
        scanf("%d",&i);
        if (i == 1)
        printf("\n");
    }
    return 0;
}

void getinput(char * inp)
{
    fgets(inp, sizeof(ADMIN_PASS), stdin);
    #ifdef debug
        printf("\nThe entered code in function>%s\n",inp);
        printf("The 1st character of entered code in function>%c\n",inp[0]);
    #endif
}

void password(char * passUser)
{
    char admin[MAX_ARR_LEN+1] = ADMIN_PASS;
    do
    {
        printf("\nPlease enter the Administrator password to Login:\n");
        getinput(passUser);
        #ifdef debug
            printf("\nThe input password in main is>%s\n", passUser);
            printf("The 1st character in main is>%c\n", passUser[0]);
        #endif
        if (strcmp(passUser, admin) != 0)
        {
            printf("The password entered is incorrect, try again\n");
        }
    } while (!(strcmp(passUser, admin) == 0));
}

I removed getchar() and the second parameter of the function getinput() cause they were useless.

Stefano Raneri
  • 323
  • 4
  • 14
  • I tried compling it on terminal and tried a sample input " ABC 123". It doesn't recognize the 1st white space – hry0918 Oct 05 '18 at 13:58
  • Ops, my bad, i think a better solution is to use fgets instead of scanf, cause if you do scanf("%[^\n]", inp), with the first character as a whitespace, it produces a loop error. So it's better instead to use fgets(inp, MAX_ARR_LEN, stdin). Now it should work. – Stefano Raneri Oct 05 '18 at 16:29
  • You're welcome, feel free to upvote my answer, or mark it as the solution. – Stefano Raneri Oct 06 '18 at 12:33
  • Hey, I tried using the fgets but i ran into a problem when compling it. Because the parameter is MAX_ARRAY_LEN, it cause the input to be different from the admin password. When I entered the correct password, it prompts the message wrong password – hry0918 Oct 06 '18 at 12:42
  • 1
    Instead of MAX_ARR_LEN use sizeof(ADMIN_PASS). – Stefano Raneri Oct 06 '18 at 15:04