0% found this document useful (0 votes)
63 views4 pages

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)
63 views4 pages

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

#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("[Link]");
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<[Link](); i++)
{
cout<<[Link](i).npm<<" "<<[Link](i).nama<<
" "<<[Link](i).ipk<<" "<<[Link](i).semester<<endl;
}
}

void simpanData(vector<mahasiswa> &mhs){


ofstream myfile("[Link]"); //,ios::out,ios::trunc
if(myfile.is_open())
{
for(int i=0; i<[Link](); i++)
{
string txt = "";
[Link]([Link](i).npm);
[Link](",");
[Link]([Link](i).nama);
[Link](",");
stringstream ss;
ss << [Link](i).ipk;
[Link]([Link]());
[Link](",");
stringstream ss2;
ss2 << [Link](i).semester;
[Link]([Link]());
[Link]("\n");
myfile << txt;
//cout<<[Link](i).npm<<" "<<[Link](i).nama<<" "<<[Link](i).ipk<<"
"<<[Link](i).semester<<endl;
}
}
[Link]();
}
void tambah_data(vector<mahasiswa> &mhs){
mahasiswa m;
cout<<"input npm ";getline(cin,[Link]);
cout<<"input nama ";getline(cin,[Link]);
[Link]();
[Link]();
cout<<"input ipk ";cin>>[Link];
cout<<"input semt ";cin>>[Link];
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<[Link](); i++)
{
if(npm == [Link](i).npm){
[Link]([Link]()+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<[Link](); i++)
{
if(i == 0){
nmax = [Link](0).ipk;
}else{
if(nmax <= [Link](i).ipk ){
nmax = [Link](i).ipk ;
}
}
}
cout<<"Nilai IPK tertinggi "<<nmax<<endl;
vector<mahasiswa>mhs_tinggi;
for(int i=0; i<[Link](); i++)
{
if([Link](i).ipk == nmax){
mhs_tinggi.push_back([Link](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