返回列表 回复 发帖
本帖最后由 zx4219770 于 2011-4-16 18:06 编辑

string当然可以当成变量用了,例如:
#include <iostream>
#include <string>
using namespace std;
void main()
{
        string str1;
        cin>>str1;
        cout<<str1<<endl;
        char x[10];
        cin>>x;
        str1=x;
        cout<<str1<<endl;
}

我用的是VS2010编译器,编译完全通过
把你的错误信息贴出来……
本帖最后由 zx4219770 于 2011-4-16 21:15 编辑

1.把那些预编译的头文本的.h都去掉,这个是VC++6.0编译器的写法
2.void fidata()函数的后面少了一个括号“}”
3.第57行的 cin>>mystudent.couse; 代码写错了,应该是cin>>mystudent.course;

下面是修改后的代码:
  1. #include<string>
  2. #include<iostream>
  3. #include<fstream>
  4. #include<iomanip>
  5. using namespace std;
  6. struct student
  7. {
  8.         string name;
  9.         string course;
  10.         int score;
  11. };
  12. void dispdata()
  13. {
  14.         ifstream file("student.dat");
  15.         student my_student;
  16.         cout<<"输出全部学生成绩:"<<endl;
  17.         cout<<setw(12)<<"姓名"<<setw(8)<<"课程"<<setw(12)<<"成绩"<<endl;
  18.         while(file.read((char*)&my_student,sizeof(student)))//非常重要
  19.         {
  20.                 cout<<setw(12)<<my_student.name
  21.                                         <<setw(8)<<my_student.course
  22.                                         <<setw(12)<<my_student.score<<endl;
  23.         }
  24.         file.close();//关闭文件;

  25. }
  26. void fidata()
  27. {
  28.         char sname[6];
  29.         bool iffind=false;
  30.         ifstream file("student.dat");
  31.         student mystudent;
  32.         file.seekg(0);
  33.         cout<<"输入要查询的学生的姓名:"<<endl;
  34.         cin>>sname;
  35.         cout<<setw(12)<<"姓名"<<setw(8)<<"课程"<<setw(12)<<"分数"<<endl;
  36.         while(file.read((char*) &mystudent,sizeof(mystudent)))
  37.         {
  38.                 if(mystudent.name==sname)
  39.                 {
  40.                         iffind=true;
  41.                         cout<<setw(12)<<mystudent.name
  42.                             <<setw(8)<<mystudent.course
  43.                                 <<setw(12)<<mystudent.score<<endl;
  44.                 }
  45.     if(!iffind)
  46.                 cout<<"没有这个学生!";
  47.         file.close();
  48.                 }
  49. }
  50. int addata()
  51. {
  52.         student mystudent;
  53.         fstream file("student.dat",ios::out|ios::app);
  54.         cout<<"添加数据(姓名 课程 分数):";
  55.         cin>>mystudent.name;
  56.         cin>>mystudent.course;
  57.         cin>>mystudent.score;
  58.         file.write((char*) &mystudent,sizeof(student));
  59.                 file.close();
  60.         return 0;
  61. }
  62. int  main()
  63. {
  64.         int select;
  65.         cin>>select;
  66.         do
  67.         {
  68.                 cout<<"choose   1:输出全部学生成绩"<<endl;
  69.                 cout<<"         2:按姓名查询成绩"<<endl;
  70.                 cout<<"         3:添加新成绩"<<endl;
  71.             switch(select)
  72.                 {
  73.                     case 1:dispdata();break;
  74.             case 2:fidata();break;
  75.                         case 3:addata();break;
  76.                     default:break;
  77.                 }
  78.         }while(select==1||select==2||select==3);
  79.         return 0;
  80. }
复制代码
返回列表