searching in Arrays

I am trying to write a program that splits a poem into separate words, loads them into an array(without making duplicate words), counts how many times a word is used in the poem and on what lines, and prints out a statements reading off the 3 things. I have an array that is full of the words but i cannot figure out how to search in to see if a word is used already so if so it is skipped. And i cannot figure out how to write an array to put all the times a word is used in the poem.

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

int n=0;
string maxwords [50];
int main(int argc, char* argr[])
{
ifstream infile;
infile.open("words.txt");
string aline;
while (getline(infile,aline))
{
istringstream theline(aline);
string aword;
while(theline>>aword)
{
maxwords[n] =(aword);
n=n+1;
}
}
cout<<maxwords[0]<<endl;
cout<<maxwords[11]<<endl;
return 0;

}

Any suggestions?
closed account (SECMoG1T)
You can make a struct that will take an string a count of it's occurences and a vector to store the lines in wich it occurs , then load them into a vector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 struct oneword
  {
     string word="";   //will store the read word
     int count=0;       // store the number of occurences
     vector<int> lines; /// stores the lines in which the word occurs
  };
 
  /// each word read from the file is pluged into a object then into a vector,  if it occurs again you'll just 
  /// increment its count 

 vector<oneword> analysis;  /// vector will hold oneword objects and iit's details
 infile>> aline; 
//check if aline is present in the vectors , if so increment it's count else create a oneword object then set 
//  it's variable word to aline and increment it count , add the lune of occurence then add it to the vector 
Last edited on
Topic archived. No new replies allowed.