how many times each word was seen

Hi,
Im trying to write a program that I put 2 input files with some words in each one ,convert them into the array, then my program should decide how many times each words used in those 2 files.output should be like this:
Output:
am 2
because 2
don’t 1
happy 2
i 4
sing 2
I just could write this much and i m not sure even this is correct or not!

#include <iostream>
#include<string>
#include<fstream>

using namespace std;

int findTarget(string arr[],int size, string word)
{
for (int i = 0; i < size; i++) {
if (arr[i] == word) {
return i;
}
}

return -1;
}




int main()
{
string word;
int count = 0;
string words[100];
string txt[100];

ifstream fin;
string target = "am";
fin.open("words.txt");

while (fin >> word) {
words[count] = word;
count++;
}

int position = findTarget(words, count,target);

cout << position << endl;






system("pause");
return 0;

}
Hi,
my program should decide how many times each words used in those 2 files ... i m not sure even this is correct or not!
1
2
3
4
5
6
    int count = 0;
    std::string word;
    while (fin >> word) {
        words[count] = word;
        count++;
    }


I don’t think the logic is correct.
What that loop does, is storing all words into an array. That represents a single piece of information, while you need at least two pieces of information, i.e. the word and its frequency.

Since you use std:.strings, perhaps you want to use a std::map to achieve that purpose. If you don’t feel confident with std::maps, you could use two std::vectors (or even two C-style arrays) which work together to store each word and its frequency at the same index.

For example, you could add another loop inside the above loop, which compare the latest read word with those already stored inside the array. If the comparison is positive, instead of adding that word, it increases its frequency.

Once you’ve correctly read the data, you can proceed to the next step, their output.
@eniozat Thanx for ur respond, But the thing is we havent reach the vectors section and have no idea how that works, thats why i didnt use it
Last edited on
Enoizat wrote:
(or even two C-style arrays)
aliklz92 wrote:
But the thing is we havent reach the vectors

Do try with C-style arrays, then: you have already used them ;-)
Im so sorry, i m so confused right now:), could u tell me how i can combine these 2 arrays with togather?!
I haven’t written the code, but it could be something like this:
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
#include <fstream>
#include <iostream>
#include <string>


int main()
{
    std::string words[100];
    int words_freq[100];
    // initialise all 'ints' inside 'words_freq' to 0

    std::ifstream fin("words.txt");
    // check if 'fin' has been correctly opened

    std::string word;
    for (int count = 0; fin >> word; ++count) {
        if(count) {
            // create a variable to store a "word already found" state
            // initialise the above variable to false
            // start a loop with incresaing counter up to 99 {
                // check if "words[counter]" is equal to "word" - if so {
                    // "words_freq[counter]" should be increased by one
                    // set the state "word already found"
                    // skip the rest of the loop
                // }
            // } end loop
        }
        if( ! "word already found" ) {
            words[count] = word;
        }
    }
    return 0;
}

Topic archived. No new replies allowed.