Help with Decimals

This is a discussion on Help with Decimals within the C++ Programming forums, part of the General Programming Boards category; I'm having some trouble with a simple little program i made. I was in Geometry and we had just got ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    5

    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)

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Quote Originally Posted by Deadlyaim
    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.
    Last edited by SlyMaelstrom; 09-28-2006 at 05:50 PM.
    Sent from my iPadŽ

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    > cout<<"Hello. Please enter X1:";
    > 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;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Replace your int variables with float variables and you should be set.
    Using double would be more appropriate than float.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    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.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    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.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    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.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    Perfect!

    Now i was just wondering how would i go about making it so the user is asked if they want to do another equation. I cheated my way with the 2nd program by doing the IF THAN statement, but that only works once and makes the program larger than need be, how would i go about doing that?

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Use a loop. It one of the basic tutorials at Cprogramming.com
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help to show decimals
    By engstudent363 in forum C Programming
    Replies: 4
    Last Post: 02-19-2008, 04:13 PM
  2. allow decimals
    By dankassdann in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2006, 07:41 PM
  3. multiple decimals = problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-18-2002, 09:32 AM
  4. Decimals To Binary
    By kas2002 in forum C++ Programming
    Replies: 8
    Last Post: 06-08-2002, 12:12 PM
  5. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 12:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21