Weird Hanging
Author |
Message |
Clayton
|
Posted: Sat Sep 09, 2006 6:08 pm Post subject: Weird Hanging |
|
|
hey, this is my *first* program written in c++, i am using MinGW for my compiler, this program compiles fine, and runs, however, after you enter your bet, it just hangs, and i dont know why, here is my code:
c++: |
#include <iostream>
#include <string>
int die_roll(int low, int high)
{
srand ((unsigned)time(NULL));
return (rand() % (high - low + 1)) + low;
}
int main()
{
std::string answer, name = "";
while(answer == "no");
{
int money, die1, die2, point, bet = 0;
money = 500;
bet = money + 1;
std::cout << "Welcome to Craps! This is a very basic game of craps." << std::endl << "Follow onscreen instructions and you will be on your way!" << std::endl << "When you roll, if you get a 7, you win your bet, if you roll a 2 or 12 you lose your bet," << std::endl << "anything else will become your point, from then on you must roll your point," << std::endl << "if you roll a 2, 12 or 7 you lose, have fun!" << std::endl;
std::cout << "Please enter your name" << std::endl;
std::cin >> name;
while(bet < money);
{
std::cout << "You have 500 dollars, how much are you going to bet?" << std::endl;
std::cin >> bet;
}
while (die1 != 0);
{
die1 = die_roll(1,6);
}
while (die2 != 0);
{
die2 = die_roll(1,6);
}
std::cout << "You rolled a " << die1 + die2 << std::endl;
if (die1 + die2 == 2 or die1 + die2 == 12){
money = money - bet;
}else if (die1 + die2 == 7){
money = money + bet;
}else{
point = die1 + die2;
std::cout << "Your point is now " << point << ". You must roll that to win." << std::endl;
}
die1 = 0;
die2 = 0;
while (die1 + die2 == point);
{
while (die1 != 0);
{
die1 = die_roll(1,6);
}
while (die2 != 0);
{
die2 = die_roll(1,6);
}
if (die1 + die2 == point){
std::cout << "You rolled a " << die1 + die2 << ", which matches your point, you win!" << std::endl;
money = money + bet;
}else if (die1 + die2 == 2 or die1 + die2 == 7 or die1 + die2 == 12)
{
std::cout << "You rolled a " << die1 + die2 << ", you lose.";
money = money - bet;
}
}
if (money == 0){
std::cout << "You have no money, you cannot play anymore" << std::endl;
answer = "no";
}else{
std::cout << "Would you like to play again?(yes/no)" << std::endl;
std::cin >> answer;
}
}
return 0;
}
|
im sure some of you are having heart attacks right now and thats fine, plz let me know how i can fix this up and make it better |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Sat Sep 09, 2006 6:20 pm Post subject: (No subject) |
|
|
You have a one character error. The outermost while loop in your main function. What happens in the body of that loop? |
|
|
|
|
|
Clayton
|
Posted: Sat Sep 09, 2006 6:53 pm Post subject: (No subject) |
|
|
ok here is my current code so far:
c++: |
#include <iostream>
#include <string>
int die_roll(int low, int high)
{
srand ((unsigned)time(NULL));
return (rand() % (high - low + 1)) + low;
}
int main()
{
std::string answer, name = "";
while(answer != "no")
{
int money, die1, die2, point, bet = 0;
money = 500;
bet = money + 1;
std::cout << "Welcome to Craps! This is a very basic game of craps." << std::endl << "Follow onscreen instructions and you will be on your way!" << std::endl << "When you roll, if you get a 7, you win your bet, if you roll a 2 or 12 you lose your bet," << std::endl << "anything else will become your point, from then on you must roll your point," << std::endl << "if you roll a 2, 12 or 7 you lose, have fun!" << std::endl;
std::cout << "Please enter your name" << std::endl;
std::cin >> name;
while(bet < money);
{
std::cout << "You have 500 dollars, how much are you going to bet?" << std::endl;
std::cin >> bet;
}
//while (die1 > 0)
//{
die1 = die_roll(1,6);
//}
//while (die2 > 0)
//{
die2 = die_roll(1,6);
//}
std::cout << "You rolled a " << die1 + die2 << std::endl;
if (die1 + die2 == 2 or die1 + die2 == 12){
money = money - bet;
}else if (die1 + die2 == 7){
money = money + bet;
}else{
point = die1 + die2;
std::cout << "Your point is now " << point << ". You must roll that to win." << std::endl;
}
die1 = 0;
die2 = 0;
while (die1 + die2 != point)
{
//while (die1 != 0)
//{
die1 = die_roll(1,6);
//}
//while (die2 != 0)
//{
die2 = die_roll(1,6);
//}
if (die1 + die2 == point){
std::cout << "You rolled a " << die1 + die2 << ", which matches your point, you win!" << std::endl;
money = money + bet;
}else if (die1 + die2 == 2 or die1 + die2 == 7 or die1 + die2 == 12)
{
std::cout << "You rolled a " << die1 + die2 << ", you lose.";
money = money - bet;
}
}
if (money == 0){
std::cout << "You have no money, you cannot play anymore" << std::endl;
answer = "no";
}else{
std::cout << "Would you like to play again?(yes/no)" << std::endl;
std::cin >> answer;
}
die1 = 0;
die2 = 0;
}
return 0;
}
|
compile and run it, and you will see that if you roll a point number, you seem to roll it on the very next roll every time (maybe i havent tested this thouroughly enough, but i think i have) |
|
|
|
|
|
bugzpodder
|
Posted: Sun Sep 10, 2006 1:37 pm Post subject: (No subject) |
|
|
here is a hint on how to debug. I wont do it for you.
you should confirm your theory by deleting everything (well comment it out) and just call your die_roll function multiple times. if it returns the same number everytime, you know die_roll function needs to be changed, otherwise something else is wrong. |
|
|
|
|
|
md
|
Posted: Sun Sep 10, 2006 3:40 pm Post subject: (No subject) |
|
|
You should also try to initialize variables before comparing them to something. |
|
|
|
|
|
wtd
|
Posted: Sun Sep 10, 2006 3:56 pm Post subject: (No subject) |
|
|
And if your while loop is going to run at least once...
That's the classic place to use the do-while loop. |
|
|
|
|
|
bugzpodder
|
Posted: Sun Sep 10, 2006 3:58 pm Post subject: (No subject) |
|
|
not sure if its what wtd meant in his first post, but i dont think
while(bet < money); loop gets executed at all. |
|
|
|
|
|
md
|
Posted: Sun Sep 10, 2006 7:29 pm Post subject: (No subject) |
|
|
bugzpodder wrote: not sure if its what wtd meant in his first post, but i dont think
while(bet < money); loop gets executed at all.
It does because money is set to 500 and bet is set to money + 1.
[syntax]int money, die1, die2, point, bet = 0;
money = 500;
bet = money + 1; [/syntax]
Bet is set twice, which isn't very efficient; but it does run.
However because die1 and die2 are not initialized before they are compared in the commented out (and pointless) loops; which could have caused problems. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
bugzpodder
|
Posted: Mon Sep 11, 2006 12:08 pm Post subject: (No subject) |
|
|
so bet<money is evaulated to false and the loop does not run? |
|
|
|
|
|
Clayton
|
Posted: Mon Sep 11, 2006 8:00 pm Post subject: (No subject) |
|
|
ok, i got the game to *run*, however, when you roll a point, you will always roll it again to win the round, i dont know why it does it, but i have no idea how to fix it, or whats causing it, my code so far:
c++: |
#include <iostream>
#include <string>
int die_roll(int low, int high)
{
srand ((unsigned)time(NULL));
return rand() % (high - low + 1) + low;
}
int main()
{
std::string answer, name = "";
int money = 500;
while(answer != "no")
{
int die1, die2, point = 0;
int bet = money + 1;
std::cout << "Welcome to Craps! This is a very basic game of craps." << std::endl << "Follow onscreen instructions and you will be on your way!" << std::endl << "When you roll, if you get a 7, you win your bet, if you roll a 2 or 12 you lose your bet," << std::endl << "anything else will become your point, from then on you must roll your point," << std::endl << "if you roll a 2, 12 or 7 you lose, have fun!" << std::endl;
std::cout << "Please enter your name" << std::endl;
std::cin >> name;
while(bet > money)
{
std::cout << "You have " << money << " dollars, how much are you going to bet?" << std::endl;
std::cin >> bet;
}
die1 = die_roll(1,6);
die2 = die_roll(1,6);
std::cout << "You rolled a " << die1 + die2 << std::endl;
if (die1 + die2 == 2 or die1 + die2 == 12){
money = money - bet;
}else if (die1 + die2 == 7){
money = money + bet;
}else{
point = die1 + die2;
std::cout << "Your point is now " << point << ". You must roll that to win." << std::endl;
}
die1 = 0;
die2 = 0;
while (die1 + die2 != point)
// dice for some reason always stay at the same value when you roll for point :(
{
die1 = die_roll(1,6);
die2 = die_roll(1,6);
if (die1 + die2 == point){
std::cout << "You rolled a " << die1 + die2 << ", which matches your point, you win!" << std::endl;
money = money + bet;
}else if (die1 + die2 == 2 or die1 + die2 == 7 or die1 + die2 == 12)
{
std::cout << "You rolled a " << die1 + die2 << ", you lose.";
money = money - bet;
}else{
std::cout << "You rolled a " << die1 + die2 << std::endl;
}
}
// problem lies somewhere between the comments here ^^
if (money == 0){
std::cout << "You have no money, you cannot play anymore" << std::endl;
answer = "no";
}else{
std::cout << "Would you like to play again?(yes/no)" << std::endl;
std::cin >> answer;
}
die1 = 0;
die2 = 0;
}
return 0;
}
|
|
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Thu Sep 14, 2006 7:28 am Post subject: (No subject) |
|
|
Don't have time to test my theory, but someone already told you: your die_roll() does something it shouldn't. |
|
|
|
|
|
|
|