| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1851 人关注过本帖
标题:请问一下,我输入可以查询第一个学号,查询不了第二个学号的信息,为什么?
只看楼主 加入收藏
tinmen
Rank: 1
来 自:广东
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-12-18
收藏
 问题点数:0 回复次数:2 
请问一下,我输入可以查询第一个学号,查询不了第二个学号的信息,为什么?
#include<iostream>            //标准输入输出
#include<fstream>            //文件输入输出
#include<string>            //字符串处理
#include<windows.h>            //使用windows api中的FileCopy函数复制文件
using namespace std;        //名字空间,可有效避免标识符重复命名冲突

//菜单类,控制菜单显示
class Menu
{
public:
    string show()
    {
        while(true)
        {
            cout<<"------------------------------\n";
            cout<<"       学生信息管理系统       \n";
            cout<<"------------------------------\n";
            cout<<"        1.录入信息            \n";
            cout<<"                              \n";
            cout<<"        2.查询信息            \n";
            cout<<"                              \n";
            cout<<"        3.浏览信息            \n";
            cout<<"                              \n";
            cout<<"        4.删除信息            \n";
            cout<<"                              \n";
            cout<<"        5.退出信息            \n";
            cout<<"                              \n";
            cout<<"------------------------------\n";
            cout<<"请输入选项:\n";
        string choice;
        cin>>choice;                    //读取命令行输入
        if (choice=="1"||choice=="2"||choice=="3"||choice=="4"||choice=="5")
            return choice;
        else
            cout<<"错误的选项,请重新输入。\n";
        }
    }
};

//学生类,负责学生信息的处理
class Student
{
public:
    string Id;
    string Name;
    string Sex;
    double Score[3];
    double Average;
    double Sum;

//保存当前学生信息
    void save(){
        //文件处理,以添加形式打开文件
        fstream fw;
        fw.open("d:\\misdata.txt",ios::app|ios::out);
        //将当前对象的属性保存到文件中,endl是换行符
        if(fw.fail())                    //(自己加的,用于测试打开操作是否成功)
        {
            cerr<<"open fail fail\n";    //(自己加的,显示打开失败)
        }
        fw<<Id<<endl;
        fw<<Name<<endl;
        fw<<Score[0]<<endl;
        fw<<Score[1]<<endl;
        fw<<Score[2]<<endl;
        fw<<Average<<endl;
        fw<<Sum<<endl;
        //关闭文件
        fw.close();
    }
//查找某一学号学生信息
    void find(string id){
        fstream ofile;
        char name[20];
        ofile.open("d:\\misdata.txt",ios::in);
        if(ofile.fail())                    //(自己加的,用于测试打开操作是否成功)
        {
            cerr<<"open fail fail\n";    //(自己加的,显示打开失败)
        }
        while(!ofile.eof())                //eof是C语言中的文件结束符
        {
            ofile>>name;
            if(name==id)
            {
                cout<<name<<endl;
                ofile>>name;
                cout<<name<<endl;
                ofile>>name;
                cout<<name<<endl;
                ofile>>name;
                cout<<name<<endl;
                ofile>>name;
                cout<<name<<endl;
                ofile>>name;
                cout<<name<<endl;
                ofile>>name;
                cout<<name<<endl;
                ofile>>name;
                cout<<name<<endl;
                break;
            }
            else
            {
                ofile>>name;
                ofile>>name;
                ofile>>name;
                ofile>>name;
                ofile>>name;
                ofile>>name;
                ofile>>name;
        if(ofile.eof())
            cout<<"没有这个人!"<<endl;}
        }
        ofile.close();
    }
//显示所有学生信息
    void list()
    {
        fstream ofile;
        char name[20];
        ofile.open("d:\\misdata.txt",ios::in);
        //ofile.eof()表示遇到文件ofile结束标记
        while(!ofile.eof())                    //如果没有叹号,结果显示不了所有信息
        {
                ofile>>name;
                cout<<name<<endl;
                ofile>>name;
                cout<<name<<endl;
                ofile>>name;
                cout<<name<<endl;
                ofile>>name;
                cout<<name<<endl;
                ofile>>name;
                cout<<name<<endl;
                ofile>>name;
                cout<<name<<endl;
                ofile>>name;
                cout<<name<<endl;
                ofile>>name;
                cout<<name<<endl;
        }
        ofile.close();
    }
    //删除某一个学号的学生信息,方法是将不要删除的文件内容复制到一个新文件中,
    //然后删除原文件,将新文件重命名为原文件名
    void del(string id)
    {
        fstream fw;
        fstream ofile;
        char name[20];
        bool flag=false;
        ofile.open("d:\\misdata.txt",ios::in);
        fw.open("d:\\temp.txt",ios::out);
        while(!ofile.eof())
        {
            ofile>>name;
            if(name==id)
            {
                flag=true;
                ofile>>name;
                ofile>>name;
                ofile>>name;
                ofile>>name;
                ofile>>name;
                ofile>>name;
                ofile>>name;
            }
            else
            {
                fw<<name<<endl;
                ofile>>name;
                fw<<name<<endl;
                ofile>>name;
                fw<<name<<endl;
                ofile>>name;
                fw<<name<<endl;
                ofile>>name;
                fw<<name<<endl;
                ofile>>name;
                fw<<name<<endl;
                ofile>>name;
                fw<<name<<endl;
                ofile>>name;
                fw<<name<<endl;
            }
        }
        if(!flag)
        {
            cout<<"没有这个人!"<<endl;
            ofile.close();
            fw.close();
        }
        else
        {
            ofile.close();
            fw.close();
            //为简单起见,采用文件复制的方式
            CopyFile("d:\\temp.txt","d:\\misdata.txt",false);
        }
    }
};

//学生信息系统类,负责与用户交互
class Mis
{
    //构造函数,用于进行初始化
public:
    //创建新学生对象
    void _new()
    {
        Student s;
        cout<<"请输入学号:";
        cin>>s.Id;
        cout<<"请输入姓名:";
        cin>>s.Name;
        cout<<"请输入性别:";
        cin>>s.Sex;
        cout<<"请输入成绩一:";
        cin>>s.Score[0];
        cout<<"请输入成绩二:";
        cin>>s.Score[1];
        cout<<"请输入成绩三:";
        cin>>s.Score[2];
        s.Sum=s.Score[0]+s.Score[1]+s.Score[2];
        s.Average=s.Sum/3;
        s.save();
    }
    //查找某个学生信息
    void _find(){
        string id=" ";
        Student s;
        cout<<"请输入学生学号:";
        cin>>id;
        s.find(id);
    }
    //显示所有学生信息
    void _list()
    {
        Student s;
        s.list();
    }
    //删除某个学生信息
    void _delete(){
        string id="";
        Student s;
        cout<<"请输入学生学号:";
        cin>>id;
        s.del(id);
    }
    //运行学生管理系统。
    void run(){
        //准备菜单以及接受用户选择。
        Menu m;
        string choice=" ";

            while(true){
                choice=m.show();
                //对不同的选择进行不同的操作。
                if(choice=="1"){
                    _new();
                }else if(choice=="2"){
                    _find();
                }else if(choice=="3"){
                    _list();
                }else if(choice=="4"){
                    _delete();
                }else if(choice=="5"){
                    return;
                }
            }
            
    }
};
//主程序入口
int main()
{
    Mis m;
    m.run();

    return 0;
}
搜索更多相关主题的帖子: windows include 管理系统 public 字符串 
2015-12-18 20:18
tinmen
Rank: 1
来 自:广东
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-12-18
收藏
得分:0 
调用时候用错了吗?

我是新手
2015-12-18 21:33
q0224100404
Rank: 2
等 级:论坛游民
帖 子:25
专家分:10
注 册:2015-11-24
收藏
得分:0 
  else
            {
                ofile>>name;
                ofile>>name;
                ofile>>name;
                ofile>>name;
                ofile>>name;
                ofile>>name;
                ofile>>name;
        if(ofile.eof())
            cout<<"没有这个人!"<<endl;}
        }
这段里面多了一个ofile>>name,去掉就好了
2015-12-28 15:58
快速回复:请问一下,我输入可以查询第一个学号,查询不了第二个学号的信息,为什 ...
数据加载中...
 
   



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

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