#include<cmath>?

Ok i found his code whick i know is incorreet but... What is the #include<cmath> do in the following program or is it pointless.
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
103
104
105
106
107
108
109
110
111
112
113
114
115
# include <iostream>
# include <cmath>
# include <iomanip>
# include <cstdlib>
using namespace std;
int main()
{
int die1;
int die2;
int dice;
int bet;
int point;
int money = 100;
char roll;

do
{
cout << "You currently have $" << money << " on hand.";

cout << "\nPlace your bet and roll the dice (minimum $1): ";
cin >> bet;

while (bet < 1 || bet > money)
{
if (bet < 1)
cout << "\nDont be so cheap put some money on the table!";
if (bet > money)
cout << "\nYou don't have that much!";
cout << "\n\nPlace your bet and roll the dice (minimum $1): ";
cin >> bet;
}

cout << "You bet $" << bet << ".";


cin.get(roll);

die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
dice = die1 + die2;

cout << "\nYou rolled a " << die1 << " and a " << die2
<< " for a total of " << dice << "."<<endl;

if (dice == 7 )
{
cout << "Youve won";
money += bet;
}


if (dice == 11)
{
cout << "Youve lost";
money -= bet;
}

else
{
do
{
cout << "You currently have $" << money << " on hand."<<endl; //copied the betting loop you had
cout << "\nPlace your bet and roll the dice (minimum $1): ";
cin >> bet;
while (bet < 1 || bet > money)
{
if (bet < 1)
cout << "\nC'mon, take a chance!";
if (bet > money)
cout << "\nYou don't have that much!";
cout << "\n\nPlace your bet (minimum $1): ";
cin >> bet;
}
cout << "You bet $" << bet << "."; //end betting loop

cin.get(roll);

die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
dice = die1 + die2;

cout << "\nYou rolled a " << die1
<< " and a " << die2
<< " for a total of " << dice << ".";

if (dice == 7)
{
cout << "\nYOU WON!!!! lets raise the Stakes"<<endl;
money += bet;
}

if (dice == 11)
{
cout << "\nYOU LOSE its ok you can WIN it back"<<endl;
money -= bet;
}

else
{
cout << "\nCooooommmee on 7. "<<endl;

}

} while (dice != point || dice != 7);
}
} while (money > 0);

cout << "/nLooks like you ran out of money. "
<< "Thanks for playing!" << endl;




system("pause");
}
<cmath> defines the functions listed here:
http://cplusplus.com/reference/clibrary/cmath/

I don't think it's needed in this file, because I can't find any reference to either of these functions in the code.
Try removing it and see if the program still compiles. (It will).
Thats what i thought. This wasnt my program if you look a couple of posts down(like 25) it was for a craps gam.
>> Try removing it and see if the program still compiles. (It will).

Yes, but in general, it's better to examine the code manually and see if something defined in the header file is used. It might be that <cmath> was really used, but happened to be included in another header file. In that case, it would still compile if <cmath> was removed.

However, it should still be included, because if that other header file was removed, or even worse if <cmath> was removed from that other header file, this file wouldn't compile anymore. It could be hard to figure out why. Even worse, it could be that the two files were maintained by two different programmers, and someone could end up with errors caused by files they can't edit.
Yeah, headers like <cmath> are usually protected from being #included more than once. For example, gcc 3.4.5 has the following for the MinGW version of <cmath>:
1
2
3
4
5
6
#ifndef _GLIBCXX_CMATH
#define _GLIBCXX_CMATH 1

.... (all of the necessary code is here)

#endif 


Basically, if _GLIBCXX_CMATH isn't already defined, the contents of the header can be #included. However, if it IS defined, then nothing happens, and it simply skips over the header file. ^_^
Last edited on
Topic archived. No new replies allowed.