1

Basically what the title says. I'm trying to write code that will take in a random word from a file called "words.txt" and output it. I run it and keep getting the error "Floating Point Exception (Core Dumped)".

Here's the code:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>

using namespace std;

int main ()
{
  vector<string> word;
  fstream file;
  file.open("words.txt");
  cout << word[rand()% word.size()]<<endl;
 return 0;
}

And here's "words.txt"

duck
goose
red
green
phone
cool
beans

Thanks guys!

Anthony O
  • 25
  • 4
  • 2
    What do you think `word.size()` would be in this case? And btw. you don't actually read _anything_ here. Did you remove that part of your code from the question? – Lukas-T Oct 30 '20 at 17:02
  • because `std::vector word` has size 0 in `cout << word[rand()% word.size()]< – Harry Oct 30 '20 at 17:03
  • 1
    first you need to read the file) –  Oct 30 '20 at 17:03
  • @churill what do you mean? – Anthony O Oct 30 '20 at 17:05
  • 1
    The problem is you never read the file so the word vector is empty. You can't access a random index of an empty vector. The floating point error is division by 0. Which although is an integer operation shows up as a floating point execption in some OSs. – drescherjm Oct 30 '20 at 17:05
  • @AnthonyO Let me ask differently: Where do you actually read the file? You only open it. `word` is never modified in the code shown. What did you expect to happen? – Lukas-T Oct 30 '20 at 17:06

2 Answers2

3

You just opened the file and didn't read that. word has no elements, so word[rand()% word.size()] is dividing something by zero. Dividing by zero is not allowed.

Also you should check if the opening of file succeeded and something is actually read.

Try this:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>

using namespace std;

int main ()
{
  vector<string> word;
  fstream file;
  file.open("words.txt");
  if (!file) // check if file is opened successfully
  {
    cerr << "file open failed\n";
    return 1;
  }
  for (string s; file >> s; ) word.push_back(s); // read things
  if (word.empty()) // check if something is read
  {
    cerr << "nothing is read\n";
    return 1;
  }
  cout << word[rand()% word.size()]<<endl;
  return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • @AnthonyO Fixed code will [work](https://wandbox.org/permlink/piEQWlsy8kJmimLK). Did you compile the code? Also check if the file exists. – MikeCAT Oct 30 '20 at 17:08
  • Oh my god I'm so stupid. I named the file "words.txt" so it was actually "words.txt.txt" – Anthony O Oct 30 '20 at 17:10
  • 2
    This is a common mistake on MS Windows because the OS file explorer by default hides file extensions for known types. You may want to change your explorer settings to turn this annoying feature off. – drescherjm Oct 30 '20 at 17:12
  • 1
    Side note: Knowing you're stupid is the first step to improvement and accomplishment. Knowing you're smart... That rarely ends well. – user4581301 Oct 30 '20 at 17:14
1

You just need to save the list of words in the txt file as a vector after reading it.

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>

using namespace std;

int main ()
{
    vector<string> words;
    ifstream file("words.txt");
    string line;
    while (getline(file, line)){
        words.push_back(line); // adds each line of words into vector
    }
cout << words[rand() % words.size()] << endl;
return 0;
}
adatest
  • 11
  • 2