帮我看下这个程序 编译能过 运行时出现错误

xjtrab 2009-04-15 02:08:13

// main.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "student.h"
#include <iostream>
#include <string>
using namespace std;


int main(int argc, char* argv[])
{
int data1,data2;
string name;
student a[4];

//const int number = 4;
//student a[4];
for(int i;i<3;i++)
{
cin>>name;
cin>>data1>>data2;
a[i].set(name,data1,data2);
//student a[i] = student(name,data1,data2);
}
sort(a,3);
return 0;
}


//student.h
// student.h: interface for the student class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_STUDENT_H__E6FD29E5_07B1_4FA3_9EA0_6C6C3C50B348__INCLUDED_)
#define AFX_STUDENT_H__E6FD29E5_07B1_4FA3_9EA0_6C6C3C50B348__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <string>
using namespace std;

class student
{
private:
string name;
int english,computer,total;

public:
student(string n,int e,int c);
student();
virtual ~student();
friend void sort(student a[],int n);
void set(string n,int e,int c);

};

#endif // !defined(AFX_STUDENT_H__E6FD29E5_07B1_4FA3_9EA0_6C6C3C50B348__INCLUDED_)

// student.cpp: implementation of the student class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "student.h"
#include <string>
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

student::student()
{

}

student::~student()
{

}
student::student(string n,int e,int c)
{
name = n;
english = e;
computer =c;
total = e+c;
}

void sort(student a[],int n)
{
student temp;
for(int i=0;i<n;i++)
{
for(int j=n+1;j<n+1;j++)
{
if(a[i].total<a[j].total)
{

temp.total=a[i].total; a[i].total = a[j].total; a[j].total=temp.total;
a[i].name.swap(a[j].name);


}
}
}
}
void student::set(string n,int e,int c)
{
name = n;
english = e;
computer =c;
total = e+c;
}
...全文
190 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
FrostyF 2009-04-17
  • 打赏
  • 举报
回复
for(int j=n+1;j<n+1;j++)
这样不会执行任何操作的
xjtrab 2009-04-15
  • 打赏
  • 举报
回复
up
zgjxwl 2009-04-15
  • 打赏
  • 举报
回复
你的sort有问题。。
xjtrab 2009-04-15
  • 打赏
  • 举报
回复
初始化了 错误还是一样 我也囧了

控制台项目
main.cpp与student.h student.cpp
nuoshueihe 2009-04-15
  • 打赏
  • 举报
回复
for(int j=n+1;j<n+1;j++)
该为
for(int j=i+1;j<n+1;j++)

li_il 2009-04-15
  • 打赏
  • 举报
回复
goodname 2009-04-15
  • 打赏
  • 举报
回复
//const int number = 4;
//student a[4];
for(int i = 0;i<3;i++)
{
初始化1下
xiaocha 2009-04-15
  • 打赏
  • 举报
回复
for(int i=0;i<3;i++)
xjtrab 2009-04-15
  • 打赏
  • 举报
回复

张三
56
43
Press any key to continue

输入上面的 就立即内存不能为read 应该也不是电脑的问题 我在不同的三台电脑上面尝试过了
li_il 2009-04-15
  • 打赏
  • 举报
回复
我猜是排序的时候搞错了。
xjtrab 2009-04-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 Loaden 的回复:]
什么错误?描述一下功能好吗?要不看起来太累。
[/Quote]

内存不能为read
  • 打赏
  • 举报
回复
报什么错误?
「已注销」 2009-04-15
  • 打赏
  • 举报
回复
什么错误?描述一下功能好吗?要不看起来太累。

65,211

社区成员

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

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