0

Вот такой вопрос, почему каждый раз ,то что я ввожу заменяется новыми введенными данными? Выводится на консоль только новая информация.

#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <windows.h>
using namespace std; 

class details
{
    public:

    details( )
    {
        string m;
        ifstream fout("detail_info.txt");
        while(getline(fout, m))
{
    cout<<m<<endl;
}
    fout.close();
    }

    details(string names,string shapes,string materials,float amounts,string colours,float weights,int quantitys)   
{
    name=names;
    shape=shapes;
    material=materials;
    amount=amounts;
    colour=colours;
    weight=weights;
    quantity=quantitys;

getting();
}

    void getting()

    {
        system("cls");
        cout<<endl<<endl<<endl<<endl;
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
        cout<<"Название детали: "<<name<<endl;
        cout<<"Форма детали: "<<shape<<endl;
        cout<<"Материал детали: "<<material<<endl;
        cout<<"Объем детали: "<<amount<<endl;
        cout<<"Цвет детали: "<<colour<<endl;
        cout<<"Вес детали: "<<weight<<endl;
        cout<<"Количество деталей: "<<quantity<<endl;
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
        cout<<endl<<endl<<endl<<endl;
    }

    void set_name(string detail_name)
    {
        name=detail_name;
    }
    string get_name()
    {
        return name;
    }

    void set_shape(string detail_shape)
    {
        shape=detail_shape;
    }

    string get_shape()
    {
        return shape;
    }

    void set_amount(float detail_amount)
    {
        amount=detail_amount;
    }

    float get_amount()
    {
        return amount;
    }

    void set_material(string detail_material)
    {
        material=detail_material;
    }

    string get_material()
    {
        return material;    
    }   
    private:
        string name;
        string shape;
        float amount;
        string material;    

    protected: 

        string colour;
        float weight;
        int quantity;   
};

class details_1 : public details
{
    public:

    void set_colour(string detail_colour)
    {
        colour=detail_colour;
    }
    string get_colour()
    {
        return colour;
    }

    void set_weight(float detail_weight)
    {
        weight=detail_weight;
    }

    float get_weight()
    {
        return weight;
    }

    void set_quantity(int detail_quantity)
    {
        quantity=detail_quantity;
    }

    int get_quantity()
    {
        return quantity;
    }

};


    void creat_inf(details *detail,details_1 *detail_1)
        {
        ofstream fout;
        fout.open("detail_info.txt", ios_base::app);

        system("cls");
        std::string name;
        std::string shape;
        float amount;
        std::string material;
        std::string colour;
        int quantity;
        float weight;

        cout<<"~~~~~~~~~~~~Ввод информации~~~~~~~~~~~~"<<endl;

        cout<<"Введите название детали:  ";
        cin>>name;

        cout<<"Введите форму детали:  ";
        cin>>shape;

        cout<<"Введите материал детали:  ";
        cin>>material;

        cout<<"Введите объем детали:  ";

        while(!(cin>>amount) || cin.get() != '\n' ) 
    {
        cout<<"~~~~~~~~~~~~~"<<endl;
        cout<<"ОШИБКА"<<endl;
        cout<<"Введите число:  ";
        cout<<endl<<endl<<endl<<endl;
        cin.clear(); 
        cin.sync();  

   }
        cout<<"Введите цвет детали:  ";
        cin>>colour;

        cout<<"Введите вес детали:  ";

            while(!(cin>>weight) || cin.get() != '\n' ) 
    {
        cout<<"~~~~~~~~~~~~~"<<endl;
        cout<<"ОШИБКА"<<endl;
        cout<<"Введите число:  ";
        cout<<endl<<endl<<endl<<endl;
        cin.clear(); 
        cin.sync();  

   }

        cout<<"Введите количество деталь:  ";

            while(!(cin>>quantity) || cin.get() != '\n' ) 
    {
        cout<<"~~~~~~~~~~~~~"<<endl;
        cout<<"ОШИБКА"<<endl;
        cout<<"Введите число:  ";
        cout<<endl<<endl<<endl<<endl;
        cin.clear(); 
        cin.sync();  

   }



        detail->set_name(name);
        detail->set_shape(shape);
        detail->set_material(material);
        detail->set_amount(amount);
        detail_1->set_colour(colour);
        detail_1->set_weight(weight);
        detail_1->set_quantity(quantity);

        system("cls");
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
        cout<<"Название детали: "<<detail->get_name()<<endl;
        cout<<"Форма детали: "<<detail->get_shape()<<endl;
        cout<<"Материал детали: "<<detail->get_material()<<endl;
        cout<<"Объем детали: "<<detail->get_amount()<<endl;
        cout<<"Цвет детали: "<<detail_1->get_colour()<<endl;
        cout<<"Вес детали: "<<detail_1->get_weight()<<endl;
        cout<<"Количество деталь: "<<detail_1->get_quantity()<<endl;
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
        cout<<endl<<endl<<endl<<endl;

        fout<<"Название детали: "<<detail->get_name()<<endl;
        fout<<"Форма детали: "<<detail->get_shape()<<endl;
        fout<<"Материал детали: "<<detail->get_material()<<endl;
        fout<<"Объем детали: "<<detail->get_amount()<<endl;
        fout<<"Цвет детали: "<<detail_1->get_colour()<<endl;
        fout<<"Вес детали: "<<detail_1->get_weight()<<endl;
        fout<<"Количество деталь: "<<detail_1->get_quantity()<<endl;
        fout<<endl<<"-------------------------------------"<<endl;  
        fout.close();
        }


void Det()
{
    details det1("Пусто","Пусто","Пусто",0,"Пусто",0,0);
}


void load_details(details *detail,details_1 *detail_1)
{
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
        cout<<"Название детали: "<<detail->get_name()<<endl;
        cout<<"Форма детали: "<<detail->get_shape()<<endl;
        cout<<"Материал детали: "<<detail->get_material()<<endl;
        cout<<"Объем детали: "<<detail->get_amount()<<endl; 
        cout<<"Цвет детали: "<<detail_1->get_colour()<<endl;
        cout<<"Вес детали: "<<detail_1->get_weight()<<endl;
        cout<<"Количество деталь: "<<detail_1->get_quantity()<<endl;
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
}


void editing_detail(details *detail,details_1 *detail_1)
{
    string name;
    load_details(detail,detail_1);
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    cout<<"Введите новое название детали:";
    cin>>name;
    detail->set_name(name);
    cout<<endl<<endl<<endl;
    cout<<"Название детали: "<<detail->get_name()<<endl;
    system("cls");
}

void editing_shape(details *detail,details_1 *detail_1)
{
    string shape;
    load_details(detail,detail_1);
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    cout<<"Введите новую форму детали:";
    cin>>shape;
    detail->set_shape(shape);
    cout<<endl<<endl<<endl;
    cout<<"Форма детали: "<<detail->get_shape()<<endl;
    system("cls");
}

void editing_material(details *detail,details_1 *detail_1)
{
    string material;
    load_details(detail,detail_1);
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    cout<<"Введите новый материал детали:";
    cin>>material;
    detail->set_material(material);
    cout<<endl<<endl<<endl;
    cout<<"Материал детали: "<<detail->get_material()<<endl;
    system("cls");
}

void editing_amount(details *detail,details_1 *detail_1)
{
    float amount;
    load_details(detail,detail_1);
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    cout<<"Введите новый объем детали:";
    cin>>amount;
    detail->set_amount(amount);
    cout<<endl<<endl<<endl;
    cout<<"Объем детали: "<<detail->get_amount()<<endl;
    system("cls");
}

void editing_colour(details *detail,details_1 *detail_1)
{
    string colour;
    load_details(detail,detail_1);
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    cout<<"Введите новый цвет детали:";
    cin>>colour;
    detail_1->set_colour(colour);
    cout<<endl<<endl<<endl;
    cout<<"Цвет детали: "<<detail_1->get_colour()<<endl;
    system("cls");
}

void editing_weight(details *detail,details_1 *detail_1)
{
    float weight;
    load_details(detail,detail_1);
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    cout<<"Введите новый вес детали:";
    cin>>weight;
    detail_1->set_weight(weight);
    cout<<endl<<endl<<endl;
    cout<<"Вес детали: "<<detail_1->get_weight()<<endl;
    system("cls");
}


void editing_quantity(details *detail,details_1 *detail_1)
{
    int quantity;
    load_details(detail,detail_1);
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    cout<<"Введите новое количество деталей:";
    cin>>quantity;
    detail_1->set_quantity(quantity);
    cout<<endl<<endl<<endl;
    cout<<"Количество деталей: "<<detail_1->get_quantity()<<endl;
    system("cls");
}


void edit_menu(details *detail,details_1 *detail_1)
{

        int nums;
        system("cls");
    cout<<" Меню редактирования данных  "<<endl<<endl;  
    cout<<"1) Редактирование названия детали"<<endl;
    cout<<"2) Редактирование формы детали"<<endl;
    cout<<"3) Редактирование материала детали"<<endl;
    cout<<"4) Редактирование объема детали"<<endl;
    cout<<"5) Редактирование цвета детали"<<endl;
    cout<<"6) Редактирование веса детали"<<endl;
    cout<<"7) Редактирование количества деталь"<<endl;
    cout<<endl<<endl<<endl<<"Введите номер пукнкта меню:  ";
    cin>>nums;
    switch(nums)
    {

    case 1: editing_detail(detail,detail_1);
            break;

    case 2: editing_shape(detail,detail_1);
            break;

    case 3: editing_material(detail,detail_1);
            break;

    case 4: editing_amount(detail,detail_1);
            break;

    case 5: editing_colour(detail,detail_1);
            break;

    case 6: editing_weight(detail,detail_1);
            break;  

    case 7: editing_quantity(detail,detail_1);
            break;  


    }

}

void remove_infile()
{
        ofstream fout;
        fout.open("detail_info.txt", ios_base::trunc);  
        fout.close();
        system("cls");
}

    int main()
    {
        details *detail = new details;
        details_1 *detail_1 = new details_1;
        int num;

        SetConsoleCP(1251);
        SetConsoleOutputCP(1251); 
            system("cls");
    do{
        cout<<"     Меню        "<<endl;
        cout<<"1) Ввод информации о детали" <<endl;
        cout<<"2) Загрузка информации о деталях из файла"<<endl;
        cout<<"3) Вывод информации на экран"<<endl;
        cout<<"4) Очистка консоли"<<endl;
        cout<<"5) Редактирование информации о детали"<<endl;
        cout<<"6) Загрузка конструктора"<<endl;
        cout<<"7) Удаление содержимого в файле"<<endl;
        cout<<"8) Выход"<<endl;
        cout<<endl<<endl<<endl<<"Введите номер пукнкта меню:  ";
        cin>>num;
        switch(num)
        {
            case 1: creat_inf(detail,detail_1);
            break;

            case 2: details();
            break;

            case 3: load_details(detail,detail_1);
            break;

            case 4: system("cls");
            break;

            case 5: edit_menu(detail,detail_1); 
            break;  

            case 6: Det();
            break;

            case 7: remove_infile();
            break;

            case 8: exit(0);
        }
    }
        while(num>0);
        system("pause");
        return 0;
    }
4
  • потому что наверно у Вас только один объект деталь. И его постоянно редактируете. Может быть нужен массив (вектор) деталей? В целом, не совсем понятно, как именно вводите и что ожидаете получить.
    – KoVadim
    16 мая 2018 в 17:02
  • Хорошо,допустим через вектор,как мне выгрузить с вектора данные с такими строками как "Название детали:" и т.д .
    – Kruk
    16 мая 2018 в 18:52
  • с массивами работали? вектор - это тот же массив. Но я не до конца понял все задание, поэтому не берусь подсказывать код.
    – KoVadim
    16 мая 2018 в 21:00
  • буду разбираться , если что напишу
    – Kruk
    16 мая 2018 в 22:43

1 ответ 1

0

Это должно помочь:

ofstream::app
0

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими пользовательским соглашением, политикой конфиденциальности и политикой о куки

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.