0
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

  char username[12] = "admin";
  char password[12] = "admin";


  system("color 9f");
  system("cls");
  login:
  printf("                        ---------------------------------- \n");
  printf("                       |                                  |\n");
  printf("                       |      WELCOME TO OMAR WATER'S     |\n");
  printf("                       |             INVENTORY            |\n");
  printf("                       |              SYSTEM              |\n");
  printf("                       |                                  |\n");
  printf("                        ---------------------------------- \n\n\n");
  printf("Please Log-In...\n\n");
  printf("Username: ");
  scanf("%c", &username);

  if (username == "admin"){
    printf("\nPassword: ");
    scanf("%c", &password);

    if (password == "admin"){
      printf("\n\nWELCOME ", &username);
    }else{
      printf("INVALID PASSWORD");
      getchar();
      system("cls");
      goto login;
    }

  }else {
    printf("INVALID USERNAME");
    getchar();
    system("cls");
    goto login;
  }


  getch();
  return 0;
}

console application; c (not c++);

I am currently in the part of my program where I want to ask for a preset username and password. When I enter the correct username, instead of asking for the password, it just flashes the 'enter password' and jumps directly to the else statement. and then does the goto login; code.

Can anyone point me to the right direction on how to make this work? By the way, I am using dev-c++ IDE.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
MBN
  • 63
  • 1
  • 1
  • 7
  • This is a very common problem that happens to programmers used to compare strings with `==` in languages like c++ for example, which has a `string` type, there is no such thing in c, and you need to compare strings one character at a time, there are of course functions in the standard library that do this for you efficiently, and are easy to use. – Iharob Al Asimi May 14 '15 at 15:31
  • 1
    IMHO, dup is not proper . This Q has __multiple__ issues. Would you mind re-cosidering the closing Mr @iharob? – Sourav Ghosh May 14 '15 at 15:31

1 Answers1

6

Point 1

In your code

 scanf("%c", &username);

is wrong, it will scan only one char to username[0], leaving all other inputs into the buffer, which will be automatically read in later calls.

use

scanf("%11s", username);

to scan for string inputs.

Point 2

You don't comapre strings like

 username == "admin"

you need to use strcmp()

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261