How can I display 2 calculated varibles from an object of a class using a member function?
Aug 23, 2017 at 2:31pm UTC
Hello,
I have developed a coin flip simulator using classes and member functions. I have been able to use rand() to seed and determine heads and tails. From the head_count and tail_count variables I am trying to compute the % occurence of each. But I am not able to display values. I cannot see where I am going wrong.
Could you identify an error?
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <iomanip>
using namespace std;
class coin
{
private :
int head_count;
int tail_count;
public :
coin(int = 0, int = 0, int = 0);
int max_toss;
void coin_toss(int );
void percentage_algorithm();
void Show_coin();
void set_number_of_tosses();
};
//class implementations section
coin::coin(int Head_Count, int Tail_Count, int Max_Toss)
{
head_count = Head_Count;
tail_count = Tail_Count;
max_toss = Max_Toss;
}
void coin::set_number_of_tosses()
{
int n;
cout << "Set the number of tosses" << endl;
cin >> n;
max_toss = n;
}
void coin::coin_toss(int max_toss)
{
srand(time(NULL));
for (int toss_counter = 0; toss_counter < max_toss; toss_counter++)
{
if ( (rand() % 2) >= 0.5)
{
head_count++;
}
}
tail_count = max_toss - head_count;
}
void coin::percentage_algorithm()
{
double percentage_heads;
double percentage_tails;
if ( max_toss == 0)
{
cout << "No tosses were made " << endl;
}else {
percentage_heads = ((head_count / max_toss) * 100.0);
percentage_tails = ((tail_count / max_toss) * 100.0);
cout << "The percentage of heads was " << setprecision(10)<< percentage_heads << endl;
cout << "The percentage of tails is " << setprecision(10) << percentage_tails<< endl;
}
return ;
}
void coin::Show_coin()
{
cout << "The number of heads is" << head_count << endl;
cout << "The number of tails is " << tail_count << endl;
cout << " The number of tosses was " << max_toss << endl;
cout << " The total number of tosses was " << max_toss << endl;
}
int main()
{
coin a;
a.set_number_of_tosses();
a.coin_toss(a.max_toss);
a.Show_coin();
a.percentage_algorithm();
return 0;
}
Aug 23, 2017 at 2:44pm UTC
It's the problem with integer division on lines 72-73. If you change the lines to
1 2
percentage_heads = double (head_count) / max_toss * 100.0;
percentage_tails = double (tail_count) / max_toss * 100.0;
you get the following output:
Set the number of tosses
20
The number of heads is11
The number of tails is 9
The number of tosses was 20
The total number of tosses was 20
The percentage of heads was 55
The percentage of tails is 45
Aug 28, 2017 at 12:10pm UTC
Hello,
Thanks Thomas! I can see that by doing a double / integer wouldn't help here.
Topic archived. No new replies allowed.