1

I am making a simple program in C++ that takes sentences/comments from Youtube from a file, these sentences all end with "!!!!". I need to get these sentences into an array list. The rest of the project isn't important, its comparing the individual words in each array and counting the number of negative and positive words from two other arrays which I got working into arrays because they are all separated by white spaces.

I am not sure how to set up the delimiter of "!!!!". I know there are a total of 94 sentences. I am aware that I hard coded the number of positive and negatives in my two working arrays, that is not an issue I am currently concerned about, I can improve this later once it all works.

In summary: I need to make a string array that is 94 strings long that takes input from a file, where every string/sentence ends with

"!!!!"

After that I should be able to figure out how to separate every word in each string and match them to the words in the other 2 arrays and count how many match for each.

Any help would be greatly appreciated, thanks in advance.

Here is the code I got so far.

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

using namespace std;

int main() {
string posArr[2006];
ifstream positive("positive-words.txt");
if (positive.is_open()) {
    for (int i = 0; i < 2006; i++) {
        positive >> posArr[i];
    }
}
positive.close();

string negArr[4783];
ifstream negative("negative-words.txt");

if (negative.is_open()) {
    for (int i = 0; i < 4783; i++) {
        negative >> negArr[i];
    }
}
negative.close();

string commentsArr[94]; 
ifstream comments("youtubecomments-dataset.txt");

//I tried a bunch of different code that didn't work so I erased it.

// cout << posArr[0] << endl;   //tested the array is working
// cout << negArr[0] << endl;
cin.ignore();
cin.get();

return 0;
}

Here are the first few lines from the Negative txt file: 2-faced 2-faces abnormal abolish abominable

Here are the first few lines from the Positive txt file: a+ abound abounds abundance abundant

Here are the first few sentences from the comments text file:

Sync system sucks big time, a total turn off when considering this car.!!!!40mpg ain't that good these days, niether is a 5 speed gearbox!!!!On the Explorer Base the radio is a lot easier and I paired my phone up no problem. Everything worked but the voice command doesn't work.!!!!I have one of these as a rental right now. The electronics aren't really that hard to figure out... But the automatic transmission is HORRIBLE. It's juttery, gringy, and jumpy. And the gears shift all over the place. I'd never buy one of these with an auto tranny. I bet it's great in manual.!!!!you're exactly as tall as i am :)!!!!

It is not structured, sentences start on the same line, and the spacing is bad in some places, but they all end with !!!!

There are 94 in total.

Xerrad Anon
  • 57
  • 1
  • 2
  • 9
  • 2
    Examples of datafile and output would be helpful – yizzlez Sep 18 '14 at 14:47
  • Why `2006` and `4783`? On YouTube you are lucky to get even `1` "positive" word and probably will get max int negative ones. Why not use `std::vector` or some other container? – crashmstr Sep 18 '14 at 14:52
  • So all of this boils down to is "How do I search for a string within a string?" Is that it? – PaulMcKenzie Sep 18 '14 at 15:06
  • @awesomeyi I added the beginning of each text file I am working with so you can see more clearly what I am trying to do. – Xerrad Anon Sep 18 '14 at 15:07
  • @PaulMcKenzie Yes thats' what I need to do basically. With one string being a file. And getting all the strings separated out of the text file into an array. – Xerrad Anon Sep 18 '14 at 15:10
  • @XerradAnon - `With one string being a file` Stop. It doesn't matter where the string comes from. What you need to do is write a small function that takes two strings and returns either true of false depending on if the second string is located at the end of the first string. You're concentrating on file I/O, and your issue has nothing to do with I/O, but string handling/searching. – PaulMcKenzie Sep 18 '14 at 15:12
  • @XerradAnon - See this link. http://stackoverflow.com/questions/20446201/how-to-check-if-string-ends-with-txt – PaulMcKenzie Sep 18 '14 at 15:15
  • @crashmstr Because my text files have 2006 positive words in it and 4783 negative words in it. So i made the array to match. I am not sure how to use std:vector. Does it have commands that can help me in this situation? – Xerrad Anon Sep 18 '14 at 15:16
  • @XerradAnon So you wrote code to work exactly with one set of words? Better not add any... With `std::vector` you use `push_back` to add a new element. You can then easily iterate, count length, etc. – crashmstr Sep 18 '14 at 15:20
  • @PaulMcKenzie I think understand what you are saying, make a string equal to !!!! and see if the first string ends with that, but I don't know how that would work. The first string ends with !!!!, that is true, but it has !!!! 93 other times before the end. How would I get it to return true or stop the first time it sees a !!!! Is that second string a delimiter? string delimiter = '!!!!'; If it were to return true the first time it sees it, i'm still unsure how to use that to put that part of the first string into an array, and then continue from the next one. – Xerrad Anon Sep 18 '14 at 15:42
  • @crashmstr Ok I am going to read up on std::vector and try to see if it can do what I need. Thanks for the input. – Xerrad Anon Sep 18 '14 at 15:44
  • @XerradAnon - You first need to write that function that determines if the string ends with "!!!!" before thinking of anything else. Maybe the function returns the position of the "!!!!" at the end of the string, and -1 to say the string doesn't exist. Once you have that information, then you go from there. That's how you incrementally develop a program -- you don't try and accomplish everything in one shot. The link I posted shows how to determine if a string exists at the end of another string. – PaulMcKenzie Sep 18 '14 at 16:08

0 Answers0