c++ - 在C++中将数据保存到.txt文件的问题

标签 c++

我正在尝试制作一个程序,使用户可以使用情况1输入数据并将其保存到.txt文件。但是,每当我使用案例2查看输出时,第一个和第二个数据都将从数字1开始。

该程序:

#include <iostream>
#include <conio.h>
#include <fstream>
#include <iomanip>
using namespace std;

struct bahanbakar{ 
    string nobbm;
    string nama;
    string stok, harga;
};

int menu();
void TampilData(fstream &MyFile, int size);
void IsiData (fstream &MyFile, bahanbakar ListBBM,int size);
void CheckData(fstream &MyFile);
void HitungData(fstream &MyFile, int *size);
void InputData(fstream &MyFile, int size);


main()
{   
    fstream MyFile;

    char lanjut;

    CheckData(MyFile);
    getch();
    int pilih = menu();

    while (pilih!=5)
    {
        int size = 1;
        HitungData(MyFile, &size);
        system("cls");

        switch(pilih)
        {
            case 1: InputData(MyFile,size); break;
            case 2: TampilData(MyFile, size); break;
            case 3: break;
            case 4: break;
            case 5: break;
        }
        MyFile.close();

        cout<<"Kembali ke Menu Awal?(y/n): ";
        cin>>lanjut;

        if(lanjut == 'y' || lanjut == 'Y')
        pilih = menu();

        else break;
    }
}

int menu()
{
    system("cls");
    int pilih;
    cout<<"==============================\n";
    cout<<"==============================\n";

        cout<<"1. INPUT DATA"<<endl;
        cout<<"2. TAMPILKAN DATA"<<endl;
        cout<<"3. SEARCHING DATA"<<endl;
        cout<<"4. SORTING DATA"<<endl;
        cout<<"5. KELUAR"<<endl;

    cout<<"===============================\n";
    cout<<"Masukkan pilihan[1-5]: ";
    cin>>pilih;
    cin.ignore();

    return pilih;

}

void IsiData(fstream &MyFile, bahanbakar ListBBM, int size)
{   
    getline(MyFile,ListBBM.nama);
    getline(MyFile,ListBBM.stok);
    getline(MyFile,ListBBM.harga);
    getline(MyFile,ListBBM.nobbm);

    cout<<"No.BBM   : "<<ListBBM.nobbm;cout<<endl;
    cout<<"Nama BBM: "<<ListBBM.nama;cout<<endl;
    cout<<"Stok : "<<ListBBM.stok;cout<<endl;
    cout<<"Harga    : "<<ListBBM.harga;cout<<endl<<endl;

    if (size>1) IsiData(MyFile, ListBBM, size-1);
}

void TampilData(fstream &MyFile, int size)
{
    bahanbakar ListBBM;

    MyFile.open("ListBBM.txt", ios::in | ios::out);

    cout<<"Daftar BBM: "<<endl;
    IsiData(MyFile,ListBBM,size);

}

void CheckData(fstream &MyFile)
{
    MyFile.open("ListBBM.txt",ios::out|ios::in);

        if (MyFile.is_open())
        {
            cout<<"Data Ada(Klik Enter untuk Melanjutkan)";
            MyFile.close();
        }

        else
        {
            cout<<"File Tidak Ada, File akan dibuat(Klik Enter untuk melanjutkan)";
            MyFile.close();

            MyFile.open("ListBBM.txt",ios::trunc|ios::in|ios::out);
            MyFile.close();
        }

}


void HitungData(fstream &MyFile, int *size)
{
    int temp;

    string wadah, tempat;

    MyFile.open("ListBBM.txt",ios::out|ios::in);

    do
    {
        temp = *size;
        tempat = wadah;

        for(int j=0; j<5;j++)
        {
            getline(MyFile,wadah);
        }

        if (wadah != tempat)
        {
            *size=*size+1;
        }
    }

    while (*size != temp);

    MyFile.close();

    if(*size>0)
    *size=*size-1;
}

void InputData(fstream &MyFile,int size)
{
    bahanbakar ListBBM;

    MyFile.open("ListBBM.txt",ios::app);

    cout << "===================================" << endl;
    cout << "|           TAMBAH BBM          |" << endl;
    cout << "+---------------------------------+" << endl;
    cout << "| Jumlah BBM saat ini : " << setw(3) << size;
    cout << "     |" << endl;
    cout << "===================================" << endl;
    // meminta inputan user
    cout << "-> Nama : ";
    getline(cin,ListBBM.nama);
    cout << "-> Stok : ";
    getline(cin,ListBBM.stok);
    cout << "-> Harga : ";
    getline(cin,ListBBM.harga);
    cout << "===================================" << endl;

    // memindahkan data dari struct ke file
    MyFile << ListBBM.nama << endl;
    MyFile << ListBBM.stok << endl;
    MyFile << ListBBM.harga << endl;
    MyFile << size+1 << endl;

    MyFile.close(); // tutup data
}


情况2的输出:
Daftar BBM:
No.BBM  : 1
Nama BBM: Oil
Stok    : 10
Harga   : 10000

No.BBM  : 1
Nama BBM: Gasoline
Stok    : 50
Harga   : 7000

No.BBM  : 3
Nama BBM: Water
Stok    : 100
Harga   : 5000

Kembali ke Menu Awal?(y/n):

有人可以告诉我我哪里出问题了吗?

最佳答案

好像我已经发现我的错误,那是在

    for(int j=0; j<5;j++)
    {
        getline(MyFile,wadah);
    }

我应该将j <5加上我正在使用的变量的数量,所以它应该是
    for(int j=0; j<4;j++)
    {
        getline(MyFile,wadah);
    }

关于c++ - 在C++中将数据保存到.txt文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61621704/

相关文章:

c++ - Clang 链接器说符号未定义

c++ - 循环缓冲区的线程安全实现

c++ - 具有未初始化成员的结构的 constexpr 默认构造函数仅在模板化时有效

带参数的 JavaScript 和 C++ 回调函数

c++ - 时间复杂度 - 大 O - 显示不在文件中的间隔中的数字

c++ - 在 cmake C++ 项目中使用 mongodb cxx 驱动程序

C++ 字符串和重载运算符

c++ - 选择性能最高的容器(阵列)

c++ - 如何在 Visual Studio 中使用默认的 C++ 字符串构造函数?

c++ - DirectX 9 字体库?