|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
程序一开始输入一大串字符就会输出
一大串“请输入 Y 或 N 进行选择!”,该怎么解决
代码:
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char ny;
- char usrname[256];
- char password[256];
- char password_sure[256];
- char u[256];
- char p[256];
-
- printf("欢迎使用用户注册登录系统!\n您要注册还是登录?\nY 注册 / N 登录\n");
- do{
- scanf("%c", &ny);
-
- if (ny == 'Y') //注册语句块
- {
- do
- {
- printf("请输入您要注册的用户名:\n");
- scanf("%s", usrname);
- printf("请输入您的密码:\n");
- scanf("%s", password);
- printf("请再次输入密码:\n");
- scanf("%s", password_sure);
- if (0 == strcmp(password_sure, password)) //验证注册密码语句块
- {
- printf("注册成功,现在前往登陆\n");
- do //内嵌登录语句块
- {
- printf("请输入用户名:\n");
- scanf("%s", u);
- if (0 == strcmp(u, usrname))
- {
- do
- {
- printf("请输入登录密码:\n");
- scanf("%s", p);
- if (0 == strcmp(p, password))
- {
- printf("登录成功\n");
- }
- else
- {
- printf("输入错误\n");
- }
- }
- while (0 != strcmp(p, password));
- }
- else
- {
- printf("用户不存在!\n");
- }
- }
- while (0 != strcmp(u, usrname));
- }
- else //输入不一致的注册密码所执行的语句块
- {
- printf("两次输入的密码不一致,请再次注册!\n");
- }
- }
- while (0 != strcmp(password_sure, password));
- }
- else if (ny == 'N') //登录语句块
- {
- printf("请输入用户名:\n");
- do
- {
- scanf("%s", u);
- if (0 == strcmp(u, usrname))
- {
- do
- {
- printf("请输入登录密码:\n");
- scanf("%s", p);
- if (0 == strcmp(p, password))
- {
- printf("登录成功\n");
- }
- else
- {
- printf("输入错误\n");
- }
- }
- while (0 != strcmp(p, password));
- }
- else
- {
- printf("用户不存在!\n");
- }
- }
- while (0 != strcmp(u, usrname));
- }
- else
- {
- if (0 == strcmp(p, password))
- {
- break;
- }
- printf("请输入 Y 或 N 进行选择!\n");
- }
- }
- while (ny == 'Y' || 'N');
- printf("程序运行结束\n"); //检查是否正确结束了所有的 while 循环
-
- getch(); //接收到新的输入后退出程序
-
- return 0;
- }
复制代码
本帖最后由 ExiaGN001 于 2023-1-14 09:58 编辑
有用请设置最佳谢谢
[已证实]问题与缓冲区有关。
改进:
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char ny;
- char usrname[256];
- char password[256];
- char password_sure[256];
- char u[256];
- char p[256];
- printf("欢迎使用用户注册登录系统!\n您要注册还是登录?\nY 注册 / N 登录\n");
- do{
- scanf("%c", &ny);
- if (ny == 'Y') //注册语句块
- {
- do
- {
- printf("请输入您要注册的用户名:\n");
- scanf("%s", usrname);
- printf("请输入您的密码:\n");
- scanf("%s", password);
- printf("请再次输入密码:\n");
- scanf("%s", password_sure);
- if (0 == strcmp(password_sure, password)) //验证注册密码语句块
- {
- printf("注册成功,现在前往登陆\n");
- do //内嵌登录语句块
- {
- printf("请输入用户名:\n");
- scanf("%s", u);
- if (0 == strcmp(u, usrname))
- {
- do
- {
- printf("请输入登录密码:\n");
- scanf("%s", p);
- if (0 == strcmp(p, password))
- {
- printf("登录成功\n");
- }
- else
- {
- printf("输入错误\n");
- }
- }
- while (0 != strcmp(p, password));
- }
- else
- {
- printf("用户不存在!\n");
- }
- }
- while (0 != strcmp(u, usrname));
- }
- else //输入不一致的注册密码所执行的语句块
- {
- printf("两次输入的密码不一致,请再次注册!\n");
- }
- }
- while (0 != strcmp(password_sure, password));
- }
- else if (ny == 'N') //登录语句块
- {
- printf("请输入用户名:\n");
- do
- {
- scanf("%s", u);
- if (0 == strcmp(u, usrname))
- {
- do
- {
- printf("请输入登录密码:\n");
- scanf("%s", p);
- if (0 == strcmp(p, password))
- {
- printf("登录成功\n");
- }
- else
- {
- printf("输入错误\n");
- }
- }
- while (0 != strcmp(p, password));
- }
- else
- {
- printf("用户不存在!\n");
- }
- }
- while (0 != strcmp(u, usrname));
- }
- else
- {
- if (0 == strcmp(p, password))
- {
- break;
- }
- printf("请输入 Y 或 N 进行选择!\n");
- while(getchar()!='\n'); //唯一新增行,用途是将该行所有剩余数据无效化
- }
- }
- while (ny == 'Y' || ny=='N'); //有改,是逻辑错误
- printf("程序运行结束\n"); //检查是否正确结束了所有的 while 循环
- getchar(); //接收到新的输入后退出程序
- return 0;
- }
复制代码
PS: 该程序的运行逻辑变更:对于执行15行时得到的无效输入,每有1行无效输入,就会导致程序输出1遍 “ 请输入 Y 或 N 进行选择!”
PS2: 请尽可能避免使用 getch(),此函数不被现行标准支持,会导致bug。
使用getchar()而非getch(),以获取更好的代码可维护性
有用请设置最佳谢谢
|
|