I have a text file with 30 records of peoples names and the dates they worked. A record would look something like this: 12 6 2011 John. The same person will be repeated serveral times in the file. I need to be able to enter a name and display all the dates that the person worked.
i'm not sure how to use the loop to read from a text file (the text file cannot be edited it is just a list of dates and names)
here is the code:
//so im using structs for the date and the name:
struct Date
{
int day;
int month;
int year;
};
struct Personnel
{
string name;
}; Personnel person;
void DoListDates(void);
//this part is reading and displaying the text file:
ifstream infile ("report.txt");
while(!infile.eof())
for (int i = 0; i < 30; i++)
{
infile >> date[i].day >> date[i].month >> date[i].year >> person[i].name
}
cout << "Date\t\tName\n";
for (int i = 0; i < 30; i++)
{
cout << date[i].day << "/" << date[i].month << "/" << date[i].year << "\t" << person[i].name << endl;
}
infile.close();
//all this code works fine the file displays no problem
//here is where i am having the problem:
void DoListDates(void)
{
for (int i = 0; i < 30; i++)
{
}
}
i don't know what to put into this for loop to read the file
i what the program to ask to enter a name then display the dates from the text file
i don't know what to put into this for loop to read the file
Well, you don't need to read anything from the file because you have gotten all the information stored into your arrays already (which I don't see declared anywhere in your code, so I will assume you have it declared somewhere).
In the doListDates function, I would keep it as a void function but use three parameter for the name (a string) and the two arrays. Then you could use the for loop you have setup and print the dates when you find the person using an if statment such as :
1 2
if(person[i] == name) //Check for match
output corresponding date
void DoListDates(void)
For the future, try not to put the word void in the parantheses.. it is just bad practice and not standard.