| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 683 人关注过本帖
标题:关于一个共用体字符串输入的问题,求助
只看楼主 加入收藏
Spears
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2009-10-10
结帖率:0
收藏
 问题点数:0 回复次数:6 
关于一个共用体字符串输入的问题,求助


# include <stdio.h>

# include <conio.h>

# include <stdlib.h>

 

union computerInfo

{

    char band[20];

    float  price;

};

 

void main(void)

{

    union computerInfo comp1 = {"Asus X80"};

    //union computerInfo comp1 ;

    int type = 0;

 

    puts("Please input the type of the computers:");

    scanf("%d",&type);

 

    switch(type)

    {

        case 0:

        puts("please input the band of the computer");

        scanf("%s",&comp1.band)(1);

        //scanf("%s",comp1.band)(2);

        //gets(&comp1.band)(3);

        //gets(comp1.band)(4);
问题出现在这里: 在这里(1)和(2)两个表达式都是可以执行的,为什么呢?按道理来说,(1)是不对;(2)是正确的,但是不能输入空格隔开的单词;(3)是错的,编译的时候就会通知有错误;(4)编译可以通过,但是程序执行的时候,不需要输入,直接就略过了输入品牌的这一步为什么?   
        printf("the type of the computer is: %s\n",comp1.band);

        break;

    case 1:

        puts("please input the price of the computer");

        scanf("%f",&comp1.price);

        printf("The price of the computer is %f\n",comp1.price);

        break;

    default:

        puts("The input type is wrong!");

        break;

    }
}
问题就在上面,求大侠解答。
搜索更多相关主题的帖子: computers include please 字符串 price 
2014-01-07 14:48
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:0 
"对"和"不对"指的是“语法”还是“逻辑”?

按道理来说,(1)是不对; ------ "不对"的是逻辑,"对"的是语法。
语法正确是因为scanf不去检查参数类型,你看scanf的原型就知道了。
逻辑不正确,因为逻辑上需要穿进去一个char*,但你实际传进去的是char(*)[20]。当然因为 comp1.band 的值 和 &comp1.band 的值相同,所以实际上不会出问题,但这仍属于“未定义行为”。

(4)编译可以通过,但是程序执行的时候,不需要输入,直接就略过了输入品牌的这一步为什么? ------ 可能你之前有输入残留,这次就被输入了。
2014-01-07 16:30
新手嗯嗯
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-11-7
收藏
得分:0 
逻辑思维错了,想得太麻烦了吧
2014-01-07 16:56
Spears
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2009-10-10
收藏
得分:0 
回复 3楼 新手嗯嗯
请问怎么个逻辑思维错了呢?这段代码用scanf输入时可以解决的。但是,由于scanf输入字符串时,不能输入由空格隔开的多个单词,比如“Asus X80”。所以,我就想使用gets()函数去输入一个字符串。但是,程序运行的时候,不给我输入的机会,直接就给略过了。不知道为什么?1楼的大神说是前面的输入由残留。但是,我在程序运行中,只输入了一个"0",没有输入多余的字符,我就不知道问题出现在哪里了?大神,你看问题出在哪里呢?
2014-01-07 19:35
Spears
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2009-10-10
收藏
得分:0 
回复 2楼 rjsp
关于(1)和(2),你给出的解释我感觉很对。但是对于(4),我又重新做了实验,还是不行,前面没有进行任何的输入,我只输入了“0”,后面的字符串输入马上就略过去了,还是不知道为什么?另外,怎样给共用体中的赋予字符串值呢?
2014-01-07 19:52
Spears
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2009-10-10
收藏
得分:0 
回复 2楼 rjsp
我将程序作了修改,如下:
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>

union computerInfo
{
    char band[20];
    float  price;
};
void main(void)
{
    union computerInfo comp1;
    puts("please input the band of the computer");
        gets(comp1.band);
        printf("the type of the computer is: %s\n",comp1.band);
        getch();
}
这样gets(comp1.band)这个函数可以实现我预先的想法——也就是直接对共用体中输入一个带有空格隔开的多个单词,例如“dell vistro 1310”。但是,如果加上关于type的输入,就会存在问题,问题代码如下
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>

union computerInfo
{
    char band[20];
    float  price;
};

void main(void)
{
    union computerInfo comp1;
    int type = 0;
    printf("Please input the type of the computers:\n");
    scanf("%d",&type);

    switch(type)
    {
    case 0:
        printf("please input the band of the computer\n");
        gets(comp1.band);
        printf("the type of the computer is: %s\n",comp1.band);
        break;
    case 1:
        puts("please input the price of the computer");
        scanf("%f",&comp1.price);
        printf("The price of the computer is %f\n",comp1.price);
        break;
    default:
        puts("The input type is wrong!");
        break;
    }
}
程序运行,出现:please input the band of the computer
我输入0
结果程序输出:please input the band of the computer
              the type of the computer is:
                Press any key to continue
还是没有找到,到底是哪个输入将gets()函数给做掉了?
2014-01-07 20:10
Spears
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2009-10-10
收藏
得分:0 
回复 2楼 rjsp
我找到问题了,原来scanf()用在gets()之前,要在两者之间加一个getchar()来消掉输入缓冲区中的依然存在的回车。非常感谢你!
2014-01-07 20:17
快速回复:关于一个共用体字符串输入的问题,求助
数据加载中...
 
   



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

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