My current program seems to be working fine most of the time, but every once in a while it will crash randomly. It's giving no error and when it does work, it works perfectly fine, so I'm not sure what's wrong
(Before there is laughing at my noobness, I'm quite new to C++ and this is just a mid-book project so there will be easier ways to do what I'm doing, I just don't know them yet :P)
I'm trying to make a hangman game, but as a personal challenge I'm adding in a reversed mode where the computer tries to intelligently guess your word. This is the part I'm having trouble with :P
The following code is where I'm having the trouble. I just isolated the function so all the useless stuff is out of the way, but still shows my problem :P
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <vector>
using namespace std;
int main()
{
int word = 100; //this is usually a global variable user defined (length of word) to determine how many vowels the computer should guess changed to local and value of 100 for the purpose of finding the issue
int count = 0;
int numberOfVowels;
srand(time(0));
int AI;
int randomNumber = 5;
string randomLetter;
int response;
numberOfVowels = word/3;
string computerUsed = "";
do
{
AI = (rand() % 4);
// Random chance to guess random letter
if (AI == 1)
{
do
{
do
{
randomNumber = rand() % 26 + 1;
} while ((randomNumber == 1) || (randomNumber == 5) || (randomNumber == 9) || (randomNumber == 15) || (randomNumber == 21));
randomLetter = (char)((randomNumber - 1) + 65);
cout << "Hmm... I'll make a lucky guess... How about " << randomLetter << "... Is that one of the letters?";
cout << "\n1. Yes\n2. No" << endl << endl;
//cin >> response;
} while (computerUsed.find(randomLetter) != string::npos);
//Otherwise guess standard vowel
}
else
{
vector<string> randomVowel;
randomVowel.push_back("A");
randomVowel.push_back("E");
randomVowel.push_back("I");
randomVowel.push_back("O");
randomVowel.push_back("U");
do
{
randomNumber = rand() % 5 + 1;
randomLetter = randomVowel[randomNumber];
} while (computerUsed.find(randomLetter) != string::npos);
cout << "I think I'll play it safe and guess " << randomLetter << "... Is that one of the letters?";
cout << "\n1. Yes\n2. No" << endl << endl;
}
++count;
computerUsed += randomLetter;
cin >> response;
} while (count < (numberOfVowels + 1));
system("Pause");
return 0;
}
|
If you want to criticise feel free but I don't want to go too far off what I know, because I want to use the program as part of an application portfolio for a University, and don't want to hand in something that isn't mine :P No doubt that there are easier ways to what I'm doing though
Thanks in advance :P