C++问题:程序运行时显示不出学生的信息!!求解决!!急~~~急~~~急!!!!!~~~~~

文件类型是.txt的
程序如下:#include "stdafx.h"
#include "iostream"
#include "string"
#include "fstream"
#include "time.h"
#include "stdlib.h"
using namespace std;
class Student{
string Name;//学生姓名
string ID;//学生学号
string Number;//学生序号
public:
string GetName();
string GetNumber();
string GetID();
friend class Class;
};
string Student::GetID()
{
return ID;
}
string Student::GetName()
{
return Name;
}
string Student::GetNumber()
{
return Number;
}
class System
{
public:
void menu();
};
class Class{
Student *student;
int number;
public:
Class(string);
~Class(){delete []student;};
int GetNumber();
friend void System::menu();
};

void System::menu()
{
string temp;
cout<<"请输入学生名单文件(*.txt)的全路径(输入exit为退出本程序):";
cin>>temp;
if(temp=="exit")exit(0);
Class cls(temp);
char tmp,tp;
loop: cout<<"Enter 1 or 0<1抽取,0退出,其它无效>:";
cin>>tmp;
if(tmp=='1')
{
int NUM;//被选中学生的号码
int b;
int number=0;
char *p=new char[100];
fstream File;
File.open(temp.c_str());
while(!File.eof()){File.getline(p,100);number++;}
b=number;
File.close();
again:srand((unsigned)time(NULL));
NUM=(rand()%(b-2))+1;
cout<<"被抽中的是:"<<endl;
cout<<"学生序号:"<<cls.student[temp[NUM]].GetNumber()<<endl;
cout<<"学生学号:"<<cls.student[temp[NUM]].GetID()<<endl;
cout<<"学生姓名:"<<cls.student[temp[NUM]].GetName()<<endl;
againchoose: cout<<"Enter 1 or 0<1为继续抽取,0为退出,其它无效>:";
cin>>tp;
if(tp=='1')goto again;
else if(tp=='0')exit(0);
else goto againchoose;
}
else if(tmp=='0')exit(0);
else goto loop;
}

Class::Class(string temp)
{
begin:string tmp;
number=0;
char *p=new char[100];
while(1)
{
fstream File;
File.open(temp.c_str());
if(File.good())
{
cout<<"文件打开成功......"<<endl;
while(!File.eof()){File.getline(p,100);number++;}
cout<<number<<endl;
student=new Student[number];
File.seekg(0, ios_base::beg);
for(int i=0;i<number;i++)
{
File>>student[i].Number>>student[i].ID>>student[i].Name;
}
File.close();
break;
}
else
{
cout<<"Warnning:Cannot open this file!"<<endl;
File.close();
cout<<"请重新输入路径:"<<endl;
cin>>temp;
goto begin;
}

}
cout<<"已经初始化....."<<endl;
cout<<"该班学生人数为:"<<number<<"人"<<endl;
}
int Class::GetNumber()
{
return number;
}

int main(int argc, char* argv[])
{

System sys;
sys.menu();
return 0;
}
断点调试时Name、ID、Number的Value都显示为{0x00000000 ""}

我给你改了两个错误现在可以运行了,不知道还有没有其他逻辑错误,自己进一步修改吧。
第一个,你在排序那个程序里一个for循环不加,这个错误有些幼稚,不像能写出这种程序的人该犯的。
第二个错误,main主函数里if判断语句判等的时候用了赋值号,应该改成==
建议:你要学会调试,这种编译不过调试一下子就可以找到是排序函数的哪句话出错了。调试用F9标记,F5调试F10安步走,F11进入内层函数
#include<iostream>
#include<string>
using namespace std;
struct student //定义一个学生结构体
{
string name;
string sex;
int num;
int math;
int english;
int chinese;
int sum; //个人总分
float pingjun; //个人平均分
};
student stu[3];
int n; //全局变量
void input(student *p, int n) //输入模块
{
int i;
double SUM,PINGJUN;
p=&stu[0];
for(i=0;i<n; i++, p++)
{
cout<<"请依次输入第"<<i+1<<"个学生的姓名,性别,学号,数学分数,英语分数语文分数"<<endl;
cin>>(*p).name;
cin>>(*p).sex;
cin>>(*p).num;
cin>>(*p).math;
cin>>(*p).english;
cin>>(*p).chinese;
(*p).sum=(*p).math+(*p).english+(*p).chinese; //求个人总分
(*p).pingjun=((*p).math+(*p).english+(*p).chinese)/3; //个人平均分
SUM=SUM+(*p).sum; //班级总分
PINGJUN=SUM/3; //班级平均分

} cout<<"班级总分"<<SUM<<endl;
cout<<"班级平均分"<<PINGJUN<<endl;
}
void output(student *p, int n) //输出学生信息
{
for(int i=0;i<n;i++,p++)
{
cout<<"第"<<i+1<<"个学生的信息"<<"\n";
cout<<"姓名"<<p[i].name<<"\n";
cout<<"性别"<<p[i].sex<<"\n";
cout<<"数学分数"<<p[i].math<<"\n";
cout<<"英语分数"<<p[i].english<<"\n";
cout<<"语文分数"<<p[i].chinese<<"\n";
cout<<"个人总分"<<p[i].sum<<"\n";
cout<<"个人平均分"<<p[i].pingjun<<"\n";
}
}
void paixu(student *p, int n) //排序模块
{
int i, j, t;
string g;
for(i=0; i<n-1; i++)
{
for(j=0; j<n-i; j++)
{
if(p[j].sum<p[j+1].sum)
{
g=p[j].name; p[j].name=p[j+1].name; p[j+1].name=g;
g=p[j].sex; p[j].sex=p[j+1].sex; p[j+1].sex=g;
t=p[j].math; p[j].math=p[j+1].math; p[j+1].math=t;
t=p[j].english;p[j].english=p[j+1].english;p[j+1].english=t;
t=p[j].chinese; p[j].chinese=p[j+1].chinese; p[j+1].chinese=t;
t=p[j].sum; p[j].sum=p[j+1].sum; p[j+1].sum=t;
}
}
}
}
void chaozhao(student *p,int n) //查找模块
{
int xh;
while(n==0)
{
cout<<"没有记录,请先输入学生信息"<<endl;
break;
}
if(n!=0)
{
cout<<"输入您要查找的学生学号"<<endl;
cin>>xh; //输入学号
}
if(xh==p[n].num)
{
cout<<"姓名"<<p[n].name<<"\t";
cout<<"性别"<<p[n].sex<<"\t";
cout<<"学号"<<p[n].num<<"\t";
cout<<"数学分数"<<p[n].math<<"\t";
cout<<"英语分数"<<p[n].english<<"\t";
cout<<"语文分数"<<p[n].chinese<<"\t";
cout<<"总分"<<p[n].sum<<"\t";
}
else
cout<<"没有找到您要查找的学生"<<endl;
}
void main() //主函数
{
student *q=&stu[0];
paixu(q,3);
int i;
cout<<"*……………………管理系统……………………*"<<endl;
cout<<"请选择你需要的操作:"<<endl;
cout<<"(1)输入学生信息"<<endl;
cout<<"(2)按学号查找"<<endl;
cout<<"(3)显示所有学生信息"<<endl;
cin>>i;
if(i==1)
input(q,3);
if(i==2)
chaozhao(q,3);
if(i==3)
output(q,3);
}
另外,团IDC网上有许多产品团购,便宜有口碑
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-07-04
cout<<"文件打开成功......"<<endl;
while(!File.eof()){File.getline(p,100);number++;}
cout<<number<<endl;
后加入 File.clear();//读完把文件里的状态清理一下
还有 cls.student[temp[NUM]] 是什么意思呢,应该是cls.student[NUM]吧,你那程序不会崩掉吗;
相似回答