| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 348 人关注过本帖
标题:看下哪里出错了
只看楼主 加入收藏
langren2526
Rank: 2
等 级:论坛游民
帖 子:46
专家分:37
注 册:2010-3-12
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
看下哪里出错了
模拟售票系统(在Turbo 2.01中测试的)。 程序开始会提示输入 1 还是 2 ,1代表座位(1--5), 2代表座位(6--10)。程序红色部分会连续输出两次,不知道为什么; 还有输入1的次数等于6了,再输入2只能输入3次,按原来的想法应该还能输入4次的,调试了很久也没能让程序按想法运行, 请高手指点一下, 谢谢!!

#include<stdio.h>
#include<time.h>
#include<stdlib.h>

int roll1 (void);
int roll2 (void);

main()
{
    int x[11]={0};
    int b, c, d=0, e=0;
    char h;
   
    printf("Please type 1 for \"smoking\"\n");
    printf("Please type 2 for \"nosmoking\"\n");
    scanf("%d", &c);
   
    srand(time(NULL));
    while(d<5 || e<5)         
    {
        if(c==1)          /*c=1 在1到5中随机挑选一个数*/
        {
            d++;
            if(d<=5)
            {
                b=roll1();
                while(x[b]!=0)  /*x[b]!=0 说明票已售出*/
                    b=roll1();
                if(x[b]==0)
                {
                    ++x[b];        
                    printf("The set at \"somking\", the number is %d\n", b);
                    printf("Please type 1 for \"smoking\"\n");
                    printf("Please type 2 for \"nosmoking\"\n");
                    scanf("%d", &c);
                }
            }
            if(d>5)
            {
                e++;
                if(e<5)
                {
                    printf("Do you want to have a set for \"nosmoking\" (y or n): ");
                    scanf("%c", &h);
                    if(h=='y')
                    {
                        b=roll2();
                        while(x[b]!=0)
                            b=roll2();
                        if(x[b]==0)
                        {
                            ++x[b];
                            printf("The set at \"nosmoking\", the number is %d\n", b);
                            printf("Please type 1 for \"smoking\"\n");
                            printf("Please type 2 for \"nosmoking\"\n");
                            scanf("%d", &c);
                        }
                    }
               
                    if(h=='n')
                    {
                        printf("Next flight leaves in 3 hours.\n");
                        printf("\nPlease type 1 for \"smoking\"\n");
                        printf("Please type 2 for \"nosmoking\"\n");
                        scanf("%d", &c);
                    }
                }
            }
        }
        
        if(c==2)           /*c=2 在6到10中随机挑选一个数*/
        {
            e++;
            if(e<=5)
            {
                b=roll2();
                while(x[b]!=0)   
                    b=roll2();
                if(x[b]==0)
                {
                    ++x[b];
                    printf("The set at \"nosmoking\", the number is %d\n", b);
                    printf("Please type 1 for \"smoking\"\n");
                    printf("Please type 2 for \"nosmoking\"\n");
                    scanf("%d", &c);
                }
            }
            if(e>5)
            {
                d++;
                if(d<5)
                {
                    printf("Do you want to have a set for \"somking\" (y or n): ");
                    scanf("%c", &h);
                    if(h=='y')
                    {
                        b=roll1();
                        while(x[b]!=0)
                            b=roll1();
                        if(x[b]==0)
                        {
                            ++x[b];
                            printf("The set at \"smoking\", the number is %d\n", b);
                            printf("Please type 1 for \"smoking\"\n");
                            printf("Please type 2 for \"nosmoking\"\n");
                            scanf("%d", &c);
                        }
                    }
                    if(h=='n')
                    {
                        printf("Next flight leaves in 3 hours.\n");
                        printf("Please type 1 for \"smoking\"\n");
                        printf("Please type 2 for \"nosmoking\"\n");
                        scanf("%d", &c);
                    }
                }
            }
        }
    }
    if(d>=5 && e>=5)
        printf("Sorry, the flight is full! Next flight leaves in 3 hours.\n");
   
    system("pause");
}

int roll1 (void)
{
    int roll;
    roll=1+rand()%5;
    return roll;
}

int roll2 (void)
{
    int roll;
    roll=6+rand()%5;
    return roll;
}
搜索更多相关主题的帖子: void include 
2010-04-16 16:44
cnfarer
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:179
帖 子:3330
专家分:21157
注 册:2010-1-19
收藏
得分:10 
注意下:scanf()会接受键盘缓冲区的回车键!

★★★★★为人民服务★★★★★
2010-04-16 16:47
南国利剑
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:29
帖 子:1165
专家分:3536
注 册:2010-4-12
收藏
得分:10 
回复 楼主 langren2526
在scanf("%c",&h);前面加上一句 getchar(); 应该就可以了。你试试看。希望能帮到你 。

南国利剑
2010-04-16 18:27
langren2526
Rank: 2
等 级:论坛游民
帖 子:46
专家分:37
注 册:2010-3-12
收藏
得分:0 
以茶代酒,敬两位一杯。
2010-04-16 22:54
快速回复:看下哪里出错了
数据加载中...
 
   



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

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