| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 447 人关注过本帖
标题:提示不明白?
只看楼主 加入收藏
dreamhouse
Rank: 1
等 级:新手上路
帖 子:107
专家分:7
注 册:2011-12-16
结帖率:59.46%
收藏
已结贴  问题点数:2 回复次数:7 
提示不明白?
#include<iostream>
 int main()
 {
   
     int  month=4;
     char season;
     if (month==12||month==1||month==2)
     {season='Winter';}
     else
     if (month==3||month==4||month==5)
     {season='Spring';}  
     else
     if (month==6||month==7||month==8)
     {season='Summer';}   
     else
     if (month==9||month==10||month==11)
     {season='Autumn';}
     else printf("Bogus Month");
     printf("April is in the %c\n",season);
     return 0;
 
}
too many characters in constant
 

搜索更多相关主题的帖子: 提示 include Summer return 
2012-12-21 00:07
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6814
专家分:42393
注 册:2010-12-16
收藏
得分:1 
int main()
 {
     int  month=4;
      // char season;
     char *pSeason = NULL;
      if (month==12||month==1||month==2) {
          pSeason = "Winter";
      }
      else if (month==3||month==4||month==5) {
          pSeason = "Spring";
      }  
      else   if (month==6||month==7||month==8) {
          pSeason = "Summer";
      }   
      else if (month==9||month==10||month==11) {
          pSeason = "Autumn";
      }
      else {
          printf("Bogus Month");

          return 0;
      }
      printf("April is in the %s\n",pSeason);
      return 0;

 }

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2012-12-21 00:36
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6814
专家分:42393
注 册:2010-12-16
收藏
得分:0 
season='Winter' season 是一个字符,空间不够,而且字符串也不能这样赋值
还有用swtch case 可能会好点

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2012-12-21 00:38
SLmask
Rank: 2
等 级:论坛游民
帖 子:14
专家分:27
注 册:2012-10-18
收藏
得分:1 
char season这里错了,应该声明一个字符数组,而不是一个字符变量,与此同时printf里面不是%c,而是%s
另外像楼上说的,字符串赋值应该用双引号而不是单引号,而且只有在初始化数组的时候能用season[] = "xxxx";这样的形式,所以下面最好弄个字符指针,正确的程序可以是下边的:
#include<stdio.h>
int main()
{

     int  month=4;
     char s[20];
     char *season = s;
     if (month==12||month==1||month==2)
     {season="Winter";}
     else
     if (month==3||month==4||month==5)
     {season="Spring";}
     else
     if (month==6||month==7||month==8)
     {season="Summer";}
     else
     if (month==9||month==10||month==11)
     {season="Autumn";}
     else printf("Bogus Month");
     printf("April is in the %s\n",season);
     return 0;

}

确实,用switch更清晰一些,LZ可以试一下,我没写
2012-12-21 01:57
SLmask
Rank: 2
等 级:论坛游民
帖 子:14
专家分:27
注 册:2012-10-18
收藏
得分:0 
啊,忘记说了,我测试的时候用的C语言,所以头文件变了,LZ注意一下啊
2012-12-21 01:58
dreamhouse
Rank: 1
等 级:新手上路
帖 子:107
专家分:7
注 册:2011-12-16
收藏
得分:0 
回复 4楼 SLmask
char *season=a;是指针引用数组吧?

vvvvvvvvvv
2012-12-21 14:44
SLmask
Rank: 2
等 级:论坛游民
帖 子:14
专家分:27
注 册:2012-10-18
收藏
得分:0 
回复 6楼 dreamhouse
嗯..设置一个字符指针指向字符数组的首位,这样在赋值的时候就可以用双引号的形式了,在非初始化的条件下时不能用双引号的形式给数组名赋值....
啊,抱歉,我好想弄错了,不用定义数组了,直接定义一个字符指针char *season;然后下面判断赋值的时候,season就会指向双引号内字符串的首地址
2012-12-21 23:17
dreamhouse
Rank: 1
等 级:新手上路
帖 子:107
专家分:7
注 册:2011-12-16
收藏
得分:0 
回复 7楼 SLmask
www.

vvvvvvvvvv
2012-12-22 11:06
快速回复:提示不明白?
数据加载中...
 
   



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

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