0

我想做的是为管理员存储用户名和密码,以便我可以将用户输入的内容与存储的内容进行比较,但我不知道如何存储它并使用指针,我认为我应该使用 strcpy 但现在我很困惑......对不起,我希望能解释我需要帮助的内容

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

struct profile
{

char First[15];
char Last[15];
char Pwd[10];
char UserName[10];
};

void main_menu(struct profile user[]);
void admin_signIn(struct profile user[]);
void userSignUp(struct profile user[]);
void userSignOn(struct profile user[]);
void quit(struct profile user[]);
void admin_menu(struct profile user[]);

int main(int argc, char *argv[])
{
   struct profile user[100];
   struct profile *puser;
   puser=&user[0];   
   puser.UserName[0] = "a";
 //puser.Pwd = "password";


   main_menu(user);


  system("PAUSE");
  return 0;
}

void main_menu(struct profile user[])
{
    int choice = 0;

   printf("User Menu\n");
   printf("          \n");
   printf("1 - Admin Sign In\n");
   printf("2 - Sign Up\n");
   printf("3 - Sign On\n");
   printf("4 - Quit\n");
   printf("Enter Choice:");
   scanf("%d", &choice);
   getchar();

   if (choice == 1)
    admin_signIn(user);
    else
       if (choice == 2)
          userSignUp(user);
          else
              if(choice == 3)
                userSignOn(user);
                else
                    if(choice == 4)
                    quit(0);


}

void admin_signIn(struct profile *puser)
{
     int i=0;
     char buff_in[20];
     //UserName="admin";
     //puser->Pwd = "password";
     //user[0].Last = "     ";
     //user[0].UserName="admin";
     //user[0].Pwd="password";


     //do
     //{
     printf("Enter admin user name:");
     fgets(buff_in,10,stdin);
     strcmp((puser+i)->UserName,buff_in);

     printf("Enter admin password:");
     fgets(buff_in,10,stdin);
     strcmp((puser+i)->Pwd,buff_in);
     i++;
     fflush(stdin);

     //printf("the user name is %s:", user[0].UserName);

     //if(user[i].UserName==user[0].UserName && user[i].Pwd ==user[0].Pwd)
              admin_menu(user);
     else
        printf("try again");
    // }while(i<2);
}

void userSignUp(struct profile user[])
{
     int i=0;

 do
 {
     printf("Enter user name that you would like to use:\n");
     gets(user[i].UserName);
     //printf("enter user password:");
     //gets(user[i].Pwd);
     i++;


  }while(i<2);
}

void userSignOn(struct profile user[])
{
     printf("you are in the user sign on menu\n");
}

void quit(struct profile user[])
{
     printf("the is the quit function\n");
}

void admin_menu(struct profile user[])
{
     printf("you are in the admin menu\n");
}
4

1 回答 1

2
puser.UserName[0] = "a";  // This is wrong. Needs to be 'a'

puser.UserName[0]是 类型char并且"a"是 类型const char*

使用strncpy -

strncpy( puser.Pwd, "password", strlen("password") );

注意:目标中的源和空终止字符应该有足够的空间。

于 2012-04-21T14:22:48.547 回答