simulating a die roll

Pages: 12
#include<iostream>

using namespace std;
int roll(), rnum;
void main()
{
int i;
for (i = 1; i <=5; i++)

cout << "Your roll is : " << roll() << endl;

srand(1811);

} // main

int roll()
{

static int roll = 0;
int rnum;

rnum = rand();
roll = rnum % 6+1;
return roll;

}
Right now im tryign to create the roll of a die and then take the sum of the odd and the sum of the even rolls and display them. All i have at the moment is the roll of the die and im only geting values of 5 or 6 back.
any reason why this isnt working properly?
Are you new to C++ Programming? The question seems irrelevant, but I assure you, it isn't.
Last edited on
i am fairly new yes i just started using it at the beginning of this semester
Alright then, so here's what it should look like. Also you should stay away from using namespaces, they're not good practice.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>

int roll();

void main()
{
    srand(1811);

    for (int i = 1; i <= 5; ++i)
    {
        std::cout << "Your roll is : " << roll() << std::endl;
    }
}

int roll()
{
    return rand() % (6 + 1);
}


Another thing you seem to be missing, is the problem. I don't see what you're problem is with this code. could you explain?
Last edited on
just the out put isnt coming out i wanted it to it was only returning 5 or 6 but that was with any random number seed. and why isnt it good practice to use namespaces?

would an if else statement work well for taking the sum of the even/odd rolls? i have to create a function to do both and i am curious if that would be the best/easiest path
You can still use it for small applications like you're doing here, but you should get used to it. Answer why is here...
http://stackoverflow.com/a/1452738

So be more specific, what is it exactly you're trying to accomplish?
ok thanks. and i am trying to simulate the roll of a die using a function roll(). Then using two other functions i need to take the even numbers sum them up and display them in the main and take the sum of the odd rolls and display them in the main also.
int addOdd(int odd)
{
static int odd;

if(roll()%2)

odd += roll();

return odd;

}

I am thinking that the odd function should look something like this but i am getting an error message saying redefinition of formal parameter
You can't declare a variable static inside a function, or method is why, because a variable inside a function or method can only be used inside of it.
Last edited on
You can't declare a variable static inside a function, or method is why


Have to correct you here.

You absolutely can make variables static inside of functions.

His problem is that he has two different variables named 'odd':

1
2
3
int addOdd(int odd)  // <- odd declared here as a parameter
{
static int odd;  // <- odd declared here AGAIN as a local 


The solution is to rename one of them.
Really? Forgive me for handing out false information then. Could you possible give the use for having static variables inside a function or method?
A static variable retains it's value from function call to function call.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// http://ideone.com/cLC4Os
#include <iostream>

int f()
{
    static int n = 0;  // initialization is done only the first time the function is called

    return n++;
}

int main()
{
    for (unsigned i = 0; i < 20; ++i)
        std::cout << f() << '\n';
}


1
2
3
4
5
6
7
8
9
10
int addOdd(int  odd)
{
	static int num1;

	if(roll()%2)
		num1 += roll();

return num1;

}


Thanks a lot. Now im trying to take the sum of the odd numbers of the rolls and return them to the main function, But all im getting is 0 when i try and display it
also when i set my srand to srand(1181) i get a value of zero for one of my rolls
What do your entire code look like right now?
I fixed the problem with the roll. and here is what i have so far.
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
	#include<iostream>

	using namespace std;
	int roll(), addOdd(int odd);
	void main()
{
	int i;
	
	srand(1181);

	for (i = 1; i <=5; i++)
	{
	cout << "Your roll is : " << roll() << endl;
	}
	cout << "The sum of the odd numbers is: " << addOdd(roll()) << endl;
	cout << "The sum of the even numbers is: "<< endl;

} // main

int roll()
{
return (rand() % 6)+1;
}

int addOdd(int  odd)
{
	static int num1 = 0;

	if(roll()%2 )
		num1 += roll();

return num1;

}
tjlandry34 wrote:
I fixed the problem with the roll. and here is what i have so far.


Try this:

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
#include<iostream>

using namespace std;

int roll();

void main() {
	int i;
	int odd=0, even=0;

	srand(1181);

	for(i = 1; i <=5; i++) {
		int r = roll();
		cout << "Your roll is : " << r << endl;
		if(r % 2) 
			even++;
		else
			odd++;
	}
	cout << "The sum of the odd numbers is: " << odd << endl;
	cout << "The sum of the even numbers is: "<< even << endl;

}

int roll() {
	return (rand() % 6)+1;
}
Last edited on
Okay but you arent taking the sum of the odd and even numbers you are just simply telling how many there are out of the five rolls
tjlandry34 wrote:
Okay but you arent taking the sum of the odd and even numbers you are just simply telling how many there are out of the five rolls


Oh, my bad. I thought you wanted how many times you rolled an odd number and vice versa.

If you want the total of each, try this:

1
2
3
4
if(r % 2) 
			even += r;
		else
			odd += r;


instead of

1
2
3
4
if(r % 2) 
			even++;
		else
			odd++;
Pages: 12