greetings everyone,

i seem to be having an issue with a function that has a reference n a value to do a loop. the nature of the prog, is to roll a dice (n) times n then count the number of time srand rolls an even num.

for the most part i think it is working--no errors, but inside a function im calling another function n then asking it to loop. And that is where im stuck @.

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

int simDiceThrow(int, int&);
int diceThrow(int&);
bool isEvenSided(int&);

const long MAX=6, MIN=1;

int main ()
{
    int diceA,
        total=0,
        totalOne=0,
        pvalue,
        maxNumber=0;

srand((unsigned)time(NULL));


        cout <<" Dice Counter\n\n";

        maxNumber = 100;

        simDiceThrow(maxNumber, pvalue);[QUOTE]not sure if error is here as the MaxNum should be getting passed to the loop inside the function[/QUOTE]

             if (isEvenSided(pvalue) != false) {total++;}

        maxNumber = 1000;

        simDiceThrow(maxNumber, pvalue);[QUOTE]im wondering how will the if statement run if there is no loop outside??[/QUOTE]

             if (isEvenSided(pvalue) != false) {totalOne++;}

    cout <<"100 x dice thrown " 
         <<" " <<total <<" " <<pvalue;

    cout <<"\n1000 x dice thrown"
         <<" " <<totalOne <<" "  <<pvalue;


  _flushall();
    cin.get();

     return 0;

}
int diceThrow (int &diceA)
{
    return (diceA = (rand() % (MAX - MIN +1))+ MIN);

}

bool isEvenSided (int &pvalue)
{
     return ((pvalue%2)== 0);
}

int simDiceThrow(int maxNumber, int &diceA)
{
    int pvalue;

    for (int roll = 0; roll < maxNumber; roll++) {pvalue = diceThrow(diceA);}
    return pvalue;
}

any suggestions are greatly appreciated.

if (isEvenSided(pvalue) != false) {total++;}

How can you compute the total outside the loop ? you should
pass the total also as a reference. And inside the function you
can make it to zero first and add it if even.

First draw a structure chart. Then you can clearly model this.

alright so after getting some dinner, i decided to redo the whole thing. from the drawing board i came up with the following code, but something is fishie about the count. it runs the loop for the srand and from what i can tell moves the counter but the count is not always right.

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
using namespace std;

int simDiceThrow(int, int&);
int diceThrow();
bool isEvenSided(int&);

int main ()
{
    int total=0,
        totalOne=0,
        maxNumber;

srand((unsigned)time(NULL));
            
    
        cout <<" Dice Counter\n\n";
    
        maxNumber = 3;
        simDiceThrow(maxNumber, total);
        
        cout <<"total even for 100: " <<total <<endl;  
             
        maxNumber = 5;
        simDiceThrow(maxNumber, totalOne);
                  
        cout <<"total even for 1000: " <<totalOne <<endl;
             
            
    cin.get();
    cin.get();
  
     return 0;
         
}
int diceThrow ()
{
     int diceA = (rand()% 6+ 1);
     return diceA; 
}

bool isEvenSided (int &pvalue)
{
     return (pvalue %2 == 0);
}

int simDiceThrow(int maxNumber, int &total)
{
    int pvalue;
    
    for (int roll = 1; roll <= maxNumber; roll++) 
    {
        cout <<setw(5) <<diceThrow() <<endl;
    
    pvalue=diceThrow();
    
    if (isEvenSided(pvalue)) {total++;}
           
    }
}

that produces some like this:

Dice Counter

1
3
2
total even for 3: 1 //this one is correct
6
5
4
4
3
total even for 5: 4 //this one isnt, i am thinking something a zero but dont know where to eliminate such prob

In your simDiceThrow() loop you output the dice values, but then calculate the number of even values by throwing the dice again. So the number of even values has no relation to the numbers previously output. The logic you actually need is to throw the dice n times, remembering each value thrown. Calculate the number of even values, then output the number set and total.

good you have taken some courage .
if you still can't underestand why look at this code and
think why ?

int simDiceThrow(int maxNumber, int &total)
{
    int pvalue;
    
    for (int roll = 1; roll <= maxNumber; roll++) 
    {
        //cout <<setw(5) <<diceThrow() <<endl;
    
    pvalue=diceThrow();
    cout << setw(5) << pvalue << endl ; 
    
    if (isEvenSided(pvalue)) {total++;}
           
    }
}

i now see where the error what and why it was, so here is my understanding of it.

num gets generated it then gets passed over to the bool and gets counted if return is true. my first issue was that the bool was not in the loop, then after that was corrected, the value was not passed over to the bool for count. here is the updated function. works great. thank you guys for pushing.

int simDiceThrow(int maxNumber, int &total)
{
    for (int roll = 1; roll <= maxNumber; roll++) 
    {
          int evalue=diceThrow();
    
          cout <<setw(2) <<evalue;
          if (roll %25 == 0) cout <<endl; //spacing
    
          if (isEvenSided(evalue)) {total++;}
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.