I'm having trouble with an assignment, and I searched all over stack and google but can't figure out the issues.
Right now, the only code I have so far is to ask user to put in a sentence and then breaks the sentence into different strings. And I also need to compare it to a text file which I halfway got.
First problem: My method of splitting up the words into different strings only works sometimes. For example when I write "history people" it tells me theres a segmentation fault, but if I type in "history people " (with the space at the end), it works fine. I'm confused what the problem is.
The 2nd problem is that I can't figure out how to compare my string to the text file line by line, it seems to store the whole file into my string variable "text".
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int noun(string n)
{
    ifstream inputfile;
    bool found = false; // set boolean value to false first
    string text;
    inputfile.open("nouns"); // open text file
    if (!inputfile.is_open()) 
    { // checks to make sure file is open
        cout << "Error" << endl;
    }
    while (getline(inputfile,text,' ')) 
    {
        if (n == text)
        {
            found = true;
            cout << text;
        } 
    }
    inputfile.close(); // close the file
    return found; // return true or false
}
int main ()
{
    string sent;
    string word1, word2, word3, word4; // strings to parse the string 'sent'
    cout << "Please enter a sentence" << endl;
    getline (cin, sent);
    int w1, w2, w3, w4 = 0; // positions in relation to the string
    while (sent[w1] != ' ')
    { // while loop to store first word
        word1 += sent[w1];
        w1++;
    }
    w2 = w1 + 1;
    while (sent[w2] != ' ')
    { // while loop to store second word
        word2 += sent[w2];
        w2++;
    }
    w3 = w2 + 1;
    while (sent[w3] != ' ')
    { // while loop to store 3rd word
        word3 += sent[w3];
        w3++;
    }
    w4 = w3 + 1;
    while (sent[w4] != sent[-1])
    { // while loop to store 4th word
        word4 += sent[w4];
        w4++;
   }
    cout << word1;
    if (sent.empty())
    { // empty set returns "invalid sentence"
        cout << "Invalid Sentence" << endl;
    }
    else if (noun(word1) == true)
    {
        cout << "This is valid according to rule 1" << endl; 
    }
    return 0;
}
 
     
    