read text file into array

Jun 15, 2015 at 10:17pm
I'm fairly new to c++, and I've just finished writing this function but it won't work. I'm trying to copy the text file records into an array then display the record with matching index to the ID the user entered. could you please point out what i've done wrong here. Thanks.
void searchById()
{
int empID[10] = {0};
string firstName[10] = {""};
string lastName[10] = {""};
int month[10] = {0};
int day[10] = {0};
int requestedId = 0;
int index = 0;
//declare object file
ifstream empIn;
empIn.open("employees.txt", ios::in);
cout << endl;
cout << "Enter employee ID to search for, or (-1) to exit: ";
cin >> requestedId;
while (isalnum(empIn.peek()) && index < 10 && requestedId != -1)
{
empIn >> empID[index];
//empIn.ignore(5, '\n');
empIn >> lastName[index];
empIn >> firstName[index];
empIn >> month[index];
empIn.ignore();
empIn >> day[index];
index++;
}//end while

if (empID[index] == requestedId)
{
cout << "Employee # " << empID[index] << '|' << lastName[index] << ',' << firstName[index] << '|' << month[index] << '/' << day[index] << endl;
}
empIn.close();

if (requestedId == -1)
{
cout << "Return to main menu." << endl;
}
else
{
cout << "This employee ID could not be found.";
}//end if
}
Last edited on Jun 15, 2015 at 10:34pm
Jun 16, 2015 at 4:45am
What errors are you getting ?
Make your request clear.
Jun 16, 2015 at 6:00am
Use code tags
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{
int empID[10] = {0};
string firstName[10] = {""};
string lastName[10] = {""};
int month[10] = {0};
int day[10] = {0};
int requestedId = 0;
int index = 0;
//declare object file
ifstream empIn;
empIn.open("employees.txt", ios::in);
cout << endl;
cout << "Enter employee ID to search for, or (-1) to exit: ";
cin >> requestedId;
while (isalnum(empIn.peek()) && index < 10 && requestedId != -1)
{
empIn >> empID[index];
//empIn.ignore(5, '\n');
empIn >> lastName[index];
empIn >> firstName[index];
empIn >> month[index];
empIn.ignore();
empIn >> day[index];
index++;
}//end while

if (empID[index] == requestedId)
{
cout << "Employee # " << empID[index] << '|' << lastName[index] << ',' << firstName[index] << '|' << month[index] << '/' << day[index] << endl;
}
empIn.close();

if (requestedId == -1)
{
cout << "Return to main menu." << endl;
}
else
{
cout << "This employee ID could not be found.";
}//end if
}
Jun 17, 2015 at 4:59pm
The user should enter an employee ID, the function then should search in text file records for matching ID and display the entire record.
The function actually is not showing anything, and I can't figure why.
Topic archived. No new replies allowed.