Floating Error

So I have been coding this problem and I believe I found the error in the program but I cannot resolve it or how to go about changing it. So I will post the problem and my code. I believe it is the srand that is giving me the error and inside the pick_words() return randNum[choices].
Thanks, problem source is case 3.

Also if anyone can help me with how to go about for void reset on case 4 that would be awesome thanks.

1
2
3
4
5
Website for the assignment it under Milestone 3 
http://codersindex.com/cpp-challenge-29/

Code is to big so I used C++ Shell
cpp.sh/2jxgh

Line 28: delete choices; should be delete[] choices;

Lines 44-46: It's easier to use while (infile >> word) ...
Lines 65-67: same comment.

Line 79: C++ arrays are indexed starting at 0, so this should be randNum = rand() % count; But more important, what if count is zero? In case 3, you pick random numbers from 0-45 and ask the user to pick a number between them. When I ran the program, I picked 37. So the program tried to pick a random word with 37 letters. Obviously there aren't any words with 37 letters, so count is zero and the program crashes because it tries to divide by zero.

Line 91: Since show_menu() doesn't return a value and you don't expect it to, declare it as void, not int.

Line 113: same thing with program()

Lines 285-288 are all wrong.. You input guess, and then do nothing with it, instead mucking around with the uninitialized guess_word array. The code from there down should be:
1
2
3
4
5
6
7
8
9
10
11
12
                    for (char & ch : guess) {  // range based for loop
                        if (isascii(ch)) {
                            ch = tolower(ch);
                        }
                    }

                    if (guess == "yes") {
                        cout << "ANSWER: " << char_word << endl << "GAME OVER\n\
";
                        return 0;
                    }
                    if (guess == "no") {



Also if anyone can help me with how to go about for void reset on case 4 that would be awesome thanks.

There's no reason to store the number in a file. Just put it in a local variable in main() and read the variable in case 4.

When I make these changes, the program seems to work pretty well.

Topic archived. No new replies allowed.