| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 727 人关注过本帖, 1 人收藏
标题:一个很简单的问题,但我解决不了
取消只看楼主 加入收藏
uncle_cash
Rank: 1
等 级:新手上路
帖 子:7
专家分:1
注 册:2011-6-4
结帖率:0
收藏(1)
已结贴  问题点数:2 回复次数:5 
一个很简单的问题,但我解决不了
下面程式的问题是当打入学校名字,但输出的是一个字元而不是我输入的学校名称字串

程序代码:
#include <stdio.h>
#include <stdlib.h>

struct rating{
    int rating_2011;
    int rating_2012;
};

struct school{
    char name[20];
    struct rating rating;
    int school_num;
};

int main ()
{
    char a[20];
    int b,c,d;
    printf("plz enter your school name:\n");
    scanf("%s",a);
    printf("plz enter your school rating in 2011:\n");
    scanf("%d",&b);
    printf("plz enter your school rating in 2012:\n");
    scanf("%d",&c);
    printf("plz enter your school number:\n");
    scanf("%d",&d);
    
    struct school no1={a,{b,c},d};
    struct school no2;
    
    no2=no1;
    
    printf("the name of your school is %s\n", no1.name);
    printf("the rating of %s in 2011 is %d\n",no1.name,no1.rating.rating_2011);
    printf("the rating of %s in 2012 is %d\n",no1.name,no1.rating.rating_2012);
    printf("the school number of %s is %d\n", no1.name,no1.school_num);
    
    printf("\n");
    
    printf("the name of your school is %s\n", no2.name);
    printf("the rating of %s in 2011 is %d\n",no2.name,no2.rating.rating_2011);
    printf("the rating of %s in 2012 is %d\n",no2.name,no2.rating.rating_2012);
    printf("the school number of %s is %d\n", no2.name,no2.school_num);
    
    printf("\n");
    
    system("Pause");
    return 0;
}

搜索更多相关主题的帖子: 名字 school rating 
2011-06-04 18:44
uncle_cash
Rank: 1
等 级:新手上路
帖 子:7
专家分:1
注 册:2011-6-4
收藏
得分:0 
我用的是xcode编译器
2011-06-04 19:49
uncle_cash
Rank: 1
等 级:新手上路
帖 子:7
专家分:1
注 册:2011-6-4
收藏
得分:0 
对不起,上面的程式码打错了,我打的是下面这个,a有引号
程序代码:
#include <stdio.h>
#include <stdlib.h>

struct rating{
    int rating_2011;
    int rating_2012;
};

struct school{
    char name[20];
    struct rating rating;
    int school_num;
};

int main ()
{
    char a[20];
    int b,c,d;
    printf("plz enter your school name:\n");
    scanf("%s",a);
    printf("plz enter your school rating in 2011:\n");
    scanf("%d",&b);
    printf("plz enter your school rating in 2012:\n");
    scanf("%d",&c);
    printf("plz enter your school number:\n");
    scanf("%d",&d);
    
    struct school no1={"a",{b,c},d};
    struct school no2;
    
    no2=no1;
    
    printf("the name of your school is %s\n", no1.name);
    printf("the rating of %s in 2011 is %d\n",no1.name,no1.rating.rating_2011);
    printf("the rating of %s in 2012 is %d\n",no1.name,no1.rating.rating_2012);
    printf("the school number of %s is %d\n", no1.name,no1.school_num);
    
    printf("\n");
    
    printf("the name of your school is %s\n", no2.name);
    printf("the rating of %s in 2011 is %d\n",no2.name,no2.rating.rating_2011);
    printf("the rating of %s in 2012 is %d\n",no2.name,no2.rating.rating_2012);
    printf("the school number of %s is %d\n", no2.name,no2.school_num);
    
    printf("\n");
    
    system("Pause");
    return 0;
}


执行结果
程序代码:
plz enter your school name:
Tom
plz enter your school rating in 2011:
3
plz enter your school rating in 2012:
2
plz enter your school number:
456
the name of your school is a
the rating of a in 2011 is 3
the rating of a in 2012 is 2
the school number of a is 456

the name of your school is a
the rating of a in 2011 is 3
the rating of a in 2012 is 2
the school number of a is 456
2011-06-04 21:02
uncle_cash
Rank: 1
等 级:新手上路
帖 子:7
专家分:1
注 册:2011-6-4
收藏
得分:0 
整数输出是正确,但字串输出是错误,它直接变为a
2011-06-04 21:05
uncle_cash
Rank: 1
等 级:新手上路
帖 子:7
专家分:1
注 册:2011-6-4
收藏
得分:0 
用了strcpy函数还是不行,谢谢大家的热心回应,我自己再试试
2011-06-04 22:02
uncle_cash
Rank: 1
等 级:新手上路
帖 子:7
专家分:1
注 册:2011-6-4
收藏
得分:0 
问题解决了,太感谢beyondyf
但有一点不明白,为何把字串内容指派给结构内的成员要用strcpy函数而不可以直接no1.name=a;????
2011-06-05 09:55
快速回复:一个很简单的问题,但我解决不了
数据加载中...
 
   



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

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