using namespace std;
int roll(), addOdd(int odd), odd;
static int num1;
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(odd) << endl;
cout << "The sum of the even numbers is: "<< endl;
} // main
int roll()
{
return (rand() % 6)+1;
}
int addOdd(int odd)
{
if( (roll() % 2) == 1 )
num1+= roll();
return num1;
}
I want this function to return the odd numbers from the roll function above. Specifically i want to use a static variable to store the sum of the odd numbers from the rolls and put them back into the main function
tags for readibility.
[code]
#include<iostream>
usingnamespace std;
int roll(), addOdd(int odd), odd;
staticint num1;
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(odd) << endl;
cout << "The sum of the even numbers is: "<< endl;
} // main
int roll()
{
return (rand() % 6)+1;
}
int addOdd(int odd)
{
if( (roll() % 2) == 1 )
num1+= roll();
return num1;
}
So let me make sure I'm understanding right, you have a function Roll() that creates a random integer from 1-6, you call Roll() 6 times, and then want the sum of all the odd numbers outputted?
Assuming you're trying to do what I typed above, I notice a few issues:
1) You declare an int odd, but never initialize it's value. In fact, odd doesn't seem to be doing anything in your code.
2) Your function int addOdd(int odd) is not necessary, and doesn't do what you want it to do. In that function, you are trying to add the result from a previous int roll() function call, but every time you call roll() it is creating a new random number, which I'm assuming you don't want.
IMO, you should change int roll() to the following:
thats great and i appreciate it but ive already stated that i have to use a static variable. and we just started arrays so i highly doubt that this is what he wants