我正在输入用户名和密码。在此之前,当我输入 backspace 而不是擦除密码时, backspace 输入了一些内容,然后我想使用ASCII,但是我不知道该脚本用于删除密码。
int a=0, i=0;
char uname[10], c=' ';
char pword[10], code[10];
char user[10]="user";
char pass[10]="pass";
a:
system("cls");
printf("\n\n\t\t\t======================================");
printf("\n\n\t\t\t| STUDENT REPORTING SCORE SYSTEM |");
printf("\n\n\t\t\t======================================");
printf("\n\n\t\t\t=========== LOGIN FIRST ============");
printf("\n\n\n\t\t\t\tENTER USERNAME : ");
scanf("%s", &uname);
printf("\n\n\t\t\t\tENTER PASSWORD : ");
while(i<10)
{
pword[i]=getch();
c=pword[i];
if(c==13) break;
else if(c==8)
//here is the blank
else printf("*");
i++;
}
pword[i]='\0';
i=0;
if(strcmp(uname,"user")==0 && strcmp(pword,"pass")==0)
{
printf("\n\n\n\t\t\tWELCOME TO STUDENT REPORTING SCORE SYSTEM\n\t\t\t\t LOGIN IS SUCCESSFUL");
printf("\n\n\n\t\t\t Press any key to continue...");
getch();
}
else
{
printf("\n\n\n\t\t\t SORRY !!!! LOGIN IS UNSUCESSFUL");
getch();
goto a;
}
我不知道该怎么写//here is the blank
。因此,当我使用a-=2
时,它不想删除*
,并且也不会输入任何内容。
答案 0 :(得分:0)
我对您的代码做了一些修改。
在我的系统上使用了getch()
,因此我使用_getch()
代替它。经过printf之后,我在下一个'\0'
处得到了一个_getch()
字符。因此,我必须在if (c == '\0') continue;
行中忽略它。
要删除'*'
字符,您必须打印"\b \b"
(\b
后退一个字符。' '
会覆盖'*'
和'\b'
再退一步。
如果用户键入10个字符作为密码,则代码中会有缓冲区溢出。您必须为最后一个10 + 1
字符分配'\n'
长度数组。
我认为您应该使用std::string
而不是char*
,并从代码中删除未使用的变量。
希望对您有帮助。
bool login(std::string& username, std::string& password)
{
username = "";
password = "";
char c = ' ';
system("cls");
printf("\n\n\t\t\t======================================");
printf("\n\n\t\t\t| STUDENT REPORTING SCORE SYSTEM |");
printf("\n\n\t\t\t======================================");
printf("\n\n\t\t\t=========== LOGIN FIRST ============");
printf("\n\n\n\t\t\t\tENTER USERNAME : ");
std::cin >> username;
printf("\n\n\t\t\t\tENTER PASSWORD : ");
while (true)
{
c = _getch();
if (c == '\r') break;
if (c == '\0') continue;
if (c == '\b')
{
if (password.length() > 0)
{
password.pop_back();
printf("\b \b");
}
}
else
{
printf("*");
password.push_back(c);
}
}
_getch(); // Read the extra '\0'
if (username == "user" && password == "pass")
{
printf("\n\n\n\t\t\tWELCOME TO STUDENT REPORTING SCORE SYSTEM\n\t\t\t\t LOGIN IS SUCCESSFUL");
return true;
}
else
{
printf("\n\n\n\t\t\t SORRY !!!! LOGIN IS UNSUCESSFUL");
return false;
}
}
int main()
{
while (true)
{
std::string username = "";
std::string password = "";
auto successLogin = login(username, password);
if (successLogin)
break;
_getch(); // Press any key to continue...
}
return 0;
}
答案 1 :(得分:0)
void login()
c:
system("cls");
char choice='Y', input, passChar[100],userChar[100];
for (int i=0; i<100; userChar[i++]=0);
while(choice=='Y'){
system ("cls");
cout<<"\n\n\t\t\t======================================";
cout<<"\n\n\t\t\t| STUDENT SCORE REPORTING SYSTEM |";
cout<<"\n\n\t\t\t======================================";
cout<<"\n\n\t\t\t=========== LOGIN FIRST ============";
cout<<"\n\n\n\t\t\t\tENTER USERNAME : ";
cin.getline(userChar,100);
system("cls");
cout<<"\n\n\t\t\t======================================";
cout<<"\n\n\t\t\t| STUDENT SCORE REPORTING SYSTEM |";
cout<<"\n\n\t\t\t======================================";
cout<<"\n\n\t\t\t=========== LOGIN FIRST ============";
cout<<"\n\n\n\t\t\t\tENTER PASSWORD: ";
for (int i=0;;i++)
{
input=getch();
if ((input!=8)&&(input!=13))
{
passChar[i]=input;
}
else if (input==8)
i-=2;
else if (input==13)
break;
system("cls");
cout<<"\n\n\t\t\t======================================";
cout<<"\n\n\t\t\t| STUDENT SCORE REPORTING SYSTEM |";
cout<<"\n\n\t\t\t======================================";
cout<<"\n\n\t\t\t=========== LOGIN FIRST ============";
cout<<"\n\n\n\t\t\t\tENTER PASSWORD: ";
for (int j=0; j<i+1; j++)
cout<<"*";
}
cout<<endl;
if (!strcmp(userChar, "admin"))
{
cout<<"UserName correct!\n";
}
else
cout<<"Username incorrect!\n";
if (!strcmp(passChar, "pass"))
{
cout<<"Password correct!\n";
}
else
cout<<"Password incorrect!\n";
if ((!strcmp(userChar,"admin"))&&(!strcmp(passChar, "pass")))
{
cout<<"Access Granted!";
getch();
return;
}
else
cout<<"Acess denied!\n";
cout<<"Do you want to try again? Y/N : ";
cin>>choice;
choice=toupper(choice);
cin.ignore(100,'\n');
for (int i=0; i<100; userChar[i++]=0);
for (int i=0; i<100; passChar[i++]=0);
}}