Cris
Cris

Reputation: 128

Finding a word in C++

I'm able to find the word in my list but I would like to display whatever number there is after the word has been found. My list has names followed by their GPA.

Example...

michael 2.3

Rachel 2.5

Carlos 3.0

I would like to add the feature of displaying the number located after the name once it's found, I declared as int GPA but I'm not sure how to incorporated in my program.

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    string name;
    int offset;
    string line;
    int gpa;

    ifstream read_file;
    read_file.open("alpha.dat");
    cout << "Please enter your name: \n";
    cin >> name;

    if (read_file.is_open())
    {
        while (!read_file.eof())
        {
            getline(read_file, line);
            if ((offset = line.find(name)) != string::npos)
            {
                cout << "the word has been found: \n";
                // cout << name << gpa; example to display
            }
        }
        read_file.close();
        return 0;
    }

Upvotes: 1

Views: 514

Answers (3)

Chiara Hsieh
Chiara Hsieh

Reputation: 3393

You can split line using stringstream, and store it into a vector, like this:

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
    string name;
    int offset;
    string line;
    int gpa;

    ifstream read_file;
    read_file.open("alpha.dat");
    cout << "Please enter your name: \n";
    cin >> name;

    if (read_file.is_open())
    {
        while (!read_file.eof())
        {
            getline(read_file, line);
            if ((offset = line.find(name)) != string::npos)
            {
                cout << "the word has been found: \n";
                stringstream iss(line);
                vector<string> tokens;

                string str;

                while (iss >> str)
                    tokens.push_back(str);

                cout << tokens[0] << tokens[1];

            }
        }
        read_file.close();
        return 0;
    }
}

Upvotes: 2

paddy
paddy

Reputation: 63471

As far as I can tell, you just need to output the line that you read from file:

while( getline(read_file, line) )
{
    if ((offset = line.find(name)) != string::npos) cout << line << endl;
}

Note that this isn't the best way to find the name. For example, what if the user enters Carl? It will be found as part of the string Carlos. Or indeed, if they enter 2, it will match parts of the GPA for multiple people.

What you can do here is use a string stream to read the name out. Let's assume it contains no spaces, which would make it conform to how you are reading the user's name in. You need to include <sstream>, by the way. Note that you can read out the GPA as part of this same mechanism.

istringstream iss( line );
string thisname, gpa;

if( iss >> thisname >> gpa ) {
    if( thisname == name ) cout << name << " " << gpa << endl;
}

Finally, you may want to consider ignoring case when comparing strings. The cheeky way is to just use the old C functions for this. I know there are C++ methods for this, but none are as simple as good old stricmp from <cstring>:

if( 0 == stricmp(thisname.c_str(), name.c_str()) ) {
    cout << name << " " << gpa << endl;
}

Upvotes: 2

jaho
jaho

Reputation: 5002

You can replace getline(read_file, line)... with:

read_file >> name >> gpa;
if (name == search_name)
    cout << name << " " << gpa << endl;

Upvotes: 0

Related Questions