Hi all,
I'm trying to make a program that breaks an entered sentence into seperate words, then compares the words to a predefined txt file of words. So far I have the following code, but it just doesnt work for some reason. no errors or anything. It asks for the sentence then closes when you hit enter.
Thanks in advance. I know the code isn't exactly neat, so if you have any suggestions they would be much appreciated.

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>  

// for the vector option
using namespace std; 

string sentence, word;  
	vector<string> sV;   

void findVerb (string word)
{
for(int k = 0; k < sV.size(); k++)  
	{    
		string match (sV[k]);
		if (match == sV[k])
		cout << "the verb in this sentence is" << sV[k] << endl;
	}   
}

int main()
{  
	
	cout << "Enter a sentence: ";  
	getline(cin, sentence);   
	// put the sentence into a stream  
	istringstream instr(sentence);   
// the >> operator separates the stream at a whitespace  
	while (instr >> word) 
	{    
	sV.push_back(word);
	}   

	ofstream verbsFile;
verbsFile.open("verbs.txt");

ifstream verbs("verbs.txt");
    if (verbs)
    {
        istream_iterator<string> start(verbs), finish;
        for_each(start, finish, findVerb);
    } else
       cout << "error!" << endl;
  


	cin.sync();  // purge enter 
	cin.get();   // console wait 
	return 0;}
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <vector>
#include <sstream>
#include <algorithm>

int main()
{
    // read verbs from the file into a vector
    std::ifstream verbs_file( "verbs.txt" ) ;
    std::istream_iterator<std::string> begin(verbs_file), end ;
    std::vector<std::string> verb_seq( begin, end ) ;

    // get the sentence from stdin
    std::string sentence ;
    std::getline( std::cin, sentence ) ;

    // read the sentence word by word
    std::istringstream stm(sentence) ;
    std::string word ;
    while( stm >> word )
    {
        // if the word is in the sequence of verbs
        if( std::find( verb_seq.begin(), verb_seq.end(), word ) != verb_seq.end() )
        {
          std::cout << "the verb is: " << word << '\n' ;
          break ;
        }
    }
}

Thank you, could you explain to me why your code is better. for example why are you putting std:: rather than using namespace std? sorry, I still have a lot to learn

EDIT: also, the code that you gave gives me the same issue, I cannot get it to the display the verb phase, it just cuts out the program before that

Hi, sorry, i hate to be that guy, but i'm still having the problem that it closes out randomly on me. Also, if does not close randomly it does not match the verb to the list for reason, even if it is there. Please help.

Ok, sorry for the triple post. I managed to fix that problem. But does anyone have a suggestion on if i wanted to seperate the sentence into a subject and object. Would code something like the following work? But then how would i find the object (or the words after the verb)?

string v;
string word = v; //after it matches the verb

cout << "The subject of this sentence is:" << 'n\';
while (stm >> word, word != v)
{

cout << word << 'n\'; 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.