鱼C论坛

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

[已解决]char变量 问题

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

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

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

x
程序一开始输入一大串字符就会输出
一大串“请输入 Y 或 N 进行选择!”,该怎么解决
代码:
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main()
  4. {
  5.         char ny;
  6.         char usrname[256];
  7.         char password[256];
  8.         char password_sure[256];
  9.         char u[256];
  10.         char p[256];
  11.        
  12.         printf("欢迎使用用户注册登录系统!\n您要注册还是登录?\nY 注册 / N 登录\n");
  13.         do{
  14.                 scanf("%c", &ny);
  15.        
  16.                 if (ny == 'Y') //注册语句块
  17.                 {
  18.                         do
  19.                         {
  20.                                 printf("请输入您要注册的用户名:\n");
  21.                                 scanf("%s", usrname);
  22.                                 printf("请输入您的密码:\n");
  23.                                 scanf("%s", password);
  24.                                 printf("请再次输入密码:\n");
  25.                                 scanf("%s", password_sure);
  26.                                 if (0 == strcmp(password_sure, password)) //验证注册密码语句块
  27.                                 {
  28.                                         printf("注册成功,现在前往登陆\n");
  29.                                         do //内嵌登录语句块
  30.                                         {
  31.                                                 printf("请输入用户名:\n");
  32.                                                 scanf("%s", u);
  33.                                                 if (0 == strcmp(u, usrname))
  34.                                                 {
  35.                                                         do
  36.                                                         {
  37.                                                                 printf("请输入登录密码:\n");
  38.                                                                 scanf("%s", p);
  39.                                                                 if (0 == strcmp(p, password))
  40.                                                                 {
  41.                                                                         printf("登录成功\n");
  42.                                                                 }
  43.                                                                 else
  44.                                                                 {
  45.                                                                         printf("输入错误\n");
  46.                                                                 }
  47.                                                         }
  48.                                                         while (0 != strcmp(p, password));
  49.                                                 }
  50.                                                 else
  51.                                                 {
  52.                                                         printf("用户不存在!\n");
  53.                                                 }
  54.                                         }
  55.                                         while (0 != strcmp(u, usrname));
  56.                                 }
  57.                                 else //输入不一致的注册密码所执行的语句块
  58.                                 {
  59.                                 printf("两次输入的密码不一致,请再次注册!\n");
  60.                                 }
  61.                         }
  62.                         while  (0 != strcmp(password_sure, password));
  63.                 }
  64.                 else if (ny == 'N') //登录语句块
  65.                 {
  66.                         printf("请输入用户名:\n");
  67.                         do
  68.                         {
  69.                                 scanf("%s", u);
  70.                                 if (0 == strcmp(u, usrname))
  71.                                 {
  72.                                         do
  73.                                         {
  74.                                                 printf("请输入登录密码:\n");
  75.                                                 scanf("%s", p);
  76.                                                 if (0 == strcmp(p, password))
  77.                                                 {
  78.                                                         printf("登录成功\n");
  79.                                                 }
  80.                                                 else
  81.                                                 {
  82.                                                         printf("输入错误\n");
  83.                                                 }
  84.                                         }
  85.                                         while (0 != strcmp(p, password));
  86.                                 }
  87.                                 else
  88.                                 {
  89.                                         printf("用户不存在!\n");
  90.                                 }
  91.                         }
  92.                         while (0 != strcmp(u, usrname));
  93.                 }
  94.                 else
  95.                 {
  96.                         if (0 == strcmp(p, password))
  97.                         {
  98.                                 break;
  99.                         }
  100.                         printf("请输入 Y 或 N 进行选择!\n");
  101.                 }
  102.         }
  103.         while (ny == 'Y' || 'N');
  104.         printf("程序运行结束\n"); //检查是否正确结束了所有的 while 循环
  105.        
  106.         getch(); //接收到新的输入后退出程序
  107.        
  108.         return 0;
  109. }
复制代码
最佳答案
2023-1-13 22:11:01
本帖最后由 ExiaGN001 于 2023-1-14 09:58 编辑

有用请设置最佳谢谢


[已证实]问题与缓冲区有关。
改进:
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main()
  4. {
  5.         char ny;
  6.         char usrname[256];
  7.         char password[256];
  8.         char password_sure[256];
  9.         char u[256];
  10.         char p[256];

  11.         printf("欢迎使用用户注册登录系统!\n您要注册还是登录?\nY 注册 / N 登录\n");
  12.         do{
  13.                 scanf("%c", &ny);

  14.                 if (ny == 'Y') //注册语句块
  15.                 {
  16.                         do
  17.                         {
  18.                                 printf("请输入您要注册的用户名:\n");
  19.                                 scanf("%s", usrname);
  20.                                 printf("请输入您的密码:\n");
  21.                                 scanf("%s", password);
  22.                                 printf("请再次输入密码:\n");
  23.                                 scanf("%s", password_sure);
  24.                                 if (0 == strcmp(password_sure, password)) //验证注册密码语句块
  25.                                 {
  26.                                         printf("注册成功,现在前往登陆\n");
  27.                                         do //内嵌登录语句块
  28.                                         {
  29.                                                 printf("请输入用户名:\n");
  30.                                                 scanf("%s", u);
  31.                                                 if (0 == strcmp(u, usrname))
  32.                                                 {
  33.                                                         do
  34.                                                         {
  35.                                                                 printf("请输入登录密码:\n");
  36.                                                                 scanf("%s", p);
  37.                                                                 if (0 == strcmp(p, password))
  38.                                                                 {
  39.                                                                         printf("登录成功\n");
  40.                                                                 }
  41.                                                                 else
  42.                                                                 {
  43.                                                                         printf("输入错误\n");
  44.                                                                 }
  45.                                                         }
  46.                                                         while (0 != strcmp(p, password));
  47.                                                 }
  48.                                                 else
  49.                                                 {
  50.                                                         printf("用户不存在!\n");
  51.                                                 }
  52.                                         }
  53.                                         while (0 != strcmp(u, usrname));
  54.                                 }
  55.                                 else //输入不一致的注册密码所执行的语句块
  56.                                 {
  57.                                 printf("两次输入的密码不一致,请再次注册!\n");
  58.                                 }
  59.                         }
  60.                         while  (0 != strcmp(password_sure, password));
  61.                 }
  62.                 else if (ny == 'N') //登录语句块
  63.                 {
  64.                         printf("请输入用户名:\n");
  65.                         do
  66.                         {
  67.                                 scanf("%s", u);
  68.                                 if (0 == strcmp(u, usrname))
  69.                                 {
  70.                                         do
  71.                                         {
  72.                                                 printf("请输入登录密码:\n");
  73.                                                 scanf("%s", p);
  74.                                                 if (0 == strcmp(p, password))
  75.                                                 {
  76.                                                         printf("登录成功\n");
  77.                                                 }
  78.                                                 else
  79.                                                 {
  80.                                                         printf("输入错误\n");
  81.                                                 }
  82.                                         }
  83.                                         while (0 != strcmp(p, password));
  84.                                 }
  85.                                 else
  86.                                 {
  87.                                         printf("用户不存在!\n");
  88.                                 }
  89.                         }
  90.                         while (0 != strcmp(u, usrname));
  91.                 }
  92.                 else
  93.                 {
  94.                         if (0 == strcmp(p, password))
  95.                         {
  96.                                 break;
  97.                         }
  98.                         printf("请输入 Y 或 N 进行选择!\n");
  99.                         while(getchar()!='\n');                                                                        //唯一新增行,用途是将该行所有剩余数据无效化
  100.                 }
  101.         }
  102.         while (ny == 'Y' ||  ny=='N');                                                                        //有改,是逻辑错误
  103.         printf("程序运行结束\n"); //检查是否正确结束了所有的 while 循环

  104.         getchar(); //接收到新的输入后退出程序

  105.         return 0;
  106. }



复制代码


PS: 该程序的运行逻辑变更:对于执行15行时得到的无效输入,每有1行无效输入,就会导致程序输出1遍 “ 请输入 Y 或 N 进行选择!”
PS2: 请尽可能避免使用 getch(),此函数不被现行标准支持,会导致bug。
        使用getchar()而非getch(),以获取更好的代码可维护性

有用请设置最佳谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-1-13 22:11:01 | 显示全部楼层    本楼为最佳答案   
本帖最后由 ExiaGN001 于 2023-1-14 09:58 编辑

有用请设置最佳谢谢


[已证实]问题与缓冲区有关。
改进:
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main()
  4. {
  5.         char ny;
  6.         char usrname[256];
  7.         char password[256];
  8.         char password_sure[256];
  9.         char u[256];
  10.         char p[256];

  11.         printf("欢迎使用用户注册登录系统!\n您要注册还是登录?\nY 注册 / N 登录\n");
  12.         do{
  13.                 scanf("%c", &ny);

  14.                 if (ny == 'Y') //注册语句块
  15.                 {
  16.                         do
  17.                         {
  18.                                 printf("请输入您要注册的用户名:\n");
  19.                                 scanf("%s", usrname);
  20.                                 printf("请输入您的密码:\n");
  21.                                 scanf("%s", password);
  22.                                 printf("请再次输入密码:\n");
  23.                                 scanf("%s", password_sure);
  24.                                 if (0 == strcmp(password_sure, password)) //验证注册密码语句块
  25.                                 {
  26.                                         printf("注册成功,现在前往登陆\n");
  27.                                         do //内嵌登录语句块
  28.                                         {
  29.                                                 printf("请输入用户名:\n");
  30.                                                 scanf("%s", u);
  31.                                                 if (0 == strcmp(u, usrname))
  32.                                                 {
  33.                                                         do
  34.                                                         {
  35.                                                                 printf("请输入登录密码:\n");
  36.                                                                 scanf("%s", p);
  37.                                                                 if (0 == strcmp(p, password))
  38.                                                                 {
  39.                                                                         printf("登录成功\n");
  40.                                                                 }
  41.                                                                 else
  42.                                                                 {
  43.                                                                         printf("输入错误\n");
  44.                                                                 }
  45.                                                         }
  46.                                                         while (0 != strcmp(p, password));
  47.                                                 }
  48.                                                 else
  49.                                                 {
  50.                                                         printf("用户不存在!\n");
  51.                                                 }
  52.                                         }
  53.                                         while (0 != strcmp(u, usrname));
  54.                                 }
  55.                                 else //输入不一致的注册密码所执行的语句块
  56.                                 {
  57.                                 printf("两次输入的密码不一致,请再次注册!\n");
  58.                                 }
  59.                         }
  60.                         while  (0 != strcmp(password_sure, password));
  61.                 }
  62.                 else if (ny == 'N') //登录语句块
  63.                 {
  64.                         printf("请输入用户名:\n");
  65.                         do
  66.                         {
  67.                                 scanf("%s", u);
  68.                                 if (0 == strcmp(u, usrname))
  69.                                 {
  70.                                         do
  71.                                         {
  72.                                                 printf("请输入登录密码:\n");
  73.                                                 scanf("%s", p);
  74.                                                 if (0 == strcmp(p, password))
  75.                                                 {
  76.                                                         printf("登录成功\n");
  77.                                                 }
  78.                                                 else
  79.                                                 {
  80.                                                         printf("输入错误\n");
  81.                                                 }
  82.                                         }
  83.                                         while (0 != strcmp(p, password));
  84.                                 }
  85.                                 else
  86.                                 {
  87.                                         printf("用户不存在!\n");
  88.                                 }
  89.                         }
  90.                         while (0 != strcmp(u, usrname));
  91.                 }
  92.                 else
  93.                 {
  94.                         if (0 == strcmp(p, password))
  95.                         {
  96.                                 break;
  97.                         }
  98.                         printf("请输入 Y 或 N 进行选择!\n");
  99.                         while(getchar()!='\n');                                                                        //唯一新增行,用途是将该行所有剩余数据无效化
  100.                 }
  101.         }
  102.         while (ny == 'Y' ||  ny=='N');                                                                        //有改,是逻辑错误
  103.         printf("程序运行结束\n"); //检查是否正确结束了所有的 while 循环

  104.         getchar(); //接收到新的输入后退出程序

  105.         return 0;
  106. }



复制代码


PS: 该程序的运行逻辑变更:对于执行15行时得到的无效输入,每有1行无效输入,就会导致程序输出1遍 “ 请输入 Y 或 N 进行选择!”
PS2: 请尽可能避免使用 getch(),此函数不被现行标准支持,会导致bug。
        使用getchar()而非getch(),以获取更好的代码可维护性

有用请设置最佳谢谢
小甲鱼最新课程 -> https://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,将永远为真,形成死循环。。。这只是个人粗略看了一下的想法,仅供参考。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

  1. while (ny == 'Y' ||  ny=='N');
复制代码
改为
  1. while (ny != 'Y' ||  ny=='N');     
复制代码
就没问题了,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 02:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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