鱼C论坛

 找回密码
 立即注册
查看: 1635|回复: 3

[已解决]char变量 问题

[复制链接]
发表于 2023-1-13 21:54:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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;
}
最佳答案
2023-1-13 22:11:01
本帖最后由 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(),以获取更好的代码可维护性

有用请设置最佳谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-1-13 22:11:01 | 显示全部楼层    本楼为最佳答案   
本帖最后由 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(),以获取更好的代码可维护性

有用请设置最佳谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-14 09:30:13 | 显示全部楼层
while (ny == 'Y' || 'N');这个语句是不是改写成while (ny == 'Y' || ny == 'N');因为while (ny == 'Y' || 'N')等价于while ((ny == 'Y') || ('N'))——此处==的优先级高于||;,不然当ny不是‘Y’时,||后面的‘N’编码不是0,将永远为真,形成死循环。。。这只是个人粗略看了一下的想法,仅供参考。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2023-1-14 12:35:28 | 显示全部楼层
ExiaGN001 发表于 2023-1-13 22:11
有用请设置最佳谢谢

while (ny == 'Y' ||  ny=='N');
改为
while (ny != 'Y' ||  ny=='N');     
就没问题了,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-7 19:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表