help with dice program
need help with the total function. I get funny result when i have to put all the dice throw together
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int total(int roll[])
{
int total;
for(int i=0;i<10;i++)
{
total+= roll[i];
}
return total;
}
int low(int roll[])
{
int low=10;
for(int i=0;i<10;i++)
{
if(roll[i]<low)
{
low=roll[i];
}
}
return low;
}
int high(int roll[])
{
int high=0;
for(int i=0;i<10;i++)
{
if(roll[i]>high)
{
high=roll[i];
}
}
return high;
}
int main()
{
int roll[10];
srand(time(0));
cout << "10 dice rolls: ";
for(int i=0; i<10; i++)
{
roll[i]=rand()%6+1;
cout << roll[i] << " ";
}
cout << "Highest roll: " << high(roll) << endl;
cout << "Lowest roll: " << low(roll)<<endl;
cout << "Total: " <<total(roll)<<endl;
return 0;
}
|
Line 9: total is an uninitialized variable. What do you think it contains? hint: garbage.
found the error. had to set the total to 0
int total = 0;
Topic archived. No new replies allowed.