返回列表 回复 发帖

求助字符串定义(急)

用string只可以定义字符串常量吗?我用字符串string定义了字符串变量,总是出错,而且我已经加了string.h头文件了问什么还会出错啊。
谢谢你,我看的是C++从入门到实践这本书的第十章本章实例中的

#include<string.h>
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
using namespace std;
struct student
{
        string name;
        string course;
        int score;
};
void dispdata()
{
        ifstream file("student.dat");
        student my_student;
        cout<<"输出全部学生成绩:"<<endl;
        cout<<setw(12)<<"姓名"<<setw(8)<<"课程"<<setw(12)<<"成绩"<<endl;
        while(file.read((char*)&my_student,sizeof(student)))//非常重要
        {
                cout<<setw(12)<<my_student.name
                        <<setw(8)<<my_student.course
                        <<setw(12)<<my_student.score<<endl;
        }
        file.close();//关闭文件;

}
/*查找数据*/
void fidata()
{
        char sname[6];
        bool iffind=false;
        ifstream file("student.dat");
        student mystudent;
        file.seekg(0);
        cout<<"输入要查询的学生的姓名:"<<endl;
        cin>>sname;
        cout<<setw(12)<<"姓名"<<setw(8)<<"课程"<<setw(12)<<"分数"<<endl;
        while(file.read((char*) &mystudent,sizeof(mystudent)))//为什么读取文件函数的第一个参量一定是字符型的?
        {
                if(mystudent.name==sname)
                {
                        iffind=true;
                        cout<<setw(12)<<mystudent.name
                            <<setw(8)<<mystudent.course
                                <<setw(12)<<mystudent.score<<endl;
                }
    if(!iffind)
                cout<<"没有这个学生!";
        file.close();
}
/*添加数据*/
int addata()
{
        student mystudent;
        fstream file("student.dat",ios::out|ios::app);
        cout<<"添加数据(姓名 课程 分数):";
        cin>>mystudent.name;
        cin>>mystudent.couse;
        cin>>mystudent.score;
        file.write((char*) &mystudent,sizeof(student));
    file.close();
        return 0;
}
int  main()
{
        int select;
        cin>>select;
        do
        {
                cout<<"choose   1:输出全部学生成绩"<<endl;
                cout<<"         2:按姓名查询成绩"<<endl;
                cout<<"         3:添加新成绩"<<endl;
            switch(select)
                {
                    case 1:dispdata();break;
            case 2:fidata();break;
                        case 3:addata();break;
                    default:break;
                }
        }while(select==1||select==2||select==3);
        return 0;
}
--------------------Configuration: ten_1ex - Win32 Debug--------------------
Compiling...
ten_1ex.cpp
D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(5) : error C2871: 'std' : does not exist or is not a namespace
D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(8) : error C2146: syntax error : missing ';' before identifier 'name'
D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(8) : error C2501: 'string' : missing storage-class or type specifiers
D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(8) : error C2501: 'name' : missing storage-class or type specifiers
D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(9) : error C2146: syntax error : missing ';' before identifier 'course'
D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(9) : error C2501: 'string' : missing storage-class or type specifiers
D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(9) : error C2501: 'course' : missing storage-class or type specifiers
D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(21) : error C2039: 'name' : is not a member of 'student'
        D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(7) : see declaration of 'student'
D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(22) : error C2039: 'course' : is not a member of 'student'
        D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(7) : see declaration of 'student'
D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(40) : error C2039: 'name' : is not a member of 'student'
        D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(7) : see declaration of 'student'
D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(44) : error C2039: 'name' : is not a member of 'student'
        D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(7) : see declaration of 'student'
D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(45) : error C2039: 'course' : is not a member of 'student'
        D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(7) : see declaration of 'student'
D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(53) : error C2601: 'addata' : local function definitions are illegal
D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(65) : error C2601: 'main' : local function definitions are illegal
D:\Program Files\MSDev98\MyProjects\ten_1ex\ten_1ex.cpp(83) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

ten_1ex.obj - 15 error(s), 0 warning(s)
求指点,是在不知道那儿错了
返回列表