关于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 奇怪的事就这样发生了 这样竟然能循环了
为什么????
请高手帮忙啊