简体   繁体   English

用C ++查找单词

[英]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. 我的名单上有名字,后面是他们的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. 我想添加一个功能,即在找到名称后立即显示该数字,我将其声明为int GPA,但不确定如何将其合并到我的程序中。

#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;
    }

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 ? 例如,如果用户输入Carl怎么办? It will be found as part of the string Carlos . 它会在字符串Carlos Or indeed, if they enter 2 , it will match parts of the GPA for multiple people. 甚至确实,如果他们输入2 ,它将匹配多个人的GPA部分。

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. 假设它不包含空格,这将使其与您在其中读取用户名的方式保持一致。顺便说一下,您需要包括<sstream> Note that you can read out the GPA as part of this same mechanism. 请注意,您可以读出GPA作为该相同机制的一部分。

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. 厚脸皮的方法是仅使用旧的C函数。 I know there are C++ methods for this, but none are as simple as good old stricmp from <cstring> : 我知道有C ++方法可以实现,但是没有一个方法像<cstring>stricmp一样简单:

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

You can split line using stringstream, and store it into a vector, like this: 您可以使用stringstream分割line ,并将其存储到向量中,如下所示:

#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;
    }
}

You can replace getline(read_file, line)... with: 您可以将getline(read_file, line)...替换为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM