Write your question here.
Write your question here.
I am having a hard time understanding what I am doing wrong in this program
and was just wondering if someone could give me some pointers.
I am making a program for extra credit it has to do with dealing with using iterators and STL correctly with functions.
My instructions were:
Declare a structure called Employee with appropriate member variables and read the file contents into member variables of the Employee structure. Create a STL list of the structure.
Design functions for the following purposes
1. Removing any entry by ID specified by the user
2. List the data of the employees with the last name starting with a character provided by the user
It works for removing an entry by the ID but when trying to only print the entries of the user Selected Character of the last name it will not work correctly. I am assuming there is something wrong with my if statement for it but I do not know why My logic is wrong or maybe it is my syntax. This is my first time asking a question on here and it seems I can't even so the preview of my question correctly lol.
#include <iostream>
#include <list>
#include <fstream>
#include <string>
usingnamespace std;
struct Employee
{
string fname;
string lname;
longint ID;
double salary;
};
// Function to erase user entry and
void erased_entry(list<Employee>& newinfo, longint IDERASED,EmployeeF,char charentry);
int main()
{
// Declared variables
list<Employee> info;
Employee E;
string fname,lname;
longint ID, ID2;
char first_char;
double salary;
ifstream in_file;
// Open text file
in_file.open("test.txt");
while(in_file>>fname>>lname>>ID>>salary)
{
E.fname=fname;
E.lname=lname;
E.ID=ID;
E.salary=salary;
info.push_back(E);
}
// Initializing iterator and seeding it to the front
list<Employee>::iterator iter=info.begin();
// Print out list
for(iter=info.begin();iter!=info.end();iter++)
{
E=*iter;
cout<<E.fname<<" "<<E.lname<<" "<<E.ID<<" "<<E.salary<<endl;
}
// Prompt User for Removing entry by ID and List of Data by a character by the user
cout << "Please indicate an ID to be removed:";
cin >> ID2;
ID = ID2;
cout << endl;
cout << "Please indicate what employees you would like to see with a character:";
cin >> first_char;
cout << endl;
//Call functions here
erased_entry(info,ID,E,first_char);
//Close test.txt
in_file.close();
return 0;
}
void erased_entry(list<Employee>& newinfo, longint IDERASED, Employee F,char charentry)
{
list<Employee>::iterator iter=newinfo.begin();
for(iter=newinfo.begin();iter!=newinfo.end();iter++)
{
if(iter->ID = IDERASED)
iter=newinfo.erase(iter);
if(iter->lname.at(0) != charentry)
iter=newinfo.erase(iter);
F=*iter;
cout << F.fname << " " << F.lname << " " << F.ID << " " << F.salary << endl;
}
}
> Design functions for the following purposes
> 1. Removing any entry by ID specified by the user
> 2. List the data of the employees with the last name starting with a character provided by the user
These are two different functions;
one to erase an entry, given the id,
another to list all entries with the last name starting with the given character.
Thanks Peter87 and JLBorges with your responses. So I couldn't understand how to make two functions do the different actions in them and return it back to main, so that is why I had it in all one function. What I am getting from your code JLBorges is that you suggest making multiple functions being that one creates the STL For the Structure Employee,one to remove entry by ID,one to print the list, one to compare the string and first character in the string, and then list the data of the employees only with the last name starting with a character, and then call all of those functions to the Main?
The only functions that you are required to write are one to remove entry by ID, and another to list the data of the employees with the last name starting with a particular character.
With the code that I posted, your complete main() would look something like:
int main()
{
// get the list of employees from data in the file
std::list<Employee> info = get_employees( "test.txt" ) ;
// ask the user for the id of the employee to be removed from the list
std::cout << "id to remove? " ;
long id_2b_removed ;
std::cin >> id_2b_removed ;
// try to removed employee with that id
std::cout << "employee with id " << id_2b_removed << " was " ;
if( erase( info, id_2b_removed ) ) std::cout << "removed\n" ;
else std::cout << "not found\n" ;
// ask the user for the first character of the last name
std::cout << "first character of the last name? " ;
char first_char ;
std::cin >> first_char ;
// list the employees with the last name starting with first_char
print_selected( info, first_char ) ;
}
Ok thanks for the tip. So for what I wanted to do was let the user see the list first then Decide whether which ID to get rid of and that part works. It is the second function that is giving me problems.
John Harris 1000 35000.00
Lisa Smith 1002 75000.00
Adam Johnson 1007 68500.00
Sheila Smith 1009 10000.00
Tristen Major 1012 75800.00
Yannic Lennart 1015 58000.00
Lorena Emil 1018 43000.00
Tereza Santeri 1020 48000.00
So when I prompt the user to give me a character to only list it gives me the last entry in the list container Tereza Santeri 1020 48000.00
Do you know what I might be doing wrong for it to only print out that one iterator?
Here is my new code: