小弟是初学者,今天写了一小段程序,但是运行时出错,望大侠们加以指点!谢了!

sakaer 2003-08-19 07:47:14
编译完全能通过,一点问题都没有,但是运行后只输入了一个对象的内容,回车换行后就出错不能再运行了,请个位帮忙看看,在线等,谢谢。
#include<iostream.h>
class Student
{
public:
Student(){StudentNum++;}
void Input();
void Show();
static void GetNum(){cout<<"there are "<<StudentNum<<"sudents"<<endl;}
private:
char *ID;
char *sex;
float math;
float english;
float chinese;
static int StudentNum;
};
void Student::Input()
{
cout<<"Pleses Enter the message:"<<endl;
cin>>*ID>>*sex>>math>>english>>chinese;
}
void Student::Show()
{
cout<<*ID<<*sex<<math<<english<<chinese<<endl;
}
int Student::StudentNum=0;
void main()
{
Student Ary[5];
for(int i=0;i<5;i++)
Ary[i].Input();
cout<<"学号 性别 数学 英语 语文:"<<endl;
for(int j=0;j<5;j++)
Ary[i].Show();
Student::GetNum();
}
...全文
73 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Smartdoggie 2003-08-19
  • 打赏
  • 举报
回复
不好意思,骗楼主分了
sakaer 2003-08-19
  • 打赏
  • 举报
回复
好了,果然是这里错了,谢谢各位的参与,揭帖。
aflyinghorse 2003-08-19
  • 打赏
  • 举报
回复
我的疏忽
析构函数改为
~Student()
{
// for (int i=0; i<StudentNum; i++)
// {
delete []ID;
delete []sex;
// }
}
sakaer 2003-08-19
  • 打赏
  • 举报
回复
aflyinghorse()老兄,我原来有一个错误没看到,就是Ary[j].Show()我给写成Ary[i].Show()了,现在改过来后,编译也对,运行结果也对,但是会弹出一个显示错误的对话框,我用的是VC++6.0,你有没有啊?
aflyinghorse 2003-08-19
  • 打赏
  • 举报
回复
再贴一次

class Student
{
public:
Student(){StudentNum++;}
~Student()
{
for (int i=0; i<StudentNum; i++)
{
delete []ID;
delete []sex;
}
}

void Input();
void Show();
static void GetNum(){cout<<"there are "<<StudentNum<<"sudents"<<endl;}
private:
char *ID;
char *sex;
float math;
float english;
float chinese;
static int StudentNum;
};
void Student::Input()
{
cout<<"Pleses Enter the message:"<<endl;
ID = new char[50];
sex = new char[10];
cin>>ID>>sex>>math>>english>>chinese;
}
void Student::Show()
{
cout<<ID<<" "<<sex<<" "<<math<<" "<<english<<" "<<chinese<<" "<<endl;
}
int Student::StudentNum=0;
int main()
{

Student Ary[2];
for(int i=0;i<2;i++)
{
Ary[i].Input();
cin.get();
}
cout<<"学号 性别 数学 英语 语文:"<<endl;
for(int j=0;j<2;j++)
Ary[j].Show();
Student::GetNum();
getchar();
}
aflyinghorse 2003-08-19
  • 打赏
  • 举报
回复
这是我得运行结果:
输入:
Pleses Enter the message:
111 man 60 70 80
Pleses Enter the message:
222 woman 70 80 90

输出:
学号 性别 数学 英语 语文:
111 man 60 70 80
222 woman 70 80 90
there are 2sudents
nriet8357 2003-08-19
  • 打赏
  • 举报
回复
越看越糊涂。
sakaer 2003-08-19
  • 打赏
  • 举报
回复
不对,还是不行,错误结果我已经贴在上面了,麻烦再帮忙想想,谢了 !
aflyinghorse 2003-08-19
  • 打赏
  • 举报
回复
还有一处
void Student::Show()
{
cout<<*ID<<*sex<<math<<english<<chinese<<endl;
}
应该改为
void Student::Show()
{
cout<<ID<<sex<<math<<english<<chinese<<endl;
}
sakaer 2003-08-19
  • 打赏
  • 举报
回复
按照上面 aflyinghorse() 仁兄的意见修改后,运行可以输入一个学生的信息,可以接受回车,但是运行结果仍然不对,把运行结果贴出来:
Pleses Enter the message:
学号 性别 数学 英语 语文:
200103100121 man 85.0 75.0 82.0
Pleses Enter the message:
学号 性别 数学 英语 语文:
Pleses Enter the message:
学号 性别 数学 英语 语文:
Pleses Enter the message:
学号 性别 数学 英语 语文:
Pleses Enter the message:
学号 性别 数学 英语 语文:
学号 性别 数学 英语 语文:
喔01.74478e-0395.90607e-039
喔01.74478e-0395.90607e-039
喔01.74478e-0395.90607e-039
喔01.74478e-0395.90607e-039
喔01.74478e-0395.90607e-039
there are 5sudents
Press any key to continue
请问那里还有错呢?
aflyinghorse 2003-08-19
  • 打赏
  • 举报
回复
class Student
{
public:
Student(){StudentNum++;}
~Student() //析构函数
{
for (int i=0; i<StudentNum; i++)
{
delete []ID;
delete []sex;
}
}

void Input();
void Show();
static void GetNum(){cout<<"there are "<<StudentNum<<"sudents"<<endl;}
private:
char *ID;
char *sex;
float math;
float english;
float chinese;
static int StudentNum;
};
void Student::Input()
{
cout<<"Pleses Enter the message:"<<endl;
ID = new char[50]; //分配内存
sex = new char[10]; //分配内存
cin>>ID>>sex>>math>>english>>chinese;
}
void Student::Show()
{
cout<<*ID<<*sex<<math<<english<<chinese<<endl;
}
int Student::StudentNum=0;
int main()
{
Student Ary[5];
for(int i=0;i<5;i++)
{
Ary[i].Input();
cin.get();
}
cout<<"学号 性别 数学 英语 语文:"<<endl;
for(int j=0;j<5;j++)
Ary[j].Show();
Student::GetNum();
}
Demonx 2003-08-19
  • 打赏
  • 举报
回复
偶也没看出什么错误。
不过关于代码的前端,偶觉得使用定义名字作用域比较好。
sakaer 2003-08-19
  • 打赏
  • 举报
回复
不行啊,我按照你说的做了,把cin.get()添上后,运行还是原来那个错误啊!
Smartdoggie 2003-08-19
  • 打赏
  • 举报
回复
输入完后记得要接受回车啊
Smartdoggie 2003-08-19
  • 打赏
  • 举报
回复
for(int i=0;i<5;i++)
{
Ary[i].Input();
cin.get();
}

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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