Help with searching an array

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



Here's what I have so far:
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
42
43
44
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int cnt = 0, index = 0;
    
    string names[50];
    ifstream inData("C:\\Names\\inData.dat");
    ofstream outData("C:\\Names\\coutData.out");
    cout << "Name Search Program \n";
    outData << "Name Search Program \n";
    
    while (!inData.eof())
    {
          getline(inData, names[cnt]);
          cnt++;
    }
    while (index < cnt)
    {
          outData << names[index] << endl;
          cout << names[index] << endl;
          index++;
    }
    string search;
    inData.close();
    outData.close();
    cout << "\nEnter a name to search for: ";
    getline(cin, search);
    
    int position[50];
    
    for(int count = 0; count < 50; count++)
    {
            if(names[count] == search);
            position[count];
    }
    
    cout << search << " appears at position(s) " << position[count];
    
    system("pause>nul");
    return(0);
}
Last edited on
you need two for loops, one to move the array along and one to set placement value for each found name. please no system functions by the way.
I don't understand what you're saying. Two nested for loops, or two seperate loops? And I'm required to use system functions for this class.
closed account (zb0S216C)
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 */ );
}


References:
[1] http://en.wikipedia.org/wiki/Loop_fusion


Wazzak
I still don't know how to do this :/
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.



Last edited on
Thank you very much, Incis.
Topic archived. No new replies allowed.