This blog is under construction

Tuesday 9 July 2013

C program to clear screen and authenticate user

Write a C program to clear screen and authenticate user.


  #include <stdio.h>
  #include <string.h>
  int main() {
        char username[10], user[2][10] = {"Sam", "Anderson"};
        char password[10], pass[2][10] = {"hello", "world"};
        int i = 0, ch;

        /* get the username from the user */
        printf("Username:");
        fgets(username, 10, stdin);
        username[strlen(username) - 1] ='\0';

        /* get the password from the user */
        printf("Password:");
        fgets(password, 10, stdin);
        password[strlen(password) - 1] = '\0';

        /* clear the screen */
        system("clear");

        /* if user is not sam/anderson, then he is invalid user */
        if (strcmp(username, user[0]) != 0 &&
                strcmp(username, user[1]) != 0) {
                printf("Invalid user!!\n");
                return 0;
        }

        /* authenticating the given user */
        if (strcmp(username, user[0]) == 0 &&
                strcmp(password, pass[0]) == 0) {
                printf("Hello Sam!! ");
                printf("Welcome To See-Programming!!\n");
        } else if (strcmp(username, user[1]) == 0 &&
                strcmp(password, pass[1]) == 0) {
                printf("Hello Anderson!! ");
                printf("Welcome to See-Programming!!\n");
        } else {
                printf("Invalid User!!\n");
        }
        return 0;
  }



  Output:
Hello Sam!! Welcome To See-Programming!!







See Also:

No comments:

Post a Comment