having trouble with for loop

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 thought i would use a getline function but now i need to use a for loop but 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)

thanks
Last edited on
I don't think that you have to use a getline function. For instance, I will use the fscanf(file_pointer, "%s", your_string) function because you will read the lines from a file.

Here I go:

If you know exactely how many lines is in that file, is very easy. You use a for-loop for(contor=0; contor<lines_number; contor++). Now, you use the fscanf function, described below (if you want more information about it, is in the reference of that site) for reading the line. You have to work on a single line (you read the line then you work with it and then the vector (the string) work with another line) so in that loop, you have to make all the operations you need on that vector.

After you read the line, you have to work with it. You can do it like this:
1
2
3
4
5
for(contor_4=0;  isspace(your_string[contor_4])!=0; contor_4++)
    {    vector_2=your_string[another_contor]; }
    for(contor_3=contor_4; your_string[contor_3]; contor_3++)
    {    vector_3=your_string[contor_3]; }
}

This is just a suggestion. I know that others people on this forum have a better ideas.
In vector_2 you get a part of that line (for instance the name or that dates)
In vector_3 you get the other part of that line (for instance that dates or the name)

For doing this, the lines in the file should be like this: 1262011 John. This is a simple way because the line is splitted in 2 parts: the numbers and the name. Between them is a space. And you can easely read those to parts and memorize them in 2 separated vectors. You can also make the line like this: 12 6 2011 character Jonh. Where I wrote "character", you can put any character you want and to read that two parts of the line (the numbers and the name) you read the string (and you put it in a vector) untill you reach that character. When you have reached the character, you keep the current value of the contor and then you read the last part of the line (you must do that with a for-loop and the condition of the loop must be your_string; which is equivalent with your_string!=NULL and you put it in another vector. This is the generalized version of the algorithm because in the first version, between those parts it was space which is a character. If you use this version, don't worry about the printing part of the numbers: they are stored in the vector like this: 1262011 but when you print them, you ca easely insert a space wherever you want like this: cout<<vector_numbers[0]<<vector_numbers[1]<<" "<<vector_numbers[2]<<... I hope you understood the idea.

So, now, we stored the numbers and the numbers in two separated vectors. A very important aspect is the index of the vectors. The first component of the numbers_vector (the vector where are stored the numbers) has the correspondant the first component in the names_vector ( the vector where are stored the names). This is very important.

Now, you've stored 30 names and dates (this is an example). There is possible that 2 or 3 or 10 names to be the same. The idea is like that: if you want to print all the dates of a name, you have to find the indexes of that number and then to print the elements in the numbers_vector which have that indexes. More clearly: a name is stored 3 times in names_vector like that: names_vector[2], names_vector[4] and names_vector[10]. Those components has stored in them the "Jack" name. The indexes of those components which have stored in them the same name are: 2, 4 and 10. You use that indexes to print the numbers_vector[2], numbers_vector[4] and the numbers_vector[10]. I hope I was clear enough. I can't write some code for you right now because I'm tired but if you really want that, and if you didn't understand all those things that I am trying to explain here, I will try something for you.
Last edited on
thanks for the reply but i'm still a bit confused. heres the code i wrote so far (there are some gaps because they are more parts to the program). I'm using structs which i've never done before. I understand i need to use a loop but i'm still not sure exactly what to put in the loop

include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct Date
{
int day;
int month;
int year;
};

struct Personnel
{
string name;
}; Personnel person;

void DoListDates(void);
int DisplayMenu (void);

int _tmain(int argc, _TCHAR* argv[])
{
Date date[30];
Personnel person[30];

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();

int option;
do
{
option = DisplayMenu();
switch ( option )
{
case 1:
DoListDates();
break;
case 2:

break;
case 3:
break;
case 4:

break;
case 5:
break;
case 6:

break;
case 7:
break;

default :
cout << "Wrong option\n";
}
} while (option != 7);

return 0;
}

int DisplayMenu (void)
{

int option;

cout << endl;
cout << "\t1. List the dates when a given person was on duty\n";
cout << "\t2. \n";
cout << "\t3. \n";
cout << "\t4. \n";
cout << "\t5. \n";
cout << "\t6. \n";
cout << "\t7. To Exit\n";
cout << "\t\tEnter option : ";
cin >> option;
return option;

void DoListDates(void)
{
for (int i = 0; i < 30; i++)
{

}
}
Please use the tags because I can't understand your code. And one thing...if you want to solve your problem, please tell me exactely what you want to do in that program because I don't think that in the code below you just want to print some strings from a file.
ok so im using structs for the date and the name:
struct Date
{
int day;
int month;
int year;
};

struct Personnel
{
string name;
}; Personnel person;

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

the rest of the code is just for a menu so don't worry about that bit

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
Topic archived. No new replies allowed.