就像标题所说的,我试图创建一个注册系统,但由于某种原因,它覆盖了应该是defaultuser[0]=“12291955”的数组,它变成了我输入的内容,除了我想让它转到defaultuser[3](用x=3表示)。所以基本上,注册系统应该添加第四个用户。(以下是我的main.c文件的头文件)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
void loginsystem(){
char defaultuser[][100] = {"12291995", "12221999", "12241344"};
char defaultpass[][100] = {"test1", "test2", "test3"};
int i = 0, indicator = 1, x = 3;
char userinput[20], passinput[20], choicest[20], prog[20];
const char* choicecmp;
system("cls");
printf("\nSystem");
printf("\nWould you like to create an account or log into an existing account? (Type Signup or Login) ");
scanf(" %[^\n]s", choicest);
printf("%d", x); //x=3
choicecmp = strlwr(choicest);
if(strcmp(choicecmp, "signup")==0 || strcmp(choicecmp, "sign up") == 0 )
{
//system("cls");
printf("\nEnter your ID Number: ");
scanf(" %s", defaultuser[x]);
printf("\nEnter your password: ");
scanf(" %s", defaultpass[x]);
getch();
choicecmp = "login";
x++;
printf("%d", x); //x becomes 1 for some reason
getch();
}
if(strcmp(choicecmp, "login")==0 || strcmp(choicecmp, "log in") == 0 ){
do{
system("cls");
printf("\nPlease login!");
printf("\n\nUsername (ID Number): ");
scanf(" %s", &userinput);
printf("\nPassword: ");
do{
passinput[i] = getch();
if(passinput[i] != '\r')
printf("*");
i++;
}while(passinput[i-1] != '\r');
passinput[i-1] = '\0';
for(int i = 0; i < x; i++){
if(strcmp(userinput, defaultuser[i]) == 0 && strcmp(passinput, defaultpass[i]) == 0)
{
system("cls");
indicator = 0;
printf("\nWelcome back User");
}}
return ;
}
我试着调试代码,结果发现int x=3在注册系统中变成了x=0,在x++之后变成了1。