0% found this document useful (0 votes)
51 views

Input Outputfile

The document contains code to manage student data stored in a CSV file. It defines a student data structure and functions to load, print, save, add, and remove student data from a vector.

Uploaded by

Anonymous uqpdrh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Input Outputfile

The document contains code to manage student data stored in a CSV file. It defines a student data structure and functions to load, print, save, add, and remove student data from a vector.

Uploaded by

Anonymous uqpdrh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream>

#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
struct mahasiswa
{
string npm;
string nama;
float ipk;
int semester;
};

void loadData( vector<mahasiswa> &mhs){


ifstream pre_fin("mahasiswa.csv");
if (!pre_fin)
{
cout << "File User Not Exist\n";
}
string pre_line;
int featt = 0;
while (getline(pre_fin, pre_line))
{
stringstream lineStream(pre_line);
string cell;
int col = 0;
mahasiswa tmp_mhs;
while( getline(lineStream,cell, ','))
{
if(col == 0)
{
tmp_mhs.npm = cell;
}
if(col == 1)
{
tmp_mhs.nama = cell;
}
if(col == 2)
{
stringstream s(cell);
s >> tmp_mhs.ipk;
}
if(col == 3)
{
stringstream s(cell);
s >> tmp_mhs.semester;
}
col++;
}
mhs.push_back(tmp_mhs);
}
}
void cetakData(vector<mahasiswa> &mhs){
for(int i=0; i<mhs.size(); i++)
{
cout<<mhs.at(i).npm<<" "<<mhs.at(i).nama<<
" "<<mhs.at(i).ipk<<" "<<mhs.at(i).semester<<endl;
}
}

void simpanData(vector<mahasiswa> &mhs){


ofstream myfile("mahasiswa.csv"); //,ios::out,ios::trunc
if(myfile.is_open())
{
for(int i=0; i<mhs.size(); i++)
{
string txt = "";
txt.append(mhs.at(i).npm);
txt.append(",");
txt.append(mhs.at(i).nama);
txt.append(",");
stringstream ss;
ss << mhs.at(i).ipk;
txt.append(ss.str());
txt.append(",");
stringstream ss2;
ss2 << mhs.at(i).semester;
txt.append(ss2.str());
txt.append("\n");
myfile << txt;
//cout<<mhs.at(i).npm<<" "<<mhs.at(i).nama<<" "<<mhs.at(i).ipk<<"
"<<mhs.at(i).semester<<endl;
}
}
myfile.close();
}
void tambah_data(vector<mahasiswa> &mhs){
mahasiswa m;
cout<<"input npm ";getline(cin,m.npm);
cout<<"input nama ";getline(cin,m.nama);
cin.clear();
cin.ignore();
cout<<"input ipk ";cin>>m.ipk;
cout<<"input semt ";cin>>m.semester;
mhs.push_back(m);
simpanData(mhs);
}
void hapus_data(vector<mahasiswa> &mhs){
string npm;
cout<<"input npm yang akan dihapus ";cin>>npm;
for(int i=0; i<mhs.size(); i++)
{
if(npm == mhs.at(i).npm){
mhs.erase(mhs.begin()+i);
break;
}
}
simpanData(mhs);
}
int main()
{
vector<mahasiswa> mhs;
loadData(mhs);
int pil = 0;
cout<<"Menu "<<endl;
cout<<"1. Lihat Data "<<endl;
cout<<"2. Tambah Data "<<endl;
cout<<"3. Hapus Data "<<endl;
cout<<"99. Exit "<<endl;
while(true){
cout<<"Pilih Menu ";cin>>pil;
if(pil == 99){
break;
}
if(pil == 1){
cetakData(mhs);
}else
if(pil == 2){
tambah_data(mhs);
}else
if(pil == 3){
hapus_data(mhs);
}

return 0;
}

/*
float nmax = 0;
for(int i=0; i<mhs.size(); i++)
{
if(i == 0){
nmax = mhs.at(0).ipk;
}else{
if(nmax <= mhs.at(i).ipk ){
nmax = mhs.at(i).ipk ;
}
}
}
cout<<"Nilai IPK tertinggi "<<nmax<<endl;
vector<mahasiswa>mhs_tinggi;
for(int i=0; i<mhs.size(); i++)
{
if(mhs.at(i).ipk == nmax){
mhs_tinggi.push_back(mhs.at(i));
}
}
for(int i=0; i<mhs_tinggi.size(); i++)
{
cout<<mhs_tinggi.at(i).npm<<" "<<mhs_tinggi.at(i).nama<<endl;
}

return 0;*/

You might also like