鱼C论坛

 找回密码
 立即注册
查看: 1719|回复: 2

[技术交流] 小甲鱼C++视频,读写文件的课后作业

[复制链接]
发表于 2018-8-16 11:39:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<iostream>
#include <fstream>
#include<string>
using namespace std;
struct Fishoil {   
public:
        Fishoil();
        string name;
        string number;
        char male;
        void change_name();
        void change_number();
        void change_male();
        void writein();
        void buttony_n();
        void button();
        void button1();
        void button2();
};


Fishoil::Fishoil() {
        name = "伍东升";
        number = "3140205306";
        male = 'M';
}


void Fishoil::change_name() {
        string new_name;
        cout << "姓名:";
        cin >>  new_name;
        name = new_name;
}


void Fishoil::change_number() {
        string new_number;
        cout << "编号: ";
        cin >> new_number;
        number = new_number;
}


void Fishoil::change_male() {
        Fishoil fishoil;
        char new_male;
        cout << "性别: ";
        cin >> new_male;
        if (new_male != 'F' && new_male != 'M')
        {
                cout << "please try again,F means Female,M mean Male !";
                fishoil.change_male();
        }
        else
                male = new_male;
}


void Fishoil:: writein() {   //录入数据的按钮
        ofstream myfile("fishoil.txt", ios::app);
        Fishoil fishoil;
        if (!myfile.is_open())
        {
                cout << "未成功打开文件" << endl;
        }
        myfile << fishoil.name << "  " << fishoil.number << "  " << fishoil.male<<"\n" << endl;
        myfile.close();
}


void Fishoil::buttony_n() { // 判断Y/N的按钮,
        Fishoil fishoil;
        int c1=0;
        cout << "您是否需要继续录入数据 ?[Y/N]\n";
        char yorn;
        cin >> yorn;
        if (yorn == 'Y')
        {
                fishoil.button1();
                fishoil.buttony_n(); //  递归实现再次的判断
        }
        else if (yorn == 'N')
                                                        //预留了一个返回按钮,以后可以做成N是返回上一级菜单,需要设置switch的递归,初步估计
                fishoil.button();
        else
                fishoil.buttony_n();
}


void Fishoil::button() { //进行选择1~3的按键主面,和按键的选择。
        Fishoil fishoil;
        int choice;
        cout << "1. 录入信息\n" << "2. 打印已录入结果\n" << "3. 退出\n";
        cout << "Please choice which button you want !\n";
        cin >>choice;      // 判断选择的按钮
        switch (choice)
        {
        case(1):
                fishoil.button1();        //录入信息按钮
                fishoil.button();        //进入主页面选择界面
        case(2):
                fishoil.button2();  // 读取 .txt文件
                fishoil.button();        // 进入主界面选择界面
        case(3):
                break;
        default:
                cout << "choice number 1~3,if you want to quit '3' means quit .\n";
                fishoil.button();
                break;
        }
}


void Fishoil::button1() {  //录入数据按钮
        Fishoil fishoil;
        fishoil.change_name();
        fishoil.change_number();
        fishoil.change_male();
        fishoil.writein();    //进入写入信息到 .txt 文件程序
        cout << "您此次录入的信息为:\n";
        cout << fishoil.name << "  "
                << fishoil.number << "  "
                << fishoil.male << "  " << endl;
        fishoil.buttony_n();   //进入是否需要继续输入的判断按钮
}


void Fishoil::button2() {    // 打印数据按钮
        ifstream in("fishoil.txt");
        string filename;
        string line;

        if (in) // 有该文件  
        {
                while (getline(in, line)) // line中不包括每行的换行符  
                {
                        cout << line << endl;
                }
        }
        else // 没有该文件  
        {
                cout << "no such file" << endl;
        }
}


int main() {
        Fishoil fishoil;
        fishoil.button();  //进入主页面选择按钮
        cout << "您的操作已经结束,正在退出...\n";
        system("pause");
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-16 11:54:43 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

 楼主| 发表于 2018-8-16 12:55:56 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 03:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表