编程

xiaohangge 2009-04-16 11:21:17
如何修改错误。谢谢
1struct month
2{
3 int number_of_day;
4 char name[4];
5};
6#include "stdafx.h"
7#include<iostream>
8#include <cstring>
9using namespace std;
10int main( )
11{
12 char *m;
13 struct month months[2]={ {31,"jan"},{28,"feb"} };
14 //months[2]={ {31,"jan"};{28,"feb"} };
15 cout<<"input month's name:";
16 cin>>m;
17 for(int i=0;i<2;i++)
18 if(strcmp(m,months[i].name)==0)
19 {
20 cout<<m<<":"<<months[i].number_of_day<<endl;
21 break;
22 };
23}
1>------ 已启动生成: 项目: jigou, 配置: Debug Win32 ------
1>正在编译...
1>c.cpp
1>c:\users\hang\documents\visual studio 2005\projects\jigou\jigou\c.cpp(13) : error C2552: “months”: 不能用初始值设定项列表初始化非聚合
1> “main::month”: 不聚合带用户定义的构造函数的类型
1>c:\users\hang\documents\visual studio 2005\projects\jigou\jigou\c.cpp(13) : error C2552: “months”: 不能用初始值设定项列表初始化非聚合
1> “main::month”: 不聚合带用户定义的构造函数的类型
1>c:\users\hang\documents\visual studio 2005\projects\jigou\jigou\c.cpp(18) : error C2036: “main::month *”: 未知的大小
1>c:\users\hang\documents\visual studio 2005\projects\jigou\jigou\c.cpp(18) : error C2027: 使用了未定义类型“main::month”
1> c:\users\hang\documents\visual studio 2005\projects\jigou\jigou\c.cpp(13) : 参见“main::month”的声明
1>c:\users\hang\documents\visual studio 2005\projects\jigou\jigou\c.cpp(18) : error C2228: “.name”的左边必须有类/结构/联合
1>c:\users\hang\documents\visual studio 2005\projects\jigou\jigou\c.cpp(20) : error C2036: “main::month *”: 未知的大小
1>c:\users\hang\documents\visual studio 2005\projects\jigou\jigou\c.cpp(20) : error C2027: 使用了未定义类型“main::month”
1> c:\users\hang\documents\visual studio 2005\projects\jigou\jigou\c.cpp(13) : 参见“main::month”的声明
1>c:\users\hang\documents\visual studio 2005\projects\jigou\jigou\c.cpp(20) : error C2228: “.number_of_day”的左边必须有类/结构/联合
1>生成日志保存在“file://c:\Users\hang\Documents\Visual Studio 2005\Projects\jigou\jigou\Debug\BuildLog.htm”
1>jigou - 8 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========
...全文
106 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

struct month
{
int number_of_day;
char name[4];
};


int main( )
{
char *m;
struct month months[2]={ {31,"jan"},{28,"feb"} };
//months[2]={ {31,"jan"};{28,"feb"} };
cout <<"input month's name:";
cin>>m;
for(int i=0;i <2;i++)
if(strcmp(m,months[i].name)==0)
{
cout <<m <<":" <<months[i].number_of_day <<endl;
break;
} //去掉这个逗号
}
hylove9494 2009-04-17
  • 打赏
  • 举报
回复
头文件
编译环境注意下
fangchao918628 2009-04-16
  • 打赏
  • 举报
回复
没有返回值!
#include "stdafx.h" 
#include <iostream>
#include <cstring>
using namespace std;

struct month
{
int number_of_day;
char name[4];
};

int main()
{
char m[50] = {0};
struct month months[2]={ {31,"jan"},{28,"feb"} };

cout<<"input month's name:"<<endl;
cin>>m;
for(int i=0;i <2;i++)
if(strcmp(m,months[i].name)==0)
{
cout<<m <<":" <<months[i].number_of_day <<endl;
break;
}
else
{
cout<<"未找到"<<endl;
break;
}
return 0;
}
zhyinty 2009-04-16
  • 打赏
  • 举报
回复
#include <iostream>
#include <cstring>

struct month
{
int number_of_day;
char name[4];
};
using namespace std;
int main( )
{
char *m;
struct month months[2]={ {31,"jan"},{28,"feb"} };
//months[2]={ {31,"jan"};{28,"feb"} };
cout <<"input month's name:";
cin>>m;
for(int i=0;i <2;i++)
if(strcmp(m,months[i].name)==0)
{
cout << m << ":" << months[i].number_of_day <<endl;
break;
};
}
mengde007 2009-04-16
  • 打赏
  • 举报
回复
没有见什么错误;去掉#include "stdafx.h"
然后char m[10];

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧