# Thread: Help with simple division

1. ## Help with simple division

Hello. I'm just beginning learning C++ and I'm having a problem with simple division. First, here's the code that I'm having a problem with. . .

Code:
```#include<iostream>

int main()
{
double number = 1;
while(1)
{
double eight = .125*number;
double one = 1;
double seven = 7;
double fract = one/seven;
double seventh = fract * number;
double test = 4100 + seventh + eight;
std::cout << number << " " << seventh << " " << eight << " " << test << std::endl;
if (test == number)
break;
number+=1;
}
return 0;
}```
The program works, but I'd like to know how to simply divide one by seven without having to define them as doubles first. For such a small program, it really doesn't make a difference but I want to know why something like the following doesn't work. . .

Code:
`double test = 4100 + ((1/7)*number) + ((1/8)*number);`
Finally, if it's not too much trouble, can someone explain the right way to do the following? I'm pretty sure that this isn't the right way.

Code:
```while(1)
{
//code
}```
Thank you very much.

-Matt

2. You need floating point numbers - ie, 7.0, and not 7.

3. To divide 1 by seven, go (1.0/7) or (1/7.0) or (1.0/7.0).

See,
Code:
`double seven = 7;`
automatically converts 7 to a double, 7.0.

(1/7) will be 0, because you're using whole numbers, not floating point numbers.

One last note: To convert an int to a double, rather than go
Code:
```int i = 4;
double r = (i*1.0)/7.0;```
you can use a cast
Code:
```int i = 4;
double r = (double)i / 7.0;```

4. Code:
```while(1)
{
//code
}```
That code will work. But you need to terminate the loop at some time or another, using a break statement.

5. >> That code will work. But you need to terminate the loop at some time or another, using a break statement.

You missed this:
Code:
```if (test == number)
break;```
Of course comparing floating point numbers for equality is a bad idea.
http://www.parashift.com/c++-faq-lit...html#faq-29.17