#include <iostream>
#include <fstream>
#include <ostream>
#include <string>
using namespace std;
class cstudent
{
    private:
        int num;                        //学号
        string name;                    //姓名
        char sex;                        //性别,男为M,女为F
        int age;                        //年龄
        float math_score;                //数学成绩
        float computer_score;            //计算机成绩
        float English_score;            //英语成绩
        float average_score;            //平均成绩
        float all;                        //总成绩
    public:    
        friend void search_num();                //按学号查找
        friend void search_name();                //按姓名查找
        friend void search_all_sex();            //按成绩和性别查找
        void fun();                                //计算总成绩和平均成绩
        void input();                            //输入学生信息
        void output();                            //输出学生信息
};
int n;    cstudent *a=new cstudent[n];            //定义学生人数为n,并建立n个对象
ofstream outfile;
ifstream infile;    

void cstudent::fun()
{    
    all=math_score+computer_score+English_score;cout<<"总成绩:"<<all<<endl;
    average_score=all/3;cout<<"平均成绩:"<<average_score<<endl;
}


void cstudent::output()
{
    cout<<"学号:";            cout<<num;                cout<<endl;
    cout<<"姓名:";            cout<<name;                cout<<endl;
    cout<<"性别:";            cout<<sex;                cout<<endl;
    cout<<"年龄:";            cout<<age;                cout<<endl;
    cout<<"数学成绩:";        cout<<math_score;        cout<<endl;
    cout<<"计算机成绩:";    cout<<computer_score;    cout<<endl;
    cout<<"英语成绩:";        cout<<English_score;    cout<<endl;
    cout<<endl;
}

void cstudent::input()
{
    cout<<"学号:";            cin>>num;                cout<<endl;            
    cout<<"姓名:";            cin>>name;                cout<<endl;
    cout<<"性别:";            cin>>sex;                cout<<endl;
    cout<<"年龄:";            cin>>age;                cout<<endl;
    cout<<"数学成绩:";        cin>>math_score;        cout<<endl;
    cout<<"计算机成绩:";    cin>>computer_score;    cout<<endl;
    cout<<"英语成绩:";        cin>>English_score;        cout<<endl;
    cout<<endl;
}


void search_num()
{
    int number,j=0;
    cout<<"请输入学号:";
    cin>>number;
    while(cin.fail())
    {
        cout<<"输入错误!"<<endl<<"学号必须是整数!请检查后重试!"<<endl;
        cin>>number;
    }
    ifstream infile("D:\\学生信息.txt",ios::binary);
        if (!infile)
        {
            cerr<<"文件打开失败"<<endl;
            exit(1);
        }
        infile.seekg(0,ios::beg);
        for (int i=0;i<2;i++)
        {
            infile.read((char *)&a[i],sizeof(a[i]));
            if (number==a[i].num)
            {
                j=1;
                a[i].output();
            }
            if (j!=1)
                cout<<"对不起,没有该学号的学生!"<<endl;
        }



}

void search_name()
{
    int j=0;
    string s;
    cout<<"请输入姓名:";
    cin>>s;
    while(cin.fail())
    {
        cout<<"输入错误!"<<endl<<"姓名必须是汉字或者英文字母!请检查后重试!"<<endl;
        cin>>s;
    }
    ifstream infile("D:\\学生信息.txt",ios::binary);
    if (!infile)
    {
        cerr<<"文件打开失败"<<endl;
        exit(1);
    }
    infile.seekg(0,ios::beg);
    for (int i=0;i<2;i++)
    {
        infile.read((char *)&a[i],sizeof(a[i]));
        if (strcmp(s,a[i].name)==1)
        {
            j=1;
            a[i].output();
        }
        if (j!=1)
            cout<<"对不起,没有该姓名的学生!"<<endl;
    }
}

void search_all_sex()
{
    float number,j=0;
    string s;
    cout<<"请输入成绩:";
    cin>>number;
    while(cin.fail())
    {
        cout<<"输入错误!"<<endl<<"成绩必须是数字!请检查后重试!"<<endl;
        cin>>number;
    }
    cout<<"请输入性别:";
    cin>>s;
    while(cin.fail())
    {
        cout<<"输入错误!"<<endl<<"性别只能是M(带便男性)或者F(代表女性)!请检查后重试!"<<endl;
        cin>>s;
    }
    ifstream infile("D:\\学生信息.txt",ios::binary);
    if (!infile)
    {
        cerr<<"文件打开失败"<<endl;
        exit(1);
    }
    infile.seekg(0,ios::beg);
    for (int i=0;i<2;i++)
    {
        infile.read((char *)&a[i],sizeof(a[i]));
        if ((number==a[i].all)&&(strcmp(s,a[i].sex)==1))
        {
            j=1;
            a[i].output();
        }
        if (j!=1)
            cout<<"对不起,没有符合该条件的学生的学生!"<<endl;
    }    
}


void main()
{    
    cout<<"请输入学生人数";
    cin>>n;
    outfile.open("D:\\学生信息.txt",ios::out|ios::binary);
    if(!outfile)
    {
        cerr<<"文件打开失败!"<<endl;
        exit(1);
    }
    for (int i=0;i<n;i++)
        a[i].input();
    outfile.close();
    for (i=0;i<2;i++)
        a[i].output();
    cout<<"请输入想要查询的方式:"<<endl;
    cout<<" 按学号查询请输入1,按姓名查询请输入2,按中成绩及性别查询请输入3"<<endl;
    int m;                    //用于存放查询方式
    cin>>m;
A:
    switch(m)
    {
    case 1:
        search_num();break;
    case 2:
        search_name();break;
    case 3:
        search_all_sex();break;
    default:
        {
            cout<<"您输入的查询方式不正确,请重新输入"<<endl;
            cout<<" 按学号查询请输入1,按姓名查询请输入2,按中成绩及性别查询请输入3"<<endl;
            goto A;
        }
    }
    char s;
    cout<<"继续?(y/n)"<<endl;
    cin>>s;
    if (s=='y')
        goto A;
    else
    {    
        cout<<"系统将退出,谢谢使用!"<<endl;
        exit(0);
    }
    
    
}






--------------------Configuration: 55 - Win32 Debug--------------------
Compiling...
55.cpp
D:\Micro\C++\安装包\MSDev98\MyProjects\5\55.cpp(26) : error C2390: '$S25' : incorrect storage class 'auto'
D:\Micro\C++\安装包\MSDev98\MyProjects\5\55.cpp(116) : error C2664: 'strcmp' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
D:\Micro\C++\安装包\MSDev98\MyProjects\5\55.cpp(154) : error C2664: 'strcmp' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
执行 cl.exe 时出错.

55.obj - 1 error(s), 0 warning(s)