DaisyJ-K
DaisyJ-K

Reputation: 15

C++ : find word in a string, count how many times was found, then print meaning of the word

I'm doing the assignment and I'm at the end of my powers. Right now I can't figure out what's missing or what I could change. I need the program to read me a file. If it finds the beginning of the search word, it lists the word and its meaning. If he finds it more than once, he writes only that word without meaning. Right now, if the program finds more words, it writes the meaning for the first word and writes the word for the other words found. I don't know what other cycle I could use. If you could help me, I would be grateful.

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include<bits/stdc++.h>


using namespace std;

int main()
{

ifstream dictionary("dictionary.txt");
if(!dictionary.is_open()){
    cout<< "File failed to open" << endl;
    return 0;
    }

int option;

    cout << "1.<starting>" << endl;
    cout << "4.<stop>" << endl;
    cin >> option;

string find_word;
string word, meaning;
string line;
string found;
int count = 0;



    if (option == 1)
    {
        cout << "Find the meaning of the word beginning with the characters:";
        cin >> find_word;

        while (getline(dictionary,line))
            {
                stringstream ss(line);
                getline (ss, word, ';');
                getline (ss, meaning, ';');

            if (word.rfind(find_word, 0) != string::npos)
                {
                    count++;
                    if (count <=1)
                    {
                        found = word + meaning;
                        cout << found << endl;
                    }

                    if (count >= 2)
                    {
                        found = word ;
                        cout << found << endl;
                    }
                }
            }
    }


      if (option == 4)
    {
        return 0;
    }

dictionary.close();

    return 0;
}


EDIT dictionary.txt looks like this:

attention; attentionmeaning
attention; attentionmeaning2
computer; computermeaning
criminal; criminalmeaning
boat; boatmeaning
alien; alienmeaning
atter; meaning
.
.
etc.

For example input is:

Find the meaning of the word beginning with the characters: att

this is what i get now (output):

attention attentionmeaning
attention
atter

this is what i expect (desire output):

attention 
attention
atter

if program find only one searching word it should write this:

Find the meaning of the word beginning with the characters: bo

output:

boat boatmeaning

Upvotes: 0

Views: 315

Answers (2)

ProXicT
ProXicT

Reputation: 1933

As it was already suggested, while reading the file, you don't know if there will be more than one entries matching your search term. That being said, you need some intermediate structure to store all the matching entries.

After you have gathered all the results, you can easily check if the data contains more than one result, in which case you only print the "word" without the meaning. In case there is only one result, you can print the "word" together with its meaning.

The code for that could look something like this:

struct Entry {
    std::string name;
    std::string meaning;

    bool startsWith(const std::string& str) {
        return name.find(str) != std::string::npos;
    }
};

Entry createEntry(const std::string& line) {
    Entry entry;
    std::stringstream ss(line);
    std::getline(ss, entry.name, ';');
    std::getline(ss, entry.meaning, ';');
    return entry;
}

int main() {
    std::string query = "att";
    std::ifstream dictionary("dictionary.txt");
    std::vector<Entry> entries;
    std::string line;
    while (std::getline(dictionary, line)) {
        Entry entry = createEntry(line);
        if (entry.startsWith(query)) {
            entries.emplace_back(std::move(entry));
        }
    }

    for (const Entry& entry : entries) {
        std::cout << entry.name << (entries.size() > 1 ? "\n" : " " + entry.meaning + '\n');
    }
}

This code could definitely be more optimized, but for the sake of simplicity, this should suffice.


Demo

Upvotes: 1

Eric T-M
Eric T-M

Reputation: 93

The problem is that at the first time through the loop you do not know if there is one or more valid words that follow from your string. I would suggest you create an empty list outside the loop, and push all the word and meaning pairs that match onto the list. Then after if the size of the list is 1 you can output the word and meaning pair else use a for loop to loop through and just print the words.

Upvotes: 0

Related Questions