注册 登录
编程论坛 C++教室

保存数据问题

zcdjt 发布于 2015-06-06 10:34, 431 次点击
//这是我写的一个小项目其中的一部分,想把学生信息保存在文本文件中,可出现了分割错误,求各位大神指点迷津。
#include<iostream>
#include<fstream>
using namespace std;
struct student
{
       student *head,*next;
       char st_name[20];
       double English;
       double yuwen;
       double math;
       string st_id;
};
void student_save()//?
{
  ofstream out;
  out.open("student.txt",ios::out);
  if(!out)
  {
    cout<<"文件打开失败"<<endl;
    exit(1);
  }
  student *temp;
  while(temp!=NULL)
  {
    out<<temp->st_name<<"/t"
    <<temp->st_id<<"/t"
    <<temp->English<<"/t"
    <<temp->math<<"/t"
    <<temp->yuwen<<"/t"
    <<endl;
    temp=temp->next;
  }
  out.close();                             
}
void In_score()
{     
     student *temp,*head;
     char chose;
     head=new student;
     head->next=NULL;
     temp=head;
     if(temp->next!=NULL)
     {
        head->next=temp;
        head=temp;
     }   
     cout<<"请输入学生姓名:";
     cin>>temp->st_name;
     cout<<"请输入学生学号(9位):";
     w:cin>>temp->st_id;
     if(temp->st_id.length()!=9)
     {
       cin.clear();
       cin.sync();
       cout<<"你输入的位数错误,请检查后重新输入"<<endl;
       goto w;
     }        
     do
     {  
         
        cout<<"请输入学生英语,语文,数学成绩:";
        cin>>temp->English>>temp->yuwen>>temp->math;
        cout<<"是否要保存,保存输入Y/y:";
        cin>>chose;
        if(chose=='Y'||chose=='y')
        {
           student_save();
           system("cls");
        }
        cout<<"继续添加信息吗,否请按(N/n):";
        cin>>chose;         
          if((chose!='N')&&(chose!='n'))
          {
              temp->next=new student;
              temp=temp->next;
          }
          else
          {
              cout<<"欢迎使用!\n"<<endl;
              break;
          }
          system("cls");
      }while((chose!='N')&&(chose!='n'));
      system("cls");
      temp->next=NULL;
}
int main()
{
  In_score();
  system("pause");
  return 0;
}
2 回复
#2
yangfrancis2015-06-06 12:05
不知你到底是哪一步出现了分割错误。换成用'\n'分割看行不行。我没试过,自己试试吧
#3
诸葛欧阳2015-06-06 13:44
save函数里的temp指针没有初始化指向一个未知内存
1