0

I have an exercise in which I need to find words in a text file starting with user input symbol. I also need to determine in which line that word is and output that in text different text file. I managed to write code to output words starting with symbol and count word's position in text, but i cannot figure out how to count in which line that word is. Also i need to find those words which have symbols like ? ! etc. not only ' ' For example if i wanna find words starting with c then my program finds only "cerebral, cortex, could, create" but not "construct, capable, computers" from my example input which is below my code.

#include <iostream>
#include <fstream>
using namespace std;

int main() {

    fstream input;
    fstream output;
    string word, line;
    char startOfWord;

    cout << "I wanna find words starting with symbol: \n";

    cin >> startOfWord;

    int lineNumber = 0;

    input.open("f.txt");
    output.open("f1.txt");

    while (!input.eof()) {

        input >> word;
        lineNumber++;
        if (word.length() > 40) {
            continue;
        }
        if (word[0] == startOfWord) {
            output << word << ' ' << lineNumber << '\n';
        }
    }

    input.close();

    output.close();

    return 0;

}

Example input: user wanna find words starting with a.

f.txt:

A Stanford University project to?construct a model 
of the cerebral cortex in silicon could help scientists 
gain a better understanding of the brain, in order to 
create more,capable.computers and advanced 
neural prosthetics. 

Output: f1.txt

a 1
a 3
and 4
advanced 4
StoryTeller - Unslander Monica
  • 148,497
  • 21
  • 320
  • 399
  • What is the problem with the `lineNumber` variable you are already using? – mkrieger1 Mar 07 '21 at 08:31
  • @mkrieger1 it's not counting lines but words – Anna Volksone Mar 07 '21 at 08:33
  • Does this answer your question? [Read file line by line using ifstream in C++](https://stackoverflow.com/questions/7868936/read-file-line-by-line-using-ifstream-in-c) – mkrieger1 Mar 07 '21 at 08:34
  • @mkrieger1 i tried using getline() to count lines but i was unsuccesful. When i put it as a condition in while loop then nothing is working. I dont understand where to put things :(. The thing is im just a beginner and started programming just couple months ago. Seems like when i will get this thing done i will be on the top of the world. – Anna Volksone Mar 07 '21 at 08:46
  • Please show your attempt using `getline` and explain the problem you were having with it – Alan Birtles Mar 07 '21 at 09:21
  • Side note: [Why is iostream::eof inside a loop condition (i.e. while (!stream.eof())) considered wrong?](https://stackoverflow.com/questions/5605125/) – Remy Lebeau Mar 07 '21 at 10:58

2 Answers2

0

You are reading the file word by word, counting each word. You need to read the file line by line, counting each line, and then you can read words from each line.

Try this:

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

int main() {
    ifstream input;
    ofstream output;
    string word, line;
    char startOfWord;

    cout << "I wanna find words starting with symbol: \n";

    cin >> startOfWord;

    int lineNumber = 0;

    input.open("f.txt");
    output.open("f1.txt");

    while (getline(input, line)) {
        ++lineNumber;
        istringstream iss(line);
        while (iss >> word) {
            if (word.length() > 40) {
                continue;
            }
            if (word[0] == startOfWord) {
                output << word << ' ' << lineNumber << '\n';
            }
        }
   }

    input.close();
    output.close();

    return 0;
}
Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
  • Thank you so much! This worked!:) – Anna Volksone Mar 07 '21 at 13:49
  • Maybe you have an idea how can i check those words which are not separated by `' '` but some other punctuation marks like `?` – Anna Volksone Mar 07 '21 at 14:07
  • @AnnaVolksone that gets into the realm of writing a lexical token parser, which is a bit more difficult. You need to define what does and does not delimit words, and them look for those delimiters. I would start with using `std::string::find_first_(not_)of()` and `std::string_substr()` rather than `operator>>`. But as the text gets more complex (handling Unicode/locales, etc), so will the rules of a parser. – Remy Lebeau Mar 07 '21 at 18:31
0

I did it! This works!

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;

void getWordsFromLine(string word, int index, vector<string> &memory) { //funkcija kas izvelk vardus no rindam
    string newWord;
    for (int i = index; i < word.length(); i++) {
        if (word[i] == ')' || word[i] == '(' || word[i] == '.' || word[i] == ',' || word[i] == '=' || word[i] == '?' || word[i] == '!') { //parbauda visas iespejamas pieturzimes
            i++;
            getWordsFromLine(word, i, memory);
            break;
        }
        else {
            newWord += word[i];
        }
    }
    memory.push_back(newWord);
}

int main() {
    int ok;
    do
    {
    ifstream input;
    ofstream output;
    string word, line;
    char startOfWord;
    char symbol;

    cout << "I wanna find words starting with symbol: \n";

    cin >> startOfWord;

    int lineNumber = 0;

    input.open("f.txt");
    output.open("f1.txt");

    while (getline(input, line)) {
        ++lineNumber;   //skaita rindas
        istringstream iss(line);
        while (iss >> word) {
            if (word.length() > 40) {
                continue;
            }
            vector<string> words;
            getWordsFromLine(word, 0, words);
            for (int i = 0; i < words.size(); i++) {
                if (words[i][0] == startOfWord) { //atrod vardus ar sakuma simbolu
                    output << words[i] << ' ' << lineNumber << '\n';
                }
            }
        }
    }

    input.close();
    output.close();
    cout<<"Vai turpinat (1) vai beigt (0)?"<<endl;
    cin>>ok;
    } while (ok==1);
    return 0;
}