那错了?

zhshto886 2007-02-02 12:02:14
#include<iostream.h>
#include<string.h>
struct good
{
char * name[6]={"zhang","kong","ma","miao","li","shao"};
int score[6]={99,98,97,96,95,94};
};

void main()
{
int max,i,j;
good god;
max=god.score[0];
for(j=i=1;i<6;i++)
{
if(god.score[i]>max)
{
max=god.score[i];j=i;
}
}
cout<<"the best is:"<<god.name[j]<<" "<<god.score[j]<<endl;
}
...全文
390 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
czp_opensource 2007-02-02
  • 打赏
  • 举报
回复
对啊!你出了什么问题,先说出来啊!!!
a_b_c_abc10 2007-02-02
  • 打赏
  • 举报
回复
当第一个成绩最大时,j应为0,而你初始化为1
user_csc 2007-02-02
  • 打赏
  • 举报
回复
哦,终于知道了,了解!!!!!
realrukawa 2007-02-02
  • 打赏
  • 举报
回复
你这个不是初始化,是赋值运算
初始化struct good god={"zhang","kong","ma","miao","li","shao",99,98,97,96,95,94};//right
数组和结构体的初始化有拷贝各元素的功能
-------------------------------------------------------

我觉得这个答案很不错啊
realrukawa 2007-02-02
  • 打赏
  • 举报
回复
好汉,struct student st[3]={{101,"Xu",'M'},{102,"Yan",'F'},{103,"Ling",'F'}};你不还是这么写的么!
user_csc 2007-02-02
  • 打赏
  • 举报
回复
还是不懂,那应该怎么写?我觉得你说的不能这么写没有道理~~呵呵!!!!那下面这个怎么解释???下面一个是正确的!!!!



#include<stdio.h>
#include<iostream.h>
struct student{
int num;
char name[10];
char sex;
};
void main()
{
struct student st[3]={{101,"Xu",'M'},{102,"Yan",'F'},{103,"Ling",'F'}};
struct student *p;
printf("No Name Sex:\n ");
for(p=st;p<st+3;p++)
printf("%-5d %-7s %c\n",p->num,p->name,p->sex);
}
realrukawa 2007-02-02
  • 打赏
  • 举报
回复
因为赋值不能这么写呗
user_csc 2007-02-02
  • 打赏
  • 举报
回复
fosjos(无聊的菜鸟程序员) ( ) 信誉:100 Blog 2007-02-02 15:41:16 得分: 0


你这个不是初始化,是赋值运算
初始化struct good god={"zhang","kong","ma","miao","li","shao",99,98,97,96,95,94};
数组和结构体的初始化有拷贝各元素的功能


为什么那样做是错误的???还是不明白哦~~~
#include<iostream.h>
#include<string.h>
struct good
{
char *name[6];//={"zhang","kong","ma","miao","li","shao"};
int score[6];//={99,98,97,96,95,94};
};
void main()
{

int max,i,j;
struct good god;
god={"zhang","kong","ma","miao","li","shao",99,98,97,96,95,94};/////为什么??
max=god.score[0];
for(j=i=1;i<6;i++)
{
if(god.score[i]>max)
{
max=god.score[i];j=i;
}
}
cout<<"the best is:"<<god.name[j]<<" "<<god.score[j]<<endl;
}
fosjos 2007-02-02
  • 打赏
  • 举报
回复
你这个不是初始化,是赋值运算
初始化struct good god={"zhang","kong","ma","miao","li","shao",99,98,97,96,95,94};
数组和结构体的初始化有拷贝各元素的功能
user_csc 2007-02-02
  • 打赏
  • 举报
回复
//我发现一个问题,怎么这样初始化是错误的????


#include<iostream.h>
#include<string.h>
struct good
{
char *name[6];//={"zhang","kong","ma","miao","li","shao"};
int score[6];//={99,98,97,96,95,94};
};
void main()
{

int max,i,j;
struct good god;
god={"zhang","kong","ma","miao","li","shao",99,98,97,96,95,94};/////为什么??
max=god.score[0];
for(j=i=1;i<6;i++)
{
if(god.score[i]>max)
{
max=god.score[i];j=i;
}
}
cout<<"the best is:"<<god.name[j]<<" "<<god.score[j]<<endl;
}
user_csc 2007-02-02
  • 打赏
  • 举报
回复
#include<iostream.h>
#include<string.h>
struct good
{
char *name[6];//={"zhang","kong","ma","miao","li","shao"};
int score[6];//={99,98,97,96,95,94};
}god={"zhang","kong","ma","miao","li","shao",99,98,97,96,95,94};

void main()
{
int max,i,j;
max=god.score[0];
for(j=i=1;i<6;i++)
{
if(god.score[i]>max)
{
max=god.score[i];j=i;
}
}
cout<<"the best is:"<<god.name[j]<<" "<<god.score[j]<<endl;
}
realrukawa 2007-02-02
  • 打赏
  • 举报
回复
顺便提一句,从封装的概念上来讲,这个类设计的相当古怪,试想一个它的对象表示什么意思?若是多个对象呢,它们的数据成员都是六个学生和六个成绩?如果只是单纯的应用也就忍了。
yongxing14 2007-02-02
  • 打赏
  • 举报
回复
楼上的说的不错。
用CLASS定义的时候构造函数非常简单。
还要注意一点,数组的定义
char * name[6]={"zhang","kong","ma","miao","li","shao"};
int score[6]={99,98,97,96,95,94};
你是创建了7个对象而不是你写的6个,注意一下。
学习。
realrukawa 2007-02-02
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
using namespace std;

struct good
{
char* name[6];
int score[6];
}god={"zhang","kong","ma","miao","li","shao",99,98,97,96,95,94};


void main()
{
int max,i,j;
max=god.score[0];
for(j=i=0;i<6;i++)
{
if(god.score[i]>max)
{
max=god.score[i];j=i;
}
}
cout<<"the best is:"<<god.name[j]<<" "<<god.score[j]<<endl;
}
fosjos 2007-02-02
  • 打赏
  • 举报
回复
struct定义不能做初始化
既然是c++,写成class用构造函数来初始化
cutftp 2007-02-02
  • 打赏
  • 举报
回复
注意数组的下标,从0-5

64,651

社区成员

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

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