创建帐户后,用户无法登录

时间:2015-11-14 14:10:33

标签: c cs50

我正在尝试在C中自己创建一个简单的应用程序。它可以帮助任何人创建他/她的帐户并保存她的个人信息,这是安全的。直到现在用户可以记录他/当用户回来记录时,他不能。我编写并使用文件I / O来保存用户凭据..

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

int main (int argc, string argv[])
{
    // The pointer to the File
    FILE* accountsDatabase = fopen("accountsDatabase.txt", "r+");

    char Fdecision;
    char Gateway = 'Y';
    char Gatewaydenied = 'N';

    string stringGateway = "Yes";
    string stringGatewayDenied = "No";
    string information;
    string Newusername;
    string Newpassword;
    string Universalpassword;
    string Universalusername;

    printf("\n \t \n");

    printf("\tThis is Dell login interference.....");
    printf("\n");
    printf("\t#################################\n");
    printf("\t#################################\n");

    printf("\n");
    printf("\n");

    printf(" Please inset your id and password in below user friendly interface.");
    printf("\n");
    printf("\n");

    printf("Please type answer in Y and N");
    printf("\n");
    printf("Do you want the Dell's login user interfae: ");
    Fdecision = GetChar();

    if (Fdecision == Gateway)
    {
        printf("Do you want to create a new user. Answer in Yes or NO: ");
        string userChoice = GetString();
        printf("\n");
        if (strcmp(stringGateway, userChoice) == 0)
        {
            printf("To create a New User Account.... Fill following boxes..\n");
            printf("\n");
            printf("username: ");
            Newusername = GetString();
            printf("password: ");
            Newpassword = GetString();
            printf("\n");
            printf("Your personal textBook..:: ");
            information = GetString();
            printf("\n");
            // Checks if it is opened..
            if (accountsDatabase == NULL)
            {
                printf("Error opening file:\n");
            }
            else
            {
                fputs(Newusername, accountsDatabase);
                fputs(Newpassword, accountsDatabase);
            }
            printf("Do you want to login.? Answer in Yes or No: ");
            string LoginDecision = GetString();
            if(strcmp(stringGateway, LoginDecision) == 0)
            {
                printf("Username: ");
                string username = GetString();
                printf("\n");
                if (strcmp(Newusername, username) == 0)
                {
                    printf("Password: ");
                    string password = GetString();
                    if (strcmp(Newpassword, Newpassword) == 0)
                    {
                        printf("\n");
                        printf("Confirming Identity.....\n");
                        printf("Identity Confirmed.....\n");
                        printf("\n");
                        printf("Do you want to reveal your sensitive information: ");
                        string secondDecision = GetString();
                        if (strcmp (stringGateway, secondDecision) == 0)
                        {
                            printf("\n");
                            printf("Enter password:: ");
                            string Currentpassword = GetString();
                            if (strcmp (Newpassword, Currentpassword) == 0)
                            {
                                printf("\n");
                                printf("%s\n", information);
                                printf("\n\n");
                            }
                        }
                    }
                }
            }
        }
        else if (strcmp(stringGatewayDenied, userChoice) == 0)
        {
            printf("\n");
            printf("Username: ");
            string username = GetString();
            printf("\n");


            // Checks if it is opened..
            if (accountsDatabase == NULL)
            {

                printf("Error opening file:\n");
            }
            else
            {
                fgets(Universalusername, "%s", accountsDatabase);


                if (strcmp(Universalusername, username) == 0)
                {
                    printf("Password: ");
                    string password = GetString();

                    if (strcmp(Universalpassword, password) == 0)
                    {
                        printf("\n");
                        printf("Confirming Identity.....\n");
                        printf("Identity Confirmed.....\n");
                        printf("\n");
                        printf("Do you want to reveal your sensitive information: ");
                        string secondDecision = GetString();
                        if (strcmp (stringGateway, secondDecision) == 0)
                        {
                            printf("\n");
                            printf("Enter password:: ");
                            string Currentpassword = GetString();
                            if (strcmp (Universalpassword, Currentpassword) == 0)
                            {
                                printf("\n");
                                printf("%s\n", information);
                                printf("\n\n");
                            }
                        }

                    }
                }
            }
            printf("Universalusername : %s", Universalusername);
        }
        else
        {
            printf("Take these security measures...\n");
            printf("##############################");
            printf("\n");
            printf(" 1) You need specil previlage to access sensitive information...\n");
            printf(" 2) Please run in a minute either alarm will be trigered..\n");
            printf(" 3) Security camera are availabel, \n 4) run now.\n");
        }

    }
    else if (Fdecision == Gatewaydenied)
    {
        printf("\n");
        printf("You can get off from front door.\n");
        printf("Don't LOok Back, it's not safe..\n'");
        printf("What do you think? Can you cheat me? \n");
        printf("No, you cannot!");
    }

    else
    {
        printf("\n");
        printf("Please choose between Yes (Y) or No (N)\n");
        printf("Not the full spelling but ony a single abbreviated letter..\n");
        printf("Y or N..\n");
    }

    fclose(accountsDatabase);
}

1 个答案:

答案 0 :(得分:-2)

您的代码不那么清晰也不易阅读。为了帮助你,我写了这段代码,你可以在日常编码中使用它,这可以让你的生活更轻松。希望这可以提供帮助。

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



input(char *prompt,char *buff);
int YesNoQuetion(char *prompt);

int main (void)
{
    char firstname[50];
    char lastname[50];
    printf("This is a little code that will help you create a clean code\n\n");
    printf("Please enter your first and last name\n");

    input("First name",firstname);
    input("Last name",lastname);

    if (YesNoQuetion("Do you realy want to know?"))
    {
       printf("\nThat's good '%s, %s', you just pressed Yes\n",firstname,lastname);
    }
    else{
         printf("\nYou answerd No, that is also good\n");;
    }

  return 0;
}

 /*__________________________________________________________________________________
*/
int YesNoQuetion(char *prompt){
    int r=0,c;
    printf("%s (Y/N) : ",prompt);
    while(1){
        c=_getch();
        if(c=='n' || c=='N' || c=='y' || c=='Y'){
            printf("%c\x8",c);
            r=c;
        }else{
            if( (c=='\n'||c=='\r') && r)
                break;
            r=0;
            printf("%d\x8\x7",c);
        }
    }
    printf("%s\n",(r=='y'|| r=='Y')?"Yes":"No");
    return (r=='y'|| r=='Y');

}
  /*__________________________________________________________________________________
*/
int input(char *prompt,char *buff){
    printf("%s :\t",prompt);
    return scanf("%s",buff);
}

/*__________________________________________________________________________________
*/