I'm trying to write a craps simulator for my homework and I keep getting a NaN or inf error in my output.

Here's my source code

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <time.h>
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int playagain;
float getbalance()
{
float balance;
cout << "How much money can you afford to lose?";
cin >> balance;
while (balance <= 0)
{
cout << "You cannot have an initial balance of 0 or less. Please re-enter your balance.";
cin >> balance;
}
return balance;
}
int rollthedice()
{
int die = 1+rand()%6;
return die;
}
float getbet()
{
float bet;
float balance;
cout << "How much would you like to bet?";
cin >> bet;
while (bet > balance || bet <= 0)
{
if (bet > balance)
{
cout << "You cannot bet more than you are willing to lose. Please re-enter your bet.";
cin >> bet;
}
if(bet <= 0)
{
cout << "You cannot bet 0 or less. Please re-enter your bet.";
cin >> bet;
}
return bet;
}
}
bool playgame()
{
int dietotal;
int point;
int die1, die2;
die1 = rollthedice();
die2 = rollthedice();
dietotal = die1+die2;
cout << "You rolled a " << die1 << " and a " << die2 << ".";
if (dietotal == 7 || dietotal == 11)
{
return 1;
}
else if (dietotal == 2 || dietotal == 3 || dietotal == 12)
{
return 0;
}
else
{
point = dietotal;
cout << "Your point is " << point << ".";
do
{
cout << "Here we go again!" << endl;
die1 = rollthedice();
die2 = rollthedice();
dietotal = die1+die2;
cout << "You rolled a " << die1 << " and a " << die2 << ".";
}
while (dietotal != point && dietotal != 7);
if (dietotal == point)
{
return 1;
}
else
{
return 0;
}
}
}
int main()
{
double bet;
double balance;
bool Gamewon;
float playerbet;
float playerbalance;
srand(time(NULL));
playerbalance = getbalance();
do
{
playerbet = getbet();
Gamewon = playgame();
if (Gamewon == 0)
{
playerbalance = balance - playerbet;
cout << "Sorry, you lost!" << endl << "Your balance is now " << playerbalance << "." << endl;
balance = playerbalance;
}
else
{
playerbalance = balance + playerbet;
cout << "Congratulations, you won!" << endl << "Your balance is now " << playerbalance << "." << endl;
balance = playerbalance;
}

cout << "Would you like to play again?(enter a 1 if you would and a 0 if you would not)";
cin >> playagain;
while (playagain > 1 || playagain < 0)
{
cout << "Invalid input. Please re-enter.";
cin >> playagain;
}
}
while (playagain == 1 && balance > 0);
return 0;
}

Here's my output

How much money can you afford to lose?1000
How much would you like to bet?1000
You rolled a 2 and a 4.Your point is 6.Here we go again!
You rolled a 4 and a 6.Here we go again!
You rolled a 5 and a 3.Here we go again!
You rolled a 4 and a 5.Here we go again!
You rolled a 2 and a 1.Here we go again!
You rolled a 2 and a 2.Here we go again!
You rolled a 6 and a 3.Here we go again!
You rolled a 3 and a 1.Here we go again!
You rolled a 6 and a 5.Here we go again!
You rolled a 2 and a 3.Here we go again!
You rolled a 4 and a 1.Here we go again!
You rolled a 6 and a 2.Here we go again!
You rolled a 5 and a 1.Congratulations, you won!
Your balance is now nan.
Would you like to play again?(enter a 1 if you would and a 0 if you would not)
inb4 op is a dumbass for not seeing a simple solution.
Last edited on
Well, Hi frustratedstudent!

That's your output. Can you give your example expected output that you should have?
Tell us more the problem that currently makes you sad.
And always put your code into <> code format tags (Just click 'Edit' and click it)
Last edited on
Given that I apparently won that particular bet, the output should have said "your balance is now 2000" since I'd bet 1,000 and won. only it's saying "your balance is now nan." I did not anticipate running into such a program-breaking problem at this late stage, and I don't know if i have enough time to rewrite the whole thing.
on line 93: you set playerbalance, but i guess you really mean balance = getbalance();

currently balance remains uninitialized.
I tried it and i'm still getting "your balance is now nan" Aren't there compilers out there that can detect this sort of thing?
Aren't there compilers out there that can detect this sort of thing?


C and C++ have a philosophy of trusting the programmer; it does what you tell it to. Other programming languages adopt different philosophies and hold the programmer's hand a lot more.

As it is, why do you have balance and playerbalance? All you seem to do is swap their values around between each other, sometimes using one and sometimes using the other. How about using just one variable to store the balance?

Anyway, the problem is your getbet function. It can finish without actually hitting the return line, so the value that ends up being returned is just some garbage value.
Last edited on
alright, so what am I doing wrong? I know it's something to do with the balance or the bet somewhere along the line because everything else is working fine.
If you stick cout << "returned bet value is " << playerbet; after line 96 you'll see the problem.

Take a look at your getbet function. What happens if the user enters an acceptable value the very first time? Will the while loop ever run? Will that return ever be reached?

Your compiler probably did spot this, but you didn't ask it to warn you about this sort of thing. Here's the warning that gcc spits out if the warning level is high enough:

In function 'float getbet()':
 warning: control reaches end of non-void function


Last edited on
THANK YOU SO MUCH! You're my favorite internet stranger right now.
Topic archived. No new replies allowed.