showpoint problems

At the end of my code why is my showpoint and setprecision not working, it does not display the trailing 0's I need.

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
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::setw;
using std::setprecision;
using std::fixed;
using std::showpoint;
int hold, roll, dice, num, num0, num1, num2, num3, num4, num5, num6;
double total;
int main()

{
	srand(time(0));
	
	
	cout<<"Enter the amount to hold"<<endl;
	cin>>hold;
	cout<<""<<endl;
	cout<<"enter how many times to roll"<<endl;
	cin>>roll;
	cout<<""<<endl;
	
//	dice=(rand() % 6);
	
	cout<<"Score"<<"\t"<<"Estimated Probability"<<endl;
	
	for (total=0; total<roll; total++) // loop as many time as roll input equals
	{
		do 
		{
			num+=(rand()%6);
			
			if ((rand()%6)==1) //if the dice roll a 1 end turn start over and move to next turn
				{
					num=0;
					break;
				}
			if (num>=hold) //if the num equals or is greater than hold then start over the count move to next turn
				{
					break;
				}
		}
		
		while (num<hold); // if num is less than hold value continue loop
		
		{
			if (num==0) num0++;
			if (num==hold) num1++;
			if (num==hold+1) num2++;
			if (num==hold+2) num3++;
			if (num==hold+3) num4++;
			if (num==hold+4) num5++;
			if (num==hold+5) num6++;
		}

	}
	
	cout<<"0"<<"\t\t"<<setprecision(6)<<showpoint<<((num0/(num0+num1+num2+num3+num4+num5+num6))<<endl;
	cout<<hold<<"\t\t"<<num1<<endl;
	cout<<hold+1<<"\t\t"<<num2<<endl;
	cout<<hold+2<<"\t\t"<<num3<<endl;
	cout<<hold+3<<"\t\t"<<num4<<endl;
	cout<<hold+4<<"\t\t"<<num5<<endl;
	cout<<hold+5<<"\t\t"<<num6<<endl;
	return 0;
}
Last edited on
At the end of my code why is my showpoint and setprecision not working, it does not display the trailing 0's I need.

You don't have a floating point number. You also have a syntax error there that will keep the code from compiling, so I wonder if this is the actual code you're using. You probably want std::fixed rather than std::showpoint if you want all the output to be the same length.

1
2
3
4
5
6
7
8
9
10
    double num_rolls = roll;

    std::cout << std::fixed;
    cout << "0" << "\t\t" << num0/num_rolls << endl;
    cout << hold << "\t\t" << num1/num_rolls << endl;
    cout << hold + 1 << "\t\t" << num2/num_rolls << endl;
    cout << hold + 2 << "\t\t" << num3/num_rolls << endl;
    cout << hold + 3 << "\t\t" << num4/num_rolls << endl;
    cout << hold + 4 << "\t\t" << num5/num_rolls << endl;
    cout << hold + 5 << "\t\t" << num6/num_rolls << endl;
got it all num needs to be a double
got it all num needs to be a double

No. num0 and crew need not be doubles, and should not be doubles given the things they represent and the way you use them. What needs to be a floating point number is the result of the division and we can achieve that by making one of the numbers a double or float. I did it the way I did because:

cout << "0" << "\t\t" << static_cast<double>(num0)/roll << endl;

is quite a bit uglier.
Topic archived. No new replies allowed.