Can someone please tell em why this wont work.


#include<iostream>

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
Posting in
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
 tags for readibility.

[code]
#include<iostream>

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;
}


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?
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int roll()
{
     
     int thisroll = (rand()%6) + 1;
     
     if( thisroll % 2 == 1 )
     {
          num1 += thisroll;
     }
     
     return thisroll;

}


If you do that, you won't need the addOdd function.

Hope that helps.
Last edited on
thats exactly what i want but i have to have a separate function to do that summation so i need the addOdd function
I modified 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
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;


int roll();
void addOdd();
int numbers[5];


int main(){
    int i;
    
    srand(time(0));
    
    addOdd();
}

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

void addOdd(){
    static int totalOddNumbers = 0;
    static int totalEvenNumbers = 0;
    
    for(int i = 1; i <= 5; i++){
        numbers[i] = roll();
        cout << "Your roll is : " << numbers[i] << endl;
        
        if(numbers[i] % 2 == 1){
            cout << numbers[i] << "is odd." << endl;
            totalOddNumbers += 1;
        }
        if(numbers[i] % 2 != 1){
            cout << numbers[i] << "is even." << endl;
            totalEvenNumbers += 1;
        }
    }
    
    cout << "Total odd numbers: " << totalOddNumbers << endl;
    cout << "Total even numbers: " << totalEvenNumbers << endl;
}


This code will print 5 random numbers and determine if the number is odd or even...

also, you need to use srand(time(0)), otherwise it will print out the same numbers as you got before.


*EDIT*

I couldn't do the total odd numbers and even numbers but i'm sure you can figure that out.
Last edited on
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
I edited my code above...hope that helps.
Last edited on
Topic archived. No new replies allowed.