I have an assignment to do, where I have to write a program that reads some names from a file, then prints them in an out file and on the screen. Then I need to allow the user to enter a name, and have the program list what number the name is in the files. i.e.
name
name1
name2
name3
name1
name
name4
Enter a name: name1
name1 appears at position(s) 2, 5
Why not just 1 loop? It's all you need. If one loop is immediately proceeded by another that are similar, the Loop Fusion[1] optimisation will take effect. It's when the compiler fuses two loops together to make 1. Here's some descriptive code to illustrate this:
1 2 3 4 5 6 7
Integer Array[10];
while( Not the end of input )
Arrayn = /* User's input... */;
while( Not at the end of Array )
/* Print the value at Arrayn */;
If the optimisation takes place, the result would be:
1 2 3 4 5
while( Not the end of input )
{
Arrayn = /* User's input... */;
Print( /* Print the value at Arrayn */ );
}
I'll look a this in the morning, but I'll say that as you run down the file looking for a match, then once you find the first match, then you can STORE the rec_Num, i.e. "2" into an array and continue through the file looking for more hits , which you will, in this case, come across in record number # "5".
So one loop that scans the file
for FileSearch = recnum ; recnum < eof(); recnum++ )
if input_name = name_in_file{
a_rec_num[i] = recnum, i++;
so that when finished, your array "a_rec_num[]" contains the rec numbers
a_rec_num[0] = 2
a_rec_num[1] = 5
One LOOP
One file scan
One array filled with answers at the end.