How to extract the details of the highest paid employee in files? - c++

I am having problem in extracting the highest paid employee from empdetails.txt and finally displaying it to the user. i have completed getting the details from the user and merging the two files but for displaying highest paid using functions, i have no idea about it.
here is my code till now:
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
class emp
{
int num,age;
char name[20],dep[5];
public:
void getdata()
{
cout<<"\n\n Name = ";
cin>>name;
cout<<"\n Emp Num = ";
cin>>num;
cout<<"\n Department= ";
cin>>dep;
cout<<"\n Age = ";
cin>>age;
}
void display1()
{
cout<<"\n"<<name<<"\t"<<num<<"\t"<<dep<<"\t\t"<<age;
}
};
class sal
{
float gs,ns;
public:
void getsal()
{
cout<<"\n Gross sal = ";
cin>>gs;
cout<<"\n Net sal = ";
cin>>ns;
}
void display2()
{
cout<<"\t"<<gs<<"\t"<<ns;
}
};
void display()
{
emp e;sal s;
ifstream fil1;
fil1.open("empdetails.txt",ios::in);
cout<<"\n\n Name \t Emp Num \t Dep \t Age \t Gross Sal \t Net Sal \n";
while(!fil1.eof())
{
fil1.read((char*)&e,sizeof(e));
e.display1();
fil1.read((char*)&s,sizeof(s));
s.display2();
}
}
int main()
{
int n;
emp e1;sal s1;
ofstream fil1,fil2,fil3;
fil1.open("emp.txt",ios::out);
fil2.open("sal.txt",ios::out);
fil3.open("empdetails.txt",ios::out);
cout<<"\n How many employee details do you want to enter = ";
cin>>n;
cout<<"\n Enter the deatils one by one \n";
for(int i=0;i<n;i++)
{
e1.getdata();
fil1.write((char*)&e1,sizeof(e1));
s1.getsal();
fil2.write((char*)&s1,sizeof(s1));
fil3.write((char*)&e1,sizeof(e1));
fil3.write((char*)&s1,sizeof(s1));
}
fil1.close();
fil2.close();
fil3.close();
cout<<"\n\n\t\t Merged file contents \n\n\t\t";
display();
getch();
return 0;
}
how can i make a function and what conditions to use?

You don't need a function, there's already one: std::max_element. It can figure out that you're working on class emp (the first two arguments to std::max_element. It can't figure out that you want employees sorted by salary, so that is the third argument that you have to provide: a function that takes two employees and which returns true if the first employee earns less than the second. (Sounds weird, but this allows you to use the same function for std::min_element)

Related

if else problem salary formula for loop desire loop user input

asking desire number to become the for loop(how many employee if input is 4 then 4 loop if 3 3 loops), salary formula not working, if else statement for string name to not accept number and vice versa integer to not accept letters. another one of my problem is how can I name the loop for example the question is name hours and rate then the cout should do 1. name hours rate, 2.name hours rate 3.name hours rate... the code is working.. just need some imporvements.
#include <iostream>
#include <cstdlib>
using namespace std;
void displayRules()
{
cout<<"====================="<<endl;
cout<<" EMPLOYEE-SALARY "<<endl;
cout<<"====================="<<endl;
cout<<" "<<endl;
}
int main()
{
char ans;
do
{
system("cls");
displayRules();
struct Employee
{
string name;
double hours;
double rate;
double salary;
Employee *next;
Employee *prev;
};
Employee *head;
head=NULL;
Employee *newEmployee;
Employee *EmpPointer;
Employee *nextEmpPointer;
Employee *prevEmpPointer;
string inpname;
int inpN;
double inphours;
double inprate;
double salary;
salary = (inprate*inphours);
for(int ctr=0; ctr<3; ctr++)
{
cout<<endl;
cout<<"Enter Name: \t\t";
cin>> inpname;
cout<<"Enter # Hours Worked: \t";
cin>> inphours;
if (inphours<0)
{
cout << "Invalid Input! Program Stopped. ";
return 0;
}
cout<<"Enter Rate per Hour: \t";
cin>> inprate;
if (inprate<0)
{
cout << "Invalid Input! Program Stopped. ";
return 0;
}
newEmployee = new Employee;
newEmployee->name=inpname;
newEmployee->hours=inphours;
newEmployee->rate=inprate;
newEmployee->next=NULL;
if (head==NULL)
head=newEmployee;
else
{
EmpPointer=head;
while (EmpPointer->next)
EmpPointer=EmpPointer->next;
EmpPointer->next=newEmployee;
}
}
cout<<endl;
Employee *displayPointer;
displayPointer=head;
system("cls");
cout<<"------------------------------------------------------------"<<endl;
cout<<" =Summary of PAYROLL= "<<endl;
cout<<"------------------------------------------------------------"<<endl;\
cout<<"Employee Name\t"<<"# Hours Worked\t"<<"Rate/Hour\t"<<"Salary\t"<<endl;
while (displayPointer)
{
cout<<displayPointer->name<<"\t\t";
cout<<displayPointer->hours<<"\t\t";
cout<<displayPointer->rate<<"\t\t";
cout<<displayPointer->salary<<endl;
displayPointer=displayPointer->next;
}
cout<<"------------------------------------------------------------"<<endl;
cout<<endl;
cout << "Would you like to run the program again? (Y/N) ";
cin>>ans;
}
while (ans == 'y' or ans == 'Y');
return 0;
}
Note: The salary wasn't being calculated so I fix that.
I broke your code into small functions in which each function only does one thing and one thing only (Single Responsibility Principle).
Also, I introduce function templates that allows you to reuse a function when you provide the type.
Finally, the code is missing a clean up of pointers to prevent memory leaks. Each time you use the keyword new to obtain a pointer to memory, you need later to check if the pointer contains null and if doesn't then use the keyword delete to free that memory, else you end with memory leaks in your code. Therefore, I leave you with the task to write the function that should iterate your employee list and free the memory to prevent memory leaks.
I hope this is useful.
#include <iostream>
#include <cstdlib>
using namespace std;
struct Employee {
string name;
double hours;
double rate;
double salary;
Employee *next;
Employee *prev;
};
void displayRules() {
cout<<"====================="<<endl;
cout<<" EMPLOYEE-SALARY "<<endl;
cout<<"====================="<<endl;
cout<<" "<<endl;
}
// Here we create a function template to make this code more reusable
template <typename T>
T consoleInput(const std::string& prompt) {
T value;
std::cout << prompt;
std::cin >> value;
return value;
}
// Lets create our own assert to exit the app.
void assertGreaterEqualThanZero(const double value, const std::string& prompt){
if (value < 0) {
cout << prompt;
exit(1);
}
}
// Small functions that do one thing only makes coding easy to debug
Employee* createEmployee(string name, int hours, int rate) {
Employee *newEmployee = new Employee;
newEmployee->name=name;
newEmployee->hours=hours;
newEmployee->rate=rate;
newEmployee->salary = (rate * hours);
newEmployee->next=NULL;
// You need to set and maintain ->prev
// if you are thinking on using a double linked list
// else remove it from the structure since is unused.
return newEmployee;
}
// This is a helper function to add new employees to a list
Employee* addToEmployeeList(Employee* list, Employee* newEmployee){
if (list==NULL) {
list = newEmployee;
} else {
Employee *EmpPointer = list;
while (EmpPointer->next)
EmpPointer=EmpPointer->next;
EmpPointer->next=newEmployee;
}
return list;
}
// The only purpose of this function is to print the list provided
void printEmployeList(Employee* employeeList){
Employee *currentEmployee = employeeList;
system("cls");
cout<<"------------------------------------------------------------"<<endl;
cout<<" =Summary of PAYROLL= "<<endl;
cout<<"------------------------------------------------------------"<<endl;
while (currentEmployee){
cout<<"Employee Name\t"<<"# Hours Worked\t"<<"Rate/Hour\t"<<"Salary\t"<<endl;
cout<<currentEmployee->name<<"\t\t";
cout<<currentEmployee->hours<<"\t\t";
cout<<currentEmployee->rate<<"\t\t";
cout<<currentEmployee->salary<<endl;
cout<<"------------------------------------------------------------"<<endl;
currentEmployee=currentEmployee->next;
}
}
// I leave you with this piece that is missing.
// TODO: create function that delete each employee in the list,
// then deletes the list in order to prevent memory leaks
int main() {
char ans;
do {
system("cls");
displayRules();
Employee *employeeList;
employeeList=NULL;
for(int ctr=0; ctr<3; ++ctr) {
// Lets declare and instantiate when we need it.
string name = consoleInput<string>("Enter Name: \t\t");
// No need to use inp (as inphours) in front of your variables
// It makes it harder to read. Just put hours as a name.
double hours = consoleInput<double>("Enter # Hours Worked: \t");
assertGreaterEqualThanZero(hours, "Invalid Input! Program Stopped.");
double rate = consoleInput<double>("Enter Rate per Hour: \t");
assertGreaterEqualThanZero(rate, "Invalid Input! Program Stopped. ");
Employee *newEmployee = createEmployee(name, hours, rate);
employeeList = addToEmployeeList(employeeList, newEmployee);
}
cout << endl;
printEmployeList(employeeList);
cout << "Would you like to run the program again? (Y/N) ";
cin>>ans;
} while (ans == 'y' or ans == 'Y');
return 0;
}

Program compiles but I think switch is ignored.

I am currently studying c++ but I fell behind a little bit, so I apologize if my question is obvious.
I have to create a program that asks for a student's name, GPA, Year of admission, and get a random 5 digit number generated for that person. The number of students will not exceed 42.
My program compiled (somehow) and I am able to get the error for invalid menu selection, however, whenever I give a valid selection (currently 1) nothing happens.
Maybe I am missing something, this is why I need help.
Here is my code.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
//print all the menu options
void print_menu()
{
cout<<"\nRCNJ Registrar Menu:"<<"\n"
<<"\n"
<<"[1] Add a student"<<"\n"
<<"[2] Display all students"<<"\n"
<<"[3] Display by year"<<"\n"
<<"[4] Display statistics"<<"\n"
<<"[5] Quit"<<"\n";
}
//get and return the student's name
void get_name(string& student_name) //call student_name after that.
{
cout<<"Please enter the sudent's name: ";
cin >> student_name;
cout<<"\n";
}
//validate and return gpa
double get_gpa()
{
double student_gpa = 0;
cout<<"Please enter the GPA: ";
cin >>student_gpa;
cout<<"\n";
while (student_gpa > 4 || student_gpa < 0)
{
cout<<"Please enter a valid GPA for the student (0.00 - 4.00): ";
cin >> student_gpa;
cout<<"\n";
}
return student_gpa;
}
//validateand return year
int get_year()
{
int student_year = 0;
cout<<"Please enter the year: ";
cin >> student_year;
cout<<"\n";
while (student_year >2016 || student_year <1972)
{
cout<<"Please enter a valid year (min 1972, max 2016): ";
cin >> student_year;
cout<<"\n";
}
return student_year;
}
//generate the student's R#
int generate_number()
{
int r_number;
srand (time(NULL));
r_number = rand() % 89999 + 10000;
return r_number;
}
//save info. Include get_name, get_gpa, get_year
void input_new_student()
{
string student_name;
double student_gpa;
int student_year;
int r_number;
int s_name, s_gpa, s_year, r_num;
get_name(student_name);
get_gpa();
get_year();
generate_number();
}
//display all students in the proper format
void print_all()
{
}
//get a year as selection and print all students that are the same year
void print_by_year()
{
}
//display statistics based on entered students
void print_statistics()
{
}
//validate and return the menu option selected by the user.
//it should call print_menu defined earlier
int get_selection(int menu_choice)
{
menu_choice = 0;
cout<<"\n"
<<"Selection: ";
cin >> menu_choice;
cout<<"\n";
while (menu_choice > 5 || menu_choice< 1)
{
cout<<" Menu choice is invalid. Please re-enter (1 - 5): ";
cin>> menu_choice;
cout<<"\n";
}
return menu_choice;
}
int main()
{
string student_name;
double student_gpa;
int student_year;
int r_number;
int menu_choice;
int s_name=0;
int s_gpa=0;
int s_year=0;
int r_num=0;
string nameArray[42];
s_name++;
double gpaArray[42];
s_gpa++;
int yearArray[42];
s_year++;
int ramapoArray[42];
r_num++;
print_menu();
get_selection(menu_choice);
switch (menu_choice)
{
case 1:
input_new_student();
nameArray[s_name] = student_name;
gpaArray[s_gpa] = student_gpa;
yearArray[s_year] = student_year;
ramapoArray[r_num] = r_number;
break;
}
return 0;
}
I dont have permission to comment, hence adding it here.
In you main(),
get_selection(menu_choice);
switch (menu_choice)
You return menu_choice, but there is none to take the value, you end you using garbage value as it is uninitialized.
So two ways you can do it, either by passing the address/reference of menu_choice or by return value. try either of these it should work, though I have not gone through the rest of your program.
As suggested by others, try a debugger e.g. gdb?

Traversing a linked list with a structure inside

So I a linked list of 4 students and inside each node for the linked list is a structure that holds some data about the students. I want to traverse this linked list and print the data inside each of the structures. I can traverse the linked list expect all the data print is 0. Any help would be much appreciated.
#include<stdlib.h>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
using namespace std;
void displayGrades( struct Outer *O);
void calculateGrades(struct Outer *O);
void readGrades( struct Outer *O);
struct Inner{
int id;
string name;
double midterm1;
double midterm2;
double midtermTotal;
double lab_H;
double finalExam;
double total;
};
Inner i1;
struct Outer{
Inner data;
Outer *next;
};
struct Outer o1, o2, o3, o4;
int main()
{
readGrades(&o1);
calculateGrades(&o1);
displayGrades(&o1);
//o1.next = &o2;
/*
readGrades(&o2);
calculateGrades(&o2);
displayGrades(&o2);
//o2.next =&o3;
readGrades(&o3);
calculateGrades(&o3);
displayGrades(&o3);
//o3.next =&o4;
readGrades(&o4);
calculateGrades(&o4);
displayGrades(&o4);
//o4.next =NULL;
*/
Outer *ptro1;
ptro1 = new Outer;
Outer *ptro2;
ptro2 = new Outer;
Outer *ptro3;
ptro3 = new Outer;
Outer *ptro4;
ptro4 = new Outer;
Outer *head=ptro1;
ptro1->next = ptro2;
ptro2->next = ptro3;
ptro3->next = ptro4;
ptro4->next = NULL;
while(head!=NULL) // && i<=2)
{
cout<<"Student ID: "<<head->data.id<<endl;
cout<<"Student Midterm1: "<<head->data.midterm1<<endl;
cout<<"Student Midterm2: "<<head->data.midterm2<<endl;
cout<<"Student Labs and Homework: "<<head->data.lab_H<<endl;
cout<<"Student Final Exam: "<<head->data.finalExam<<endl;
head = head->next;
}
return 0;
}
void readGrades(struct Outer *O){
cout<<"Enter the student's id: "<<endl;
cin>>o1.data.id;
cout<<"Enter the student's midterm #1 grade: ";
cin>>o1.data.midterm1;
cout<<"Enter the student's midterm #2 grade: ";
cin>>o1.data.midterm2;
cout<<"Enter the student's lab and homework grade: ";
cin>>o1.data.lab_H;
cout<<"Enter the student's final exam grade: ";
cin>>o1.data.finalExam;
}
void displayGrades(struct Outer *O){
cout<<"The students final grade is: ";
if(O->data.total>=90)
{
cout<<"A"<<endl;
}
else if(O->data.total<=89 && O->data.total>=80)
{
cout<<"B"<<endl;
}
else if(O->data.total<=79 && O->data.total>=70)
{
cout<<"C"<<endl;
}
else if(O->data.total<=69 && O->data.total<60)
{
cout<<"F"<<endl;
}
}
void calculateGrades(struct Outer *O){
O->data.midtermTotal=(((O->data.midterm1/50)+(O- >data.midterm2/50))/2)*35;
O->data.lab_H=(O->data.lab_H/20)*25;
O->data.finalExam=(O->data.finalExam/100)*40;
O->data.total=O->data.midtermTotal+O->data.lab_H+O->data.finalExam;
//displayGrades();
}
In the below function you need to use O as you are passing it but you are using
o1 which will overwrite every time
void readGrades(struct Outer *O)
{
cout<<"Enter the student's id: "<<endl;
cin>>O->data.id;
cout<<"Enter the student's midterm #1 grade: ";
cin>>O->data.midterm1;
cout<<"Enter the student's midterm #2 grade: ";
cin>>O->data.midterm2;
cout<<"Enter the student's lab and homework grade: ";
cin>>O->data.lab_H;
cout<<"Enter the student's final exam grade: ";
cin>>O->data.finalExam;
}
You should create a constructor that initializes the member variables to 0. Most of compilers do not do that by default.
struct Inner{
Inner() : id(0),midterm1(0),midtermTotal(0), lab_H(0), finalExam(0), total(0)
{
}
int id;
string name;
double midterm1;
double midterm2;
double midtermTotal;
double lab_H;
double finalExam;
double total;
};

Data file handling modify function error c++

class flight //main class flight which the user uses to book tickets
{
int booking_id;
int pnr,p_age;
char p_name[25],d_a_name[25],a_a_name[25],gender,departing_date[10],arrival_date[10],b_id;
long double price;
public:
flight()
{
static int id=0;
booking_id=id++;
}
void modfunction(int n);
};
void flight::modfunction(int n) //function for entering new values when the user has chosen to modify
{
getchar();
cout<<"Enter the passenger's name :";
gets(p_name);
cout<<"Enter the passenger age :";
cin>>p_age;
getchar();
cout<<"Enter the passenger's gender :";
cin>>gender;
getchar();
cout<<"Enter the departing date(dd-mm-yyyy) :";
gets(departing_date);
cout<<"Do you want to book return ticket with a 10% discount?(y/n) :";
cin>>return_ticket_input;
if(return_ticket_input=='y')
{
cout<<"Enter the arrival date :";
gets(arrival_date);
}
cout<<"Choose the airline.\n\n";
flights(departing_date,return_ticket_input,arrival_date);
cout<<"\n\nEnter the desired flight number :";
cin>>selected_fno;
}
void modify_ticket()//function for modifying a ticket
{
int n;
system("clear");
cout<<"Enter the booking id for modification ";
cin>>n;
flight f;
fstream fp("flight.dat",ios::binary);
ifstream ifile("flight.dat",ios::binary);
int found=0;
int count_variable = 0;
while(ifile.read((char*)&f,sizeof(f)))
{
if(n==f.retbid())
{
f.output();
f.modfunction(n);
// int s=sizeof(f) * (count_variable + 1);
fp.seekp(ifile.tellg());
fp.write((char*)&f,sizeof(f));
found = 1;
f.output();
getchar();
}
count_variable++;
}
ifile.close();
fp.close();
if(found==0)
{
cout<<"Passenger not founed!";
}
cout<<"Press any key to continue.";
getch();
}
So this is the minimal version of my problem.
The class named flight. a function called modfuction which is used to input data from the user. and another function called modify ticket used by the user to modify the record details.
The problem is even though the user enters the booking id(n) and it outputs the record, when the user tries to update it(modify) it stays the same!
please help me as this is a part of my school project!
thanks in advance!
Instead of fstream fp("flight.dat",ios::binary); write:
fstream fp("flight.dat",ios::binary|ios::in|ios::out);
P.S.: Encountered Same Problem A minute ago..

C++ saving records to file

i've been thinking how to do this "save to file" thing all night, but it seems that luck wasnt on my side.. .
I am new to this, and i want to get learned, thats all.
I am from Bulgaria, and for those who wonder what is "edinen", this is your citizen number as a member of that country. (I still dont know how to explain this...)
Here is what I've got
(I've already included the "fstream", but i still dont know how to use it!
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
#define n 30
int num=0;
struct uslugi
{
char name[30];
char surname[30];
char lastname[30];
char illness[30];
long int edinen;
}grupa[n];
void add_record();
void show_record();
void search_record();
void remove_record();
void add_record() // FUNCTION - ADD RECORD(S)
{
system("title Add Record");
int br;
cout<<"\n How many pacients do you want to enter?";
cout<<"\n >> ";
cin>>br;
for(int i=num;i<num+br;i++)
{
cout<<"\n \t\t ENTERING DATA FOR PACIENT NUMBER - "<<i+1<<endl;
cout<<"\n Name:";
cout<<"\n >> ";
cin>>grupa[i].Name;
cout<<"\n Surname:";
cout<<"\n >> ";
cin>>grupa[i].surname;
cout<<"\n Lastname:";
cout<<"\n >> ";
cin>>grupa[i].lastname;
cout<<"\n edinen:";
cout<<"\n >> ";
cin>>grupa[i].edinen;
cout<<"\n Ill from:";
cout<<"\n >> ";
cin>>grupa[i].illness;
}
num=num+br;
}
void show_record() // FUNCTION - SHOW RECORD(S)
{
if (num==0)
{
cout<<"\t\t by far there are no pacient at all \n";
}
cout<<"\n \t\t\t list with all pacients"<<num<<endl;
for(int i=0;i<num;i++)
{
cout<<"\n Name:";
cout<<"\n >> "<<grupa[i].name;
cout<<"\n Surname:";
cout<<"\n >> "<<grupa[i].surname;
cout<<"\n Lastname:";
cout<<"\n >> "<<grupa[i].lastname;
cout<<"\n edinen:";
cout<<"\n >> "<<grupa[i].edinen;
cout<<"\n Ill from:";
cout<<"\n >> "<<grupa[i].illness;
cout<<"\n\n";
}
}
void remove_record() // FUNCTION - DELETE RECORD(S)
{
int k,index;
for(int i=0;i<num;i++)
{
cout<<"\n("<<i+1<<") "<<endl;
cout<<"Pacient name: "<<grupa[i].name<<endl;
cout<<"Surname: "<<grupa[i].surname<<endl;
cout<<"Lastname: "<<grupa[i].lastname<<endl;
cout<<"edinen: "<<grupa[i].edinen<<endl;
cout<<"Ill from: "<<grupa[i].illness<<endl<<endl;
}
cout<<"Who do you want to delete? \n >> ";
cin>>index;
for(k=0;k<num;k++)
{
if(k>=index)
{
grupa[k].edinen=grupa[k+1].edinen;
}
if(k==num-1)
break;
else
cout<<"BY FAR THERE IS NO DATA HERE"<<endl;
}
num--;
for (i=0;i<num;i++)
{
cout<<"Pacient name: "<<grupa[i].name<<endl;
cout<<"Surname: "<<grupa[i].surname<<endl;
cout<<"Lastname: "<<grupa[i].lastname<<endl;
cout<<"edinen: "<<grupa[i].edinen<<endl;
cout<<"Ill from: "<<grupa[i].illness<<endl<<endl;
}
}
void print_count() // FUNCTION - PRINTING
{
cout<<"\n \t\t\t NUMBER OF PACIENTS - "<<num<<endl;
cout<<""<<endl;
}
void search_record() // FUNCTION - SEARCHING
{
int flag=0;
long int tempegn;
cout<<"\n Type the edinen for the pacient you search: ";
cout<<"\n >> ";
cin>>tempegn;
for (int i=0;i<n;i++)
if(tempegn==grupa[i].edinen)
{
cout<<"Pacient name: "<<grupa[i].name<<endl;
cout<<"Surname: "<<grupa[i].surname<<endl;
cout<<"Lastname: "<<grupa[i].lastname<<endl;
cout<<"edinen: "<<grupa[i].edinen<<endl;
cout<<"Ill from: "<<grupa[i].illness<<endl<<endl;
flag++;
}
if (!flag)
{
cout<<"\n\t PACIENT WITH THAT NAME DOESNT EXCIST \n\n";
}
}
void main() // MAIN FUNCTION (MENU)
{
int choice;
do
{
cout<<"\n\t\t******************* Menu *******************"<<endl;
cout<<"\t\t* *";
cout<<"\n\t\t* 1.Add new pacient *";
cout<<"\n\t\t* 2.Search for a pacient by edinen *";
cout<<"\n\t\t* 3.Delete pacient *";
cout<<"\n\t\t* 4.List with all pacients *";
cout<<"\n\t\t* 5.Exit *";
cout<<"\n\t\t* *\n";
cout<<"\t\t************************************************"<<endl;
cout<<"\n Type your choice! ";
cout<<"\n >> ";
cin>>choice;
switch(choice)
{
case 1:{print_count();add_record();break;}
case 2:{print_count();search_record();break;}
case 3:{print_count();remove_record();break;}
case 4:{print_count();show_record();}
}
}
while(choice!=5);
}
I would suggest you read a book, or at least some documentation about this. Also, to understand how this kind of input works, it is required knowledge of oop concepts. As far as I noticed you do not have such a knowledge, I would suggest you stick for a while with procedural i/o functions from C, like fprintf. A google search will show many resources about them and they are pretty easy to understand, at least compared with those from C++, like the ones you are using.
To answer to your question, I will try to explain how to achive what you want. Note that this is a very simplified explanation and not 100% accurate.
Firstly, you need to open the file and create a i/o stream, using ifstream for input files or ofstrsam for output files:
ifstream f("input_filename");
ofstream g("output_filename);
After you created f and/or g (they are not both requied. Also, f and g are just random names) you can use them in the same way you use cin and cout, in this case, as f is for input, it should be used like cin.