Reputation: 113
I am trying to make a program where a user can input data using case 1 and save it to .txt file. But whenever I look to the output using case 2, the first and second data will start with number 1.
The program:
#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
}
The output in case 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):
Can someone tell me where I went wrong?
Upvotes: 1
Views: 153
Reputation: 113
Look like I already found my mistake, it was in
for(int j=0; j<5;j++)
{
getline(MyFile,wadah);
}
I should put j<5 with the number of my variable I'm using so it should be
for(int j=0; j<4;j++)
{
getline(MyFile,wadah);
}
Upvotes: 1