标题:循环问题 谢谢
只看楼主
刘暮哲
Rank: 2
来 自:江苏
等 级:论坛游民
帖 子:75
专家分:83
注 册:2009-9-25
结帖率:69.23%
已结贴  问题点数:8 回复次数:4 
循环问题 谢谢
#include<stdio.h>
#include<string.h>
#define n 4
struct drug
{
    char name[20];
    float price;
    int quantity;
};
struct update  
{
    char na[20];
    float p_increase;
    int    q_increase;
};


void main()
{
    int i,j;
    float sum;
    struct drug items[4]={{"a",39,40},{"b",38,99},{"c",12,400},{"d",31,120}};
    struct update up;
    char ch;
    do
    {
     printf("please input the item you want update\n");
     scanf("%s %f %d",up.na,&up.p_increase,&up.q_increase);

     for(i=0;i<n;i++)
     {
   
        if(strcmp(up.na,items[i].name)==0)

        {
            printf("the %s is to be updated\n",up.na);
            if(up.q_increase+items[i].quantity<0)
                printf("sorry!\n");
            else
                items[i].quantity=items[i].quantity+up.q_increase;
                items[i].price=items[i].price+up.p_increase;
                sum=items[i].price*items[i].quantity;
                printf("%s %f %d %f\n",items[i].name,items[i].price,items[i].quantity,sum);
        }
     else if(strcmp(up.na,items[1].name)&&strcmp(up.na,items[2].name)&&strcmp(up.na,items[3].name)&&strcmp(up.na,items[0].name))

            printf("no the item,sorry!\n");
     }
     printf("any one else?y or no\n");
     scanf("%c",&ch);
   
    }while(ch=='y'||ch=='Y');
   
}
               


这个程序怎么不能循环啊  



我准备输入Y 再循环一次  但是直接显示press any key to continue 为什么啊??谢谢
还有我这程序红色部分 我想优化下 怎么办?

搜索更多相关主题的帖子: 400 void quantity include please 
2009-11-04 10:23
xu362726904
Rank: 6Rank: 6
等 级:侠之大者
帖 子:160
专家分:471
注 册:2009-6-18
得分:0 
你换成要求输入0和1选择
2009-11-04 10:30
玩出来的代码
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:河南新乡
等 级:贵宾
威 望:11
帖 子:742
专家分:2989
注 册:2009-10-12
得分:4 
ffush(stdin);                          //清除缓冲区
 printf("any one else?y or no\n");

     scanf("%c",&ch);
   
    }while(ch=='y'||ch=='Y');
   
}
        

离恨恰如春草,更行更远还生。
2009-11-04 12:53
刘暮哲
Rank: 2
来 自:江苏
等 级:论坛游民
帖 子:75
专家分:83
注 册:2009-9-25
得分:0 
#include<stdio.h>
#include<string.h>
#define n 4
struct drug
{
    char name[20];
    float price;
    int quantity;
};
struct update  
{
    char na[20];
    float p_increase;
    int    q_increase;
};


void main()
{
    int i,j;
    float sum;
    struct drug items[4]={{"a",39,40},{"b",38,99},{"c",12,400},{"d",31,120}};
    struct update up;
    char ch;
    do
    {
     printf("please input the item you want update\n");
     scanf("%s %f %d",up.na,&up.p_increase,&up.q_increase);

     for(i=0;i<n;i++)
     {
   
        if(strcmp(up.na,items[i].name)==0)

        {
            printf("the %s is to be updated\n",up.na);
            if(up.q_increase+items[i].quantity<0)
                printf("sorry!\n");
            else
                items[i].quantity=items[i].quantity+up.q_increase;
                items[i].price=items[i].price+up.p_increase;
                sum=items[i].price*items[i].quantity;
                printf("%s %f %d %f\n",items[i].name,items[i].price,items[i].quantity,sum);
        }
     else if(strcmp(up.na,items[1].name)&&strcmp(up.na,items[2].name)&&strcmp(up.na,items[3].name)&&strcmp(up.na,items[0].name))

            printf("no the item,sorry!\n");
     }
     fflush(stdin);
     printf("any one else?y or no\n");
     scanf("%c",&ch);
   
    }while(ch=='y'||ch=='Y');
    printf("thanks!bye!\n");
   
}
运行如下

怎么我输入没有的书目  我只想它显示一条no the item,sorry!
结果他显示四条  为什么?






做最好的自己
2009-11-04 19:20
玩出来的代码
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:河南新乡
等 级:贵宾
威 望:11
帖 子:742
专家分:2989
注 册:2009-10-12
得分:4 
你一个for循环要四次的,
  else if(strcmp(up.na,items[1].name)&&strcmp(up.na,items[2].name)&&strcmp(up.na,items[3].name)&&strcmp(up.na,items[0].name))

            printf("no the item,sorry!\n");     break;    //加个break,没找到,跳出循环。

离恨恰如春草,更行更远还生。
2009-11-04 21:08



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-290953-1-1.html




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

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