| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 431 人关注过本帖
标题:保存数据问题
只看楼主 加入收藏
zcdjt
Rank: 3Rank: 3
等 级:论坛游侠
威 望:4
帖 子:99
专家分:181
注 册:2014-9-9
结帖率:85.71%
收藏
已结贴  问题点数:20 回复次数:2 
保存数据问题
//这是我写的一个小项目其中的一部分,想把学生信息保存在文本文件中,可出现了分割错误,求各位大神指点迷津。
#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;
}
搜索更多相关主题的帖子: 文本文件 English include double 小项目 
2015-06-06 10:34
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:10 
不知你到底是哪一步出现了分割错误。换成用'\n'分割看行不行。我没试过,自己试试吧
2015-06-06 12:05
诸葛欧阳
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:流年
等 级:贵宾
威 望:82
帖 子:2790
专家分:14619
注 册:2014-10-16
收藏
得分:10 
save函数里的temp指针没有初始化指向一个未知内存

一片落叶掉进了回忆的流年。
2015-06-06 13:44
快速回复:保存数据问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015749 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved