I';我试图在我的c程序登录中应用do-while循环,但我不明白
请帮我解决我的问题I';我试图在我的c程序登录中应用do-while循环,但我不明白,c,C,请帮我解决我的问题 #include <stdio.h> #include <string.h> int samplepre () { do { char username[3]; // users's input username char password[3]; //user's input password char choice; // Main menu choice char x; // option try aga
#include <stdio.h>
#include <string.h>
int samplepre ()
{
do {
char username[3]; // users's input username
char password[3]; //user's input password
char choice; // Main menu choice
char x; // option try again
char string;
int i; // for for loop : index
printf ("\t************\n");
printf ("\t*Login Menu*\n");
printf ("\t************\n");
printf ("Enter your username:"); // The user is ask to input its username
scanf ("%s",username);
printf ("Enter password:"); // The user is ask to input its password
scanf ("%s",password);
if (strcmp(username,"admin") == 0 && strcmp(password,"vdedote") == 0)
{ // if the condition is true the set of codes will execute
system ("cls");
printf ("****************\n");
printf ("*WELCOME ADMIN!*\n");
printf ("****************\n");
system ("PAUSE");
//printf ("\n");
printf ("What may I be of service sir?\n");
printf ("************\n");
printf ("*MAIN MENU:*\n");
printf ("************\n");
//printf ("\n");
printf ("[A] Remove Vowels\n"); // The user will be asked of his own choice
printf ("[B] Remove Entered Character\n");
printf ("[C] Arithmetic\n");
printf ("Select your choice:");
scanf ("%s",choice);
}
else
{ // if the user inputs wrong data
// x = option try again
printf ("****************\n");
printf ("*ACCESS DENIED!*\n");
printf ("****************\n");
printf ("Would you like to try again <Y/N>");
scanf ("%s",x);
}
while (strcmp(username,"admin") == 1 && strcmp(password,"vdedote") == 1);
}
#包括
#包括
int samplepre()
{
做{
字符用户名[3];//用户的输入用户名
字符密码[3];//用户的输入密码
char choice;//主菜单选项
char x;//选项重试
字符串;
int i;//for循环:索引
printf(“\t***************\n”);
printf(“\t*登录菜单*\n”);
printf(“\t***************\n”);
printf(“输入用户名:”;//要求用户输入用户名
scanf(“%s”,用户名);
printf(“输入密码:”;//要求用户输入密码
scanf(“%s”,密码);
if(strcmp(用户名,“admin”)==0&&strcmp(密码,“vdedote”)==0)
{//如果条件为真,则将执行一组代码
系统(“cls”);
printf(“******************\n”);
printf(“*欢迎管理员!*\n”);
printf(“******************\n”);
系统(“暂停”);
//printf(“\n”);
printf(“先生,我能为您服务吗?\n”);
printf(“************\n”);
printf(“*主菜单:*\n”);
printf(“************\n”);
//printf(“\n”);
printf(“[A]删除元音\n”);//将要求用户自行选择
printf(“[B]删除输入的字符\n”);
printf(“[C]算术\n”);
printf(“选择您的选择:”);
scanf(“%s”,选项);
}
其他的
{//如果用户输入了错误的数据
//x=选项重试
printf(“******************\n”);
printf(“*访问被拒绝!*\n”);
printf(“******************\n”);
printf(“您想再试一次吗”);
scanf(“%s”,x);
}
while(strcmp(用户名,“admin”)==1和strcmp(密码,“vdedote”)==1);
}
当字符串相等时,strcmp()
返回0
返回值
strcmp()
和strncmp()
函数返回的整数小于,
如果s1
(或其第一个n
字节),则等于或大于零
分别发现小于、匹配或大于
s2
当字符串相等时,strcmp()
返回0
返回值
strcmp()
和strncmp()
函数返回的整数小于,
如果s1
(或其第一个n
字节),则等于或大于零
分别发现小于、匹配或大于
s2
您在
while
之前缺少一个右括号}
修复此语法错误后,请确保理解如果字符串相等,
strcmp
将返回零。在while
之前缺少一个右大括号}
修复此语法错误后,请确保了解如果字符串相等,
strcmp
将返回零。因为您没有关闭do
do{.......}
while(...);
因为你没有关闭do
do{.......}
while(...);
while
之前缺少一个}
李>
scanf
用户名
和密码分配足够的空间
while
之前缺少一个}
李>
scanf
用户名
和密码分配足够的空间
谢谢你的提问
- 更正语法错误(大括号)后
- 在使缩进看起来更好之后
- 在你理解了strcmp的概念之后
- 当你明白这样做是非常危险的
while (strcmp(username,"admin") != 0 && strcmp(password,"vdedote") != 0);
因此循环知道它需要重复,因为它不是具有正确密码登录的管理员 谢谢你的提问
- 更正语法错误(大括号)后
- 在使缩进看起来更好之后
- 在你理解了strcmp的概念之后
- 当你明白这样做是非常危险的
while (strcmp(username,"admin") != 0 && strcmp(password,"vdedote") != 0);
因此循环知道它需要重复,因为它不是具有正确密码登录的管理员 你有什么问题?请说得更具体一些。输入结束时需要一个“while”更新您的问题,描述您遇到的困难。我如何才能使其不相等?@iharob我希望我的程序执行do-while循环,例如,如果用户输入了错误的用户名和密码,我希望程序再次询问,直到输入正确的用户名和密码。你有什么问题?请说得更具体一些。输入结束时需要一个“while”更新您的问题,描述您遇到的困难。我如何才能使其不相等?@iharob我希望我的程序执行do-while循环,例如,如果用户输入了错误的用户名和密码,我希望程序再次询问它,直到它键入正确的用户名和密码。它希望在测试结束时有一个“while”