no output

so i wrote this code that basically simulates the playing of craps.
the code worked fine but i have to write it using classes. Since ive never programed using classes i dont know what im doing wrong. the issue im having is that there is no output showing. I get no compiler errors or debugging errors.
Any help would be greatly appreciated. Below i have included all my classes and header files.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//main.cpp

#include <iostream>
#include <ctime>    // for time()
#include <cstdlib>
#include "roll.h"
#include "game.h"
using namespace std;


int main ()
{
	game mygame;
	mygame.play_game(0);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//roll.h
#ifndef ROLL_H
#define ROLL_H

class roll
{
public:
	roll(int, int);
	roll();
	int die1;
	int die2;
	int toss;
	~roll();

};

#endif // ROLL_H


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//roll.cpp
#include "roll.h"
#include "iostream"
#include <ctime> 
#include <cstdlib>
using namespace std;


roll::roll()
{
	
}
roll::roll(int, int)
{
	die1 =(rand()%6) + 1;
	die2 =(rand()%6) + 1;
	toss =(die1 + die2);
	roll hand;
}

roll::~roll()
{
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//game.h
#ifndef GAME_H
#define GAME_H
#include "roll.h"

class game
{
public:
	game();
	int wallet;
	int wager;
	int point;
	double win;
	double loss;
	void play_game(int num);
	roll hand;
	~game();
};

#endif // GAME_H


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
//game.cpp
#include "game.h"
#include "roll.h"
#include "iostream"
using namespace std;

game::game()
{
	roll hand;
	wallet = 10000;
	wager = 1;
	win = 0;
}

void game::play_game(int)
{
	for(int i = 0; i< 10000; i++)
	{
		if(hand.toss == 7 || hand.toss == 11)
		{
			win++;
			cout << win;
		}
	}
}

game::~game()
{
}


Try making the following changes to your roll.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
roll::roll()
{
	die1 =(rand()%6) + 1;
	die2 =(rand()%6) + 1;
	toss =(die1 + die2);
}

roll::roll(int, int)
{
	die1 =(rand()%6) + 1;
	die2 =(rand()%6) + 1;
	toss =(die1 + die2);
	//roll hand;
}

its suppose to output the amount of wins but its not outputting anything even with the changes
just says "press any key to continue"
That is because despite running the loop for 10000 times, the value of hand.toss does not change. The constructor gets called only once.

Try to output the value of hand.toss in your function and you can verify the above.
Last edited on
ah i see, it keeps outputting 12, but its suppose to be two random numbers every time it runs the loop. any ideas where i went wrong?
Insert a public member function in your roll class:

1
2
3
4
5
6
void roll::throw_dice()
{
	die1 =(rand()%6) + 1;
	die2 =(rand()%6) + 1;
	toss =(die1 + die2);
}


In your play_game, keep calling this function so that the value of hand.toss is changed.

1
2
3
4
5
6
7
8
9
10
11
12
void game::play_game(int)
{
	for(int i = 0; i< 10000; i++)
	{
                              hand.throw_dice();
		if(hand.toss == 7 || hand.toss == 11)
		{
			win++;
			cout << win;
		}
	}
}


EDIT: You may also want to first randomize the system by providing a seed to srand() so that you get a different output each time the program is run.
Last edited on
sorry for all the questions but when i do that, im sure im doing it wrong

i get two errors

1.error: ISO C++ forbids declaration of 'throw_dice' with no type [-fpermissive]

2. error: no 'int roll::throw_dice()' member function declared in class 'roll'

i had srand in my old code but i dont know how to implement it with classes

thanks for taking the time to help me.
Last edited on
Show the exact modified version of your 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
//roll.cpp
#include "roll.h"
#include "iostream"
#include <ctime> 
#include <cstdlib>
using namespace std;


roll::roll()
{
	
}
roll::roll(int, int)
{
	die1 =(rand()%6) + 1;
	die2 =(rand()%6) + 1;
	toss =(die1 + die2);
	roll hand;
}

int roll::throw_dice(int)
{
	die1 =(rand()%6) + 1;
	die2 =(rand()%6) + 1;
	toss =(die1 + die2);
}


roll::~roll()
{
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//roll.h
#ifndef ROLL_H
#define ROLL_H

class roll
{
public:
	roll(int, int);
	roll();
	int die1;
	int die2;
	int toss;
	int throw_dice;
	~roll();

};

#endif // ROLL_H


Also would i put the srand in the game.h file or the roll.h file?
Last edited on
The return type should be void for throw_dice () function in header and cpp files.
ah now it seems to output.
however....
its giving me a very large negative number, and the same number
i think it might be because i dont have my srand(time(0)) set up in my classes, any hints on where it would go?

again thank you for taking the time to help me.
Last edited on
You need to mention the input variable name in your definition for playgame function.
the output is like -22000 which is far too high, my old code gave me 20% chance of winning. ths code ouputs like -200% of winning

im sure its something wrong with the logic of the code but since ive never used classes before im lost.

would it be helpful if i posted my old code that wasnt written with classes?
Last edited on
Please post your entire modified code again.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//main.cpp
#include <iostream>
#include <ctime>    // for time()
#include <cstdlib>
#include "roll.h"
#include "game.h"
using namespace std;


int main ()
{
	game mygame;
	mygame.play_game(0);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//roll.h
#ifndef ROLL_H
#define ROLL_H

class roll
{
public:
	roll(int, int);
	roll();
	int die1;
	int die2;
	int toss;
	void throw_dice();
	int srand();
	~roll();
};


#endif // ROLL_H 



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
//roll.cpp
#include "roll.h"
#include "iostream"
#include <ctime> 
#include <cstdlib>
using namespace std;


roll::roll()
{
	
}
roll::roll(int, int)
{
	die1 =(rand()%6) + 1;
	die2 =(rand()%6) + 1;
	toss =(die1 + die2);
	roll hand;
}

void roll::throw_dice()
{
	die1 =(rand()%6) + 1;
	die2 =(rand()%6) + 1;
	toss =(die1 + die2);
}


roll::~roll()
{
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//game.h
#ifndef GAME_H
#define GAME_H
#include "roll.h"
#include <ctime>

class game
{
public:
	game();
	int wallet;
	int wager;
	int point;
	double win;
	double loss;
	void play_game(int num);
	roll hand;
	~game();
};

#endif // GAME_H 


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
//game.cpp
#include "game.h"
#include "roll.h"
#include <ctime>    // for time()
#include <cstdlib>

#include "iostream"
using namespace std;

game::game()
{
	srand(time(0));
	roll hand;
	wallet = 10000;
	wager = 1;
	win = 0;
}

void game::play_game(int)
{
	srand(time(0));
	cout << hand.toss;
	for(int i = 0; i< 10000; i++)
	{
		hand.throw_dice();
		if(hand.toss == 7 || hand.toss == 11)
		{
			win++;
		}
	}
	cout << (win/10000) << endl;
}


game::~game()
{
}


I did get srand to work.
Change
void game::play_game(int)
to
void game::play_game()

and delete the function
roll::roll(int, int)
nevermind the issue was i was outputing hand.toss silly mistake

thanks for all your help.
Last edited on
You're welcome!
Topic archived. No new replies allowed.