# Help with Decimals

• 09-28-2006
Help with Decimals
I'm having some trouble with a simple little program i made. I was in Geometry and we had just got some formulas, and i could do them all in my head but everyone else was having trouble with them. So i knew there was a better way and then i came home and made this program. It is used to find the midpoint between two points on a coordinate gride or w/e its called =p.

Code:

```#include <iostream> using namespace std; int main() {     int R, T, Y, U, A, B; cout<<"Hello.  Please enter X1:"; cin>> R; cout<< "Now X2:"; cin>> T; cout<< "Now Y1:"; cin>> Y; cout<< "Finally Y2:"; cin>> U; cin.ignore(); A = R + T; B = Y + U; A = A / 2; B = B / 2; cout<<"The midpoint of the two points is ["; cout<< A <<","; cout<< B; cout<< "]"; cin.get (); }```

The problem is that the program doesnt put out decimals so it's essentially useless.

I also created this program which is used to find the distance between two points.

Code:

``` #include <iostream> using namespace std; int main() {     int R, T, Y, U, A, B, C, D, E, M;     cout<< "Hello Please enter X1:";     cin>> R ;     cout<< "Now X2:";     cin>> T;     cout<< "Y1:";     cin>> Y;     cout<< "Now finally Y2:";     cin>> U;     cin.ignore ();     A = R - T;     B = Y - U;     C = A * A;     D = B * B;     E = C + D;     cout<< "Square this number to get the distance :" << E <<"\n";     cout<< "Would you like to find the distance of another number (1 y, 2 n)";     cin>> M;         if ( M == 1 ) {           cout<< "Hello Please enter X1:" ;     cin>> R ;     cout<< "Now X2:";     cin>> T;     cout<< "Y1:";     cin>> Y;     cout<< "Now finally Y2:";     cin>> U;     cin.ignore ();     A = R - T;     B = Y - U;     C = A * A;     D = B * B;     E = C + D;     cout<< "Square this number to get the distance :" << E <<"\n";     cout<< "Thanks for using the program!"<<"\n";   cin.get (); }     else if ( M == 2 ){         }     cin.ignore();     }```
My knowledge of C++ is very limited, i only got about 5 or 6 lessons in on the tutorials and then school started taking over my life. Any help with my problem is greatly appreciated ^_^ (ALso i realize that this is probably very sloppy coding so i'm sorry in advance)
• 09-28-2006
SlyMaelstrom
Quote:

My knowledge of C++ is very limited

...Umm... I don't know that I would say that, at all. Especially when it comes to using floating points which is about as basic as programming gets. Almost all languages support floating point numbers. Replace your int variables with double variables and you should be set.

I would also say you're using a lot more variables than you need to. You seem to think that C++ is super limited, which it's not and I can tell you that it can support more than one operation per statement... so outside of your coordinate variables and you're result (which can also be ignored), you're just wasting stack space.

Lastly
Code:

`cout<< "Square this number to get the distance :" << E <<"\n";`
Look into the <cmath> library. Specifically the exp() function.
• 09-28-2006
Salem
> cin>> R;
I don't suppose it occurred to you to call your variable X1, having asked to input X1
It would make the rest of the code so much more readable.

If you want decimals, do
double X1;
• 09-28-2006
Daved
>> Replace your int variables with float variables and you should be set.
Using double would be more appropriate than float.
• 09-28-2006
SlyMaelstrom
Quote:

Originally Posted by Daved
>> Replace your int variables with float variables and you should be set.
Using double would be more appropriate than float.

Agreed... I should have said doubles. I usually use the word float as a generic word for floating point variables, but I should have known better with a new programmer.
• 09-28-2006
Ok ill try everything you all said. Thanks everyone! ^_^

PS. I didn't call them X1, X2, or w/e because i was getting some weird errors and when i changed the names of the variables to R, T, it worked fine, maybe an error in my coding probably.
• 09-28-2006
King Mir
Use discriptive variable names. Exept for counters and a few other common variables, your variable names should be at least four letters long, and should say something about the variable's function.
• 09-28-2006