Builds right but crashes instantly

I currently trying to make a word jumbler with a betting game within. I typed in the word jumbler part and then added the play loop and finally the betting part. I used visual studios to create the program. I build the code and it had no errors but one warnings saying that : bet cannot be initialized. The problem

I am having is that it builds fine then when I run the program it crashes instantly why?

This is my simple code.


#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 8;
const int MAX_GUESSES = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS] =
{
{"supercalifragilisticexpialdocious", "Famous song title from the Walt Disney movie Mary Poppins."},
{"mightier", "The pen is than the sword."},
{"defiance", "A daring or bold resistance to authority or to any opposing force."},
{"equivalently", "Equal in value, measure, force, effect, and significance."},
{"jumble", "It's what the game is all about."},
{"terminator", "This person used to assassinate but now protects John Connor."},
{"prenatal", "To give birth."},
{"deprecatingly", "To express earnest disapproval of."}
};

string guess;
int money = 50;
int bet;
char another;


do
{
srand(time(0));
int choice = (rand() % NUM_WORDS);
string theWord = WORDS[choice][WORD]; // word to guess
string theHint = WORDS[choice][HINT]; // hint for word

string jumble = theWord; // jumbled version of word
int length = jumble.size();

for (int i=0; i<length; ++i)
{
int index1 = (rand() % length);
int index2 = (rand() % length);
char temp = jumble[index1];
jumble[index1] = jumble[index2];
jumble[index2] = temp;
}
cout << "\t\t\tWelcome to Word Jumbler!\n\n";
cout << "Unscramble the letters to make a word.\n";
cout << "Type 'hint' for a hint.\n";
cout << "Type 'quit' to quit the game.\n\n";
cout << "You only get five tries to guess the word.\n";
cout << "The jumbled word is: " << jumble;

cout << "You currently have $" << money << " on hand.";
cout << "\nPlace your bet and guess the word.";

while(bet < 1 || bet > money);
{
if (bet < 1)
cout << "\nNeed more money cheapstake!";

if (bet > money)
cout << "\nYou don't have enough weakling!";
cout << "\n\nPlace your bet again! : ";
cin >> bet;

cout << "You bet $" << bet << "."; //end betting
}
for(int nIndex = 0; nIndex < MAX_GUESSES; nIndex++)
{
cout << "\n\nGuess " << nIndex+1 << ": ";
cin >> guess;

if (guess == "hint")
{
cout << theHint;
money -= bet; //subtract amount bet placed
}//end if
else if (guess == theWord)
{
cout << "\nThat's it! You guessed it!\n";
money += bet; //substract the amount of bet placed
break;
}//end else if
else
{
cout << "Sorry, that's not it.";
}//end else
}//end for
cout << "\n\nYou have: " << money;
cout << "\n\n\nWould You Like To Play Again? (y/n): ";
cin >> another;
}while(another == 'y' || another == 'Y');

while (money >= 0)

cout << "\nYou hit rock bottom and died. "
<< "Thanks for playing!" << endl;

system("pause");
cout << "Thanks for playing!";

return 0;
}//finished

Last edited on
is it giving an error??? Usually if it crashes like your saying, it is because your are accesing outside of an array

edit: you never initialized the bet variable in your program
Last edited on
so you are saying that

This:

string guess;
int money = 50;
int bet;
char another;

I need to create a variable for bet.

then that means I need to make a variable that able to pick a number between 1 and the current amount of money.
So I could I do this?

int bet; 0< && < money;
Last edited on
Topic archived. No new replies allowed.