Floating Point Exception

I am not a programmer. I took a class a year ago and recently decided to modify a hangman game I made. Instead of making a game, I am trying to input a file with several thousand words and then test them each individually to see how many guesses it takes the c++ program to get the word. As my code is now when I try to run it it runs a floating point exception. I believe this is because of my rand()% (When I comment out index-- I instead get an error saying its trying to erase on an index higher than the highest of the string. But the floating point error is fixed). I don't see why there should be a floating point exception (core dumped). Any help is appreciated. And I have not finished the program, so if anyone sees good improvements or anything I would appreciate that as well.

#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
using namespace std;
int guesscount(string u, int length);


int main()
{

srand(time(0));
string list[109582], guessed[26];
string word, wordout, rando;
int wordlength;

ifstream x;
x.open("c++hangman.txt");
for(int i=0; i<109582; i++)
{
x >> list[i];
cout << list[i];
wordlength=list[i].length();

cout<< " Guesses Taken [" << guesscount(list[i],wordlength) << "]";

}

//word=list[rand()%109582];
//wordlength=word.length();

}

int guesscount(string u, int length)
{
int count=0;
string alpha="abcdefghijklmnopqrstuvwxyz";
int j=0, rando=0, index=26;
while(j<length)
{
rando=rand()%index;

if(alpha[rando]==u[j])
{
j++;
u=u.erase(j,1);
}
alpha=alpha.erase(rando,1);
index--;
count++;
}




return count;
}
The remainder of a division is undefined if the divisor is zero, thus rand() % index makes no sense if index is zero.
Topic archived. No new replies allowed.