鱼C论坛

 找回密码
 立即注册
查看: 7857|回复: 23

这两天在看输入确认的内容,代码会长,我还是老出错,希望有人能耐心帮我看看

[复制链接]
发表于 2013-8-27 18:51:13 | 显示全部楼层 |阅读模式
5鱼币
这段代码是为了说明菜单技术,
#include<stdio.h>
char get_choice();
char get_first();
int get_int();
void count();
int main()
{
int choice;
void count();
while((choice=get_choice())!='q')
{
  switch(choice)
  {
   case 'a':printf("Buy low,sell high.\n");
   break;
   
   case 'b':putchar('\a');
   break;
   
   case 'c':count();
   break;
   
   default:printf("program error\n");
   break;
  }
  
}
printf("Bye!");
  return 0;
}
void count()
{
int n,i;
printf("Count how far?Enter an ingeter.\n");
n=get_int();
for(i=1;i<=n;i++)
printf("%d\n",i);
while(getchar()!='\n')
continue;//δ践?
}
char get_choice()
{
int ch;
printf("Enter the letter of your choice.\n");
printf("a:advice       b:bell\n");
printf("c:count        q:quit\n");
ch=get_first();
while((ch<'a'||ch>'c')&&ch!='q'){
printf("Please enter a,b,c or q.\n");
ch=get_first();
  
}
return ch;
}

char get_first()
{
int ch;
while((ch=getchar())!='\n')
continue;
return ch;
}

int get_int()
{
int input;
char ch;
while(scanf("%d",&input)!=1)
{
  while((ch=getchar())!='\n')
  putchar(ch);
  printf(" is not an ingeter.\nPlease enter an ingeter value\
  such as 25,-178 or 3.\n ");
}
return input;
}
问题在于输入a,b,c或q的时候达不到目的,像这样 未命名.jpg

最佳答案

查看完整内容

教你个小技巧哈。。。 刚开始学的时候不要写像 while((ch=getchar())!='\n') while(getchar()!='\n') while(scanf("%d",&input)!=1) 等等这样的长语句。。。 99%的时候跟你想象的不一样。 分成两个语句,三个语句。 既逻辑清楚,又便与调试。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-27 18:51:14 | 显示全部楼层
本帖最后由 liufei_vip 于 2013-8-27 20:46 编辑

教你个小技巧哈。。。
刚开始学的时候不要写像
while((ch=getchar())!='\n')
while(getchar()!='\n')
while(scanf("%d",&input)!=1)
等等这样的长语句。。。
99%的时候跟你想象的不一样。
分成两个语句,三个语句。
既逻辑清楚,又便与调试。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-27 19:17:58 | 显示全部楼层
你到底想写一个什么东西啊 注释又不写
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-8-27 19:25:00 | 显示全部楼层

#include<stdio.h>
char get_choice();//输入
char get_first();//读取第一个字符
int get_int();//输入类型确认,为整数型
void count();//count计算
int main()
{
        int choice;
        void count();
        while((choice=get_choice())!='q')
        {
                switch(choice)//标签
                {
                        case 'a':printf("Buy low,sell high.\n");
                        break;
                       
                        case 'b':putchar('\a');
                        break;
                       
                        case 'c':count();
                        break;
                       
                        default:printf("program error\n");
                        break;
                }
               
        }
        printf("Bye!");
                return 0;
}
void count()
{
        int n,i;
        printf("Count how far?Enter an ingeter.\n");
        n=get_int();
        for(i=1;i<=n;i++)
        printf("%d\n",i);
        while(getchar()!='\n')
        continue;//δ践?       
}
char get_choice()
{
        int ch;
        printf("Enter the letter of your choice.\n");
        printf("a:advice       b:bell\n");
        printf("c:count        q:quit\n");
        ch=get_first();
        while((ch<'a'||ch>'c')&&ch!='q'){
        printf("Please enter a,b,c or q.\n");
        ch=get_first();
               
        }
        return ch;       
}


char get_first()
{
        int ch;
        while((ch=getchar())!='\n')
        continue;
        return ch;
}


int get_int()
{
        int input;
        char ch;
        while(scanf("%d",&input)!=1)
        {
                while((ch=getchar())!='\n')
                putchar(ch);
                printf(" is not an ingeter.\nPlease enter an ingeter value\
                such as 25,-178 or 3.\n ");
        }
        return input;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-27 19:44:58 | 显示全部楼层
貌似好了 没怎么仔细看
  1. #include<stdio.h>
  2. char get_choice();//输入
  3. char get_first();//
  4. int get_int();//输入类型确认,为整数型
  5. void count();//count计算
  6. int main()
  7. {
  8.         int choice;
  9.         void count();
  10.         while((choice=get_choice())!='q')
  11.         {
  12.                 switch(choice)//标签
  13.                 {
  14.                 case 'a':printf("Buy low,sell high.\n");
  15.                         break;
  16.                        
  17.                 case 'b':putchar('\a');
  18.                         break;
  19.                        
  20.                 case 'c':count();
  21.                         break;
  22.                        
  23.                 default:printf("program error\n");
  24.                         break;
  25.                 }
  26.                
  27.         }
  28.         printf("Bye!");
  29.         return 0;
  30. }
  31. void count()
  32. {
  33.         int n,i;
  34.         printf("Count how far?Enter an ingeter.\n");
  35.         n=get_int();
  36.         for(i=1;i<=n;i++)
  37.         printf("%d\n",i);
  38.         while(getchar()!='\n')
  39.         continue;//δ践?        
  40. }
  41. char get_choice()
  42. {
  43.         int ch;
  44.         printf("Enter the letter of your choice.\n");
  45.         printf("a:advice       b:bell\n");
  46.         printf("c:count        q:quit\n");
  47.         ch=get_first();
  48.         while((ch<'a'||ch>'c')&&ch!='q'){
  49.         printf("Please enter a,b,c or q.\n");
  50.         ch=get_first();
  51.                
  52.         }
  53.         return ch;        
  54. }


  55. char get_first()
  56. {
  57.         int ch;
  58.         while((ch=getchar())==' ')//屏蔽空格
  59.         ;
  60.         ungetc(ch,stdin);//将第一个送回
  61.         ch = getchar();
  62.         return ch;
  63. }


  64. int get_int()
  65. {
  66.         int input;
  67.         char ch;
  68.         while(scanf("%d",&input)!=1)
  69.         {
  70.                 while((ch=getchar())!='\n')
  71.                         putchar(ch);
  72.                         printf(" is not an ingeter.\nPlease enter an ingeter value\
  73.                 such as 25,-178 or 3.\n ");
  74.         }
  75.         return input;
  76. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-27 19:46:00 | 显示全部楼层
我就是改了下 char get_first();
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-27 19:49:07 | 显示全部楼层
就喜欢这种简单又有悬赏的帖子,等我打完游戏帮你看看哈。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-8-27 20:24:04 | 显示全部楼层
牡丹花下死做鬼 发表于 2013-8-27 19:46
我就是改了下 char get_first();

我那样定义get_first是想让它只读取第一个字符,丢弃其它比如换行符,这里有问题吗??
你写的这一个语句  ungetc(ch,stdin);我还没有学到呢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-27 20:27:13 | 显示全部楼层
诸葛暗 发表于 2013-8-27 20:24
我那样定义get_first是想让它只读取第一个字符,丢弃其它比如换行符,这里有问题吗??
你写的这一个语句 ...

char get_first()
{
        int ch;
        while((ch=getchar())!='\n')//如果ch不是换行符就跳过下面的语句??你想干嘛循环又不结束会直到获取到 \n 为止 然后返回的就一定是 '\n'了啊
        continue;
        return ch;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-27 20:41:53 | 显示全部楼层

有人先回答了啊
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-27 20:55:27 | 显示全部楼层
亲,看看我修改后的代码,应该就是你想要的了
  1. //这段代码是为了说明菜单技术,
  2. #include<stdio.h>
  3. char get_choice();
  4. char get_first();
  5. int get_int();
  6. void count();
  7. int main()
  8. {
  9. int choice;
  10. void count();
  11. while((choice=get_choice())!='q')
  12. {
  13.   switch(choice)
  14.   {
  15.    case 'a':printf("Buy low,sell high.\n");
  16.    break;
  17.    
  18.    case 'b':putchar('\a');
  19.    break;
  20.    
  21.    case 'c':count();
  22.    break;
  23.    
  24.    default:printf("program error\n");
  25.    break;
  26.   }
  27.   
  28. }
  29. printf("Bye!");
  30.   return 0;
  31. }
  32. void count()
  33. {
  34. int n,i;
  35. printf("Count how far?Enter an ingeter.\n");
  36. n=get_int();
  37. for(i=1;i<=n;i++)
  38. printf("%d\n",i);
  39. while(getchar()!='\n')
  40. continue;//δ践?
  41. }
  42. char get_choice()
  43. {
  44. int ch;
  45. printf("Enter the letter of your choice.\n");
  46. printf("a:advice       b:bell\n");
  47. printf("c:count        q:quit\n");
  48. ch=get_first();
  49. while((ch<'a'||ch>'c')&&ch!='q'){
  50. printf("Please enter a,b,c or q.\n");
  51. ch=get_first();
  52.   
  53. }
  54. return ch;
  55. }

  56. char get_first()
  57. {
  58. int ch;
  59. //while((ch=getchar())!='\n')
  60. //continue;  //按照你这样返回的ch都是'\n'
  61. ch=getchar();
  62. return ch;
  63. }

  64. int get_int()
  65. {
  66. int input;
  67. char ch;
  68. while(scanf("%d",&input)!=1)
  69. {
  70.   while((ch=getchar())!='\n')
  71.   putchar(ch);
  72.   printf(" is not an ingeter.\nPlease enter an ingeter value\
  73.   such as 25,-178 or 3.\n ");
  74. }
  75. return input;
  76. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-8-27 21:42:06 | 显示全部楼层
zZ_小春 发表于 2013-8-27 20:55
亲,看看我修改后的代码,应该就是你想要的了

你修改后的代码就不能体现get_first的作用了,换行符也算到了里面 问题.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-27 22:13:14 | 显示全部楼层
诸葛暗 发表于 2013-8-27 21:42
你修改后的代码就不能体现get_first的作用了,换行符也算到了里面

getchar()能读取多个字符吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-8-27 22:25:45 | 显示全部楼层
牡丹花下死做鬼 发表于 2013-8-27 20:27
char get_first()
{
        int ch;

牡丹.jpg 这是我对你的代码的测试结果,我感觉有两个问题,一是输入count后,先是只读取了首字符,然后ount也读入了下一步..
二是输入d后,回车也产生了一个效果...
能改进吗
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-8-27 22:28:28 | 显示全部楼层
zZ_小春 发表于 2013-8-27 22:13
getchar()能读取多个字符吗?

一次一次的读啊,结果把换行符也算了进去- -
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-27 22:30:39 | 显示全部楼层
诸葛暗 发表于 2013-8-27 22:25
这是我对你的代码的测试结果,我感觉有两个问题,一是输入count后,先是只读取了首字符,然后ount也读入了 ...

O(∩_∩)O~ 我只是把你的代码改了下 O(∩_∩)O~我在看看你的代码啊
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-27 22:31:46 | 显示全部楼层
诸葛暗 发表于 2013-8-27 22:25
这是我对你的代码的测试结果,我感觉有两个问题,一是输入count后,先是只读取了首字符,然后ount也读入了 ...

在读取完之后清空缓存就可以了 O(∩_∩)O~
setbuf(stdin,NULL);
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-8-27 22:41:44 | 显示全部楼层
liufei_vip 发表于 2013-8-27 20:44
教你个小技巧哈。。。
刚开始学的时候不要写像
while((ch=getchar())!='\n')

捣鼓了半天,才发现你的话是真理~~555
可我还是不明白分开写和合起来写怎么就不一样了呢?结果都不一样了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-8-27 22:42:39 | 显示全部楼层
我可算是找到哪里错了,char get_first()
{
        int ch;
        while((ch=getchar())!='\n')
        continue;
        return ch;
}


这段,应该是char get_first()
{
        int ch;
        ch=getchar();
        while((getchar())!='\n')
        continue;
        return ch;
}

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-27 22:43:57 | 显示全部楼层
我刚想说出这里的问题
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-5 20:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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