Locating a Name in a Data File

Hi,
Basically, this code is suppose to allow the user to search through a list of names stored in a file for a particular one. The names in the file will be stored one per line (spaces are allowed in the names).
So lets say this is my data file:

Michael Jackson
Justin Bieber
Taylor Swift
Kanye West

So, I'm wondering how to locate a name in the file, and write what position the name is in? I'm pretty sure it has something to do with the .compare function, but I'm not exactly sure how to use it in this case.
Thanks~

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
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>

using namespace std;

int main()
{
ifstream names_file;  // declare an input file stream variable
ofstream output_file;
    string name_to_find; // file name variable
	char name_from_file[1000];

    cout << "Please enter the name of your names file: ";
    getline(cin, name_to_find);  // can use setw, but loses spaces

    names_file.open(name_to_find.c_str()); // open file with this name
    while (!names_file)    // while the file did NOT open successfully
    {
        names_file.close();  // close the OS connection -- freeing resources
        names_file.clear();  // clear the open failure flag

        cout << "\nI'm sorry, I could not open '" << name_to_find << "'. Please enter another name: ";
        getline(cin, name_to_find);

        names_file.open(name_to_find.c_str());
		
		cout << "\nFile '" << name_to_find << "' opened successfully!\n\n";
		
		cout << "What name would you like to find in this file? ";
		cin >> name_from_file;
		
    }
	
	cout << "\nThank you for using the NSP!!\n\n";
	cout << "Endeavor to have a amazing day!\n\n";	   
	
}
First, this is C++ so you should be declaring your variables close to first use, not in one big glob at the beginning of the scopes. Next you should be using constructors to open your files, whenever possible, instead of calling the open() function and let the class destructors do their jobs of closing the files. Unless your using an outdated compiler you should be able to use C++ strings when opening or constructing your file streams, using the c_str() method should no longer be required.

You should also avoid C-strings whenever possible, prefer C++ strings.

Your variable names could also use some work, use more meaningful names. And try to eliminate some of the unnecessary comments, let your code be your documentation by using meaningful variable names and declaring variables closer to their actual use.

Perhaps something more like:

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
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string file_name;
    cout << "Please enter the name of your names file: ";
    getline(cin, file_name);

    ifstream names_file(file_name);

    while(!names_file)
    {
        names_file.clear();

        cout << "\nI'm sorry, I could not open '" << file_name << "'. Please enter another name: ";
        getline(cin, file_name);

        names_file.open(file_name);
    }

    cout << "\nFile '" << file_name << "' opened successfully!\n\n";

    string name_to_find;
    cout << "What name would you like to find in this file? ";
    getline(cin, name_to_find);

    // Now read though the file to try to find the name.
    string current_name;
    int counter = 0;
    bool found = false;

    while(!found && getline(names_file, current_name))
    {
        if(current_name == name_to_find)
            found = true;
        counter++;
    }

    if(found)
    {
        cout << name_to_find << " is name " << counter << " in the file.\n";
    }
    else
    {
        cout << "Could not find " << name_to_find << " in the input file.\n";
    }

    cout << "\nThank you for using the NSP!!\n\n";
    cout << "Endeavor to have a amazing day!\n\n";

}


jlb’s code is neater and I definitely suggest you to stick to it.
Anyway, when you ask for a “position” inside a file, you’re using a vague expression, which can (likely) refers to a line, but also to the “byte” where the data is.
That’s why I’m proposing you another example, just for hints.

Happy coding!
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Basically, this code is suppose to allow the user to search through
// a list of names stored in a file for a particular one.
// The names in the file will be stored one per line
// (spaces are allowed in the names).

// So lets say this is my data file:
// Michael Jackson
// Justin Bieber
// Taylor Swift
// Kanye West

// So, I'm wondering how to locate a name in the file, and write
// what position the name is in?
// I'm pretty sure it has something to do with the .compare function,
// but I'm not exactly sure how to use it in this case.
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::string name_to_find = " "; // file name variable
    std::ifstream names_file;
    while(!name_to_find.empty())
    {
        std::cout << "\n\nPlease enter the name of your names file "
                     "or just press ENTER to exit: ";
        std::getline(std::cin, name_to_find);
        if(name_to_find.empty())
            break;

        names_file.open(name_to_find);
        if(names_file.is_open())
        {
            std::cout << "What name would you like to find in this file? ";
            std::string name_from_file;
            std::getline(std::cin, name_from_file);
            std::string readname;
            bool found {false};
            while(std::getline(names_file, readname))
            {
                if(name_from_file == readname)
                {
                    std::cout << "Found a correspondence for the required data "
                                 "at position "
                              << ( (long)names_file.tellg()
                                  - name_from_file.length() )
                              << "\n";
                    found = true;
                }
            }
            if(!found)
            {
                std::cout << "Sorry, no match for the required name.\n\n";
            }
        }
        else
        {
            std::cout << "Can't find any file with that name.\n"
                         "Please, try again.\n\n";
        }
        names_file.close();
    }

    std::cout << "\nThank you for using the NSP!!\n\n";
    std::cout << "Endeavor to have a amazing day!\n\n";

    return 0;
}

Topic archived. No new replies allowed.