Problem with search method

Hello,
I'm writing a search method to take a user's input, then look through a txt file for the user's name and display the contents related to the user. My program works, however it displays all users in the text file.

The text file contains the following contents:

jim,111,5551111
sarah,222,5552222
sam,333,5553333

Below is my code:

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
 fstream fin;
    fin.open("rolodex.txt");

    string contactName;
    string search;

    cout << "Enter contact name :";
    cin >> contactName;

    while(fin.good())
    {
        getline(fin, contactName);


        int locOfCma1 = contactName.find (',');
        int locOfCma2 = contactName.find (',',locOfCma1+1);
        int locOfCma3 = contactName.find (',',locOfCma2+1);

        string name = contactName.substr(0,locOfCma1);
        string zip  = contactName.substr(locOfCma1 +1, locOfCma2 - locOfCma1 -1);
        string teleNum = contactName.substr(locOfCma2 + 1, locOfCma3 );

        cout <<endl;
        cout << name << zip << teleNum;

    }

  fin.close(); // closing the file


The program is displaying all data for all users. Where I'd like to type in jim, and then display 111,5551111. Thank you for taking time to read this. All input is greatly appreciated.
I know this doesn't pertain to much to your problem, but it greatly helps others if you add one-line comments in your code describing the intent. It also looks like you're not doing "sanity-checking" on variables. Meaning, what if the variable fin doesn't initialize properly? The whole thing will "explode" so-to-say. But anyway, It looks as though each thing you are looking for is on it's own line in the document? If this is the case, you can read all the line text at once (in a sense) by using the fstream method
fstream::getline()
and if you give it the following arguments
fin.getline(pCharBuff, 128, '\n'); where "pCharBuff" is a pointer to a character buffer.
Then the function should read up to 128 characters OR until it hits the newline (\n) character.
I'll leave parsing the string up to you but that should guarantee that you get only one line from the doc.

To search for a string isn't to hard either:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// * pName is assumed to be the given name by the user.
size_t iSizeOfStr = strlen(pName); // We need the length of the input.

// Allocate new memory for the comparitor.
char *pCompName = new char[64];
memset(pCompName, 0, 64);

while(nullptr != pCompName) // For non-C++11 code, use NULL.
{
    // Since we are using fstream, fStrm will be assumed as fstream fStrm.
    fStrm.read(pCompName, iSizeOfStr);

    if (!strcmp(pComName, Name))
    {
        // Parsing code goes here.
        break;
    }
}

delete[] pComName;


This code is obviously incomplete, but I'll leave it up to you to fill in the blanks. But what this does is it extracts the name from the file and compares it to the one given, if strcmp() returns 0, then they are the same.
Topic archived. No new replies allowed.