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

using namespace std;

int main() {

    ifstream inFile;
    inFile.open("words.txt");

    if (inFile.fail()) {
        cerr << "Error Opening File " << endl;
        exit(1);
    }
    string item;

    int count = 0;

    while (!inFile.eof()) {
        inFile >> item;
        count++;
    }
    cout << count << " Word identified!" << endl;

    //cout << "All Words" << words << endl;

}

I've looked up multiple guides on youtube and I've still got no answer even after copying people's code. Right now I've tried to make a code we're in Visual studio I have a resource file called words.txt and even though I'm trying to input the words into my code It's still not responding with anything and I have a count to identify how many words there is and there clearly isn't 1.

  • Probably not what you're fighting with, but watch out for [`while (!inFile.eof())`](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 May 09 '22 at 19:56
  • Look at `std::set` for a quick way to filter out duplicates. – user4581301 May 09 '22 at 19:57
  • Side note: Internet tutorials are often sketchy and unless you've developed a good set of BS filters, its often hard to tell a bad Internet tutorial from a good one. The Internet isn't curated in any meaningful way, so there is no easy measure of quality. On the other hand, it's the curation that tends to get Stack Overflow its reputation as being unfriendly. People hate it when they get called on their mistakes. – user4581301 May 09 '22 at 20:02
  • What you have should almost work. When you read in a word, also print it out so you can get a better idea of what's really going on. Another good tool is a debugger. With a debugger you can run the program at your speed and see that the program does as it does it. When you see it doing something you didn't expect, you just found a bug. – user4581301 May 09 '22 at 20:04
  • I appreciate it and I've asked for help on this from a different source yet, I'm not getting the complete understanding needed in order to fulfill this task. – John Richardson May 09 '22 at 20:08
  • If you `while (inFile >> item) { count++; }` as suggested by the link in the first comment, The only other change I would make is to swap `if (inFile.fail())` for `if (inFile.is_open())`, and that's just for reasons of clarity. That'll get the count of everything sorted out. For the unique count, add items as you find them to a `std::set` and then call the `std::set::size` method to get the count of items in the set. – user4581301 May 09 '22 at 20:12
  • Do you think you could help me find a working code that allows me to pull out words from a note.txt and display them in the compiler? – John Richardson May 09 '22 at 21:26
  • I'm assuming you're mis-using the term compiler. Here's how to do it at runtime: `while (inFile >> item) { cout << item << endl; }` – user4581301 May 09 '22 at 21:27

0 Answers0