| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 558 人关注过本帖
标题:关于do-while的问题
只看楼主 加入收藏
刘暮哲
Rank: 2
来 自:江苏
等 级:论坛游民
帖 子:75
专家分:83
注 册:2009-9-25
结帖率:69.23%
收藏
已结贴  问题点数:10 回复次数:7 
关于do-while的问题
#include <math.h>
#include<stdio.h>
#include <string.h>
struct record
{
    char author[20];
    char title[30];
    float price;
    struct
    {
        char month[10];
        int year;
    }date;
    char publisher[20];
    int quantity;
};
int look(struct record table[],char s1[],char s2[],int m);

void main()
{
    int i,m,num;
    float money;
    char ch;
    char a[10],t[20];
    struct record book[]={{"liuzhao","C",10,"may",2009,"电子工业出版社",100},{"tangxiaoli","C++",20,"july",2008,"大展",200},{"liu","java",29.00,"match",2000,"baidu",209}};  /*定义初始数据*/
    do
    {
    printf("please input the author and title of the book:\n");
    scanf("%s",a);
    scanf("%s",t);
    m=sizeof(book)/sizeof(struct record);
    i=look(book,a,t,m);
    if(i!=-1)
    {    printf("the book is in stock!\n");
        printf("author:%s\ntitle:%s\nprice:%f\nmonth:%s\nyear:%d\npublisher:%s\nquantity:%d\n",book[i].author,book[i].title,book[i].price,book[i].date.month,book[i].date.year,book[i].publisher,book[i].quantity);
    }
    else
        printf("required copies not in stock!\n");
    printf("please input the number of the book you want:\n"); /*输入想要的数目*/
    scanf("%d",&num);
    if(num<=book[i].quantity)
    {
        money=book[i].quantity*num;
        printf("you have pay %f yuan in total!\n",money);
    }
    else
        printf("sorry !");
    printf("\n-------------------------------------------------------\n");
    printf("do you want buy some else book?  Y OR N\n");
    ch=getchar();
    putchar(ch);
    putchar('\n');
    }while(ch=='y'||ch=='Y');
   
    printf("thanks,bye!\n");


   


 
}
int look(struct record table[],char s1[],char s2[],int m)
{
    int i,j;
    for(i=0;i<m;i++)
    {
        if((strcmp(s1,table[i].author)==0)&&(strcmp(s2,table[i].title)==0))
            return i;
    }
    return -1;
}
我运行如下
 
 
图片附件: 游客没有浏览图片的权限,请 登录注册

这个为什么不运行do-while循环呢?直接到最后 printf("thanks,bye!\n");
   
我有运行如下
图片附件: 游客没有浏览图片的权限,请 登录注册

在printf("please input the number of the book you want:\n");
我输入Y   奇怪的事就这样发生了  这样竟然能循环了
为什么????
请高手帮忙啊  
搜索更多相关主题的帖子: quantity include record 出版社 money 
2009-10-29 17:02
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
do {
    ......
}while(ch=='y'||ch=='Y');   // 如果 ch 是y或Y,则循环,否则退出循环。

—>〉Sun〈<—
2009-10-29 17:16
xy4919961
Rank: 5Rank: 5
等 级:职业侠客
帖 子:199
专家分:362
注 册:2009-10-24
收藏
得分:0 
while(ch=='y'||ch=='Y');
 你打Y.结果为真 也就是1,,当然循环阿;||运算只要有一个为真结果都为真,,

QQ群:96348241
2009-10-29 18:31
Kid_X
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:216
专家分:515
注 册:2007-10-8
收藏
得分:5 
第一种情况的原因是:scanf函数不会读入回车符,也就是你输入所需书本数时输入的是4和回车符,而回车符仍在输入缓冲区,所以getchar时ch的值是'\n',后面putchar输出了一个回车符,你可以看到do you want buy some else book?  Y OR N这句话下面多空了一行。
第二种情况的原因是:由于你输入的Y,所以scanf直接返回,num值没改变。而你输入的Y仍在输入缓冲区中,所以getchar直接得到ch的值是'Y',满足循环条件。

2009-10-29 21:07
刘暮哲
Rank: 2
来 自:江苏
等 级:论坛游民
帖 子:75
专家分:83
注 册:2009-9-25
收藏
得分:0 
那请问我怎么改?用fflush函数?还是?请帮忙,谢谢!

做最好的自己
2009-11-02 17:39
刘暮哲
Rank: 2
来 自:江苏
等 级:论坛游民
帖 子:75
专家分:83
注 册:2009-9-25
收藏
得分:0 
回复 3楼 xy4919961
那请问怎么改?

做最好的自己
2009-11-02 17:39
刘暮哲
Rank: 2
来 自:江苏
等 级:论坛游民
帖 子:75
专家分:83
注 册:2009-9-25
收藏
得分:0 
回复 4楼 Kid_X
我觉得是这样的问题,那请问我应该怎么改,谢谢

做最好的自己
2009-11-02 18:11
Kid_X
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:216
专家分:515
注 册:2007-10-8
收藏
得分:5 
VC的话应该可以使用
fflush(stdin);清空输入缓冲区。其他编译器就不清楚了。
2009-11-02 23:11
快速回复:关于do-while的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020442 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved