Thread: Pascal to C++ 2 functions

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    43

    Pascal to C++ 2 functions

    all right I’m trying to convert this Pascal code to C++ witch i can do i just need help with 2 functions

    here is the Pascal code
    Code:
    //Returns the amount Of pixels between a1, b1 And a2, b2
    
    
    Function Dist(a1, b1, a2, b2 : Integer) : Integer;
    Var
      aa, bb : Extended;
    Begin
      If(a1>a2)Then
        aa:= (a1 - a2) * (a1 - a2)
      Else
        aa:= (a2 - a1) * (a2 - a1);
      If(b1>b2)Then
        bb:= (b1 - b2) * (b1 - b2)
      Else
        bb:= (b2 - b1) * (b2 - b1);
      result:= trunc(Sqrt(aa + bb)) + 1;
    End;
    ok now i need help with a trunc function in C++ what it does i believe is takes away the decimal point from any number.
    and the sqrt function witch is square root.
    I’m Dyslexic and I know that I don’t spell well so quit telling to learn my English because I do my best at it all right.

    Windows XP with Dev-C++ for now.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    ok now i need help with a trunc function in C++ what it does i believe is takes away the decimal point from any number.
    Casting to an integral type will result in truncation.

    and the sqrt function witch is square root.
    sqrt() is available in <cmath>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    > ok now i need help with a trunc function in C++ what it does i believe is takes away the decimal point from any number.
    Just cast it to an integral type such as int:

    result = 1 + static_cast< int >( sqrt( aa + bb ) );

    >and the sqrt function witch is square root.
    #include <cmath>
    std::sqrt( double n );

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    43
    thank you
    I’m Dyslexic and I know that I don’t spell well so quit telling to learn my English because I do my best at it all right.

    Windows XP with Dev-C++ for now.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    43
    also if you guys coud answer this one
    Code:
    if (GetAsyncKeyState(VK_ESCAPE))
    {
    exit(0);
    }
    Now if I press the ESC button the then the program should stop right. But it seams to stop even when I haven’t pressed the button.

    lol and thanks laserlight your always answering my posts I appreciate it.
    I’m Dyslexic and I know that I don’t spell well so quit telling to learn my English because I do my best at it all right.

    Windows XP with Dev-C++ for now.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by MSDN
    If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. ....
    -->> it never retuns 0.
    Kurt

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Correct, you need to do an "and with 1" to see if the key was pressed or not.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    43
    ok so the way i have it now it is returning true no matter what because it never returns 0
    can some one leave an example of how it shoud be frased. thanks for you help i see how it works now anyway : )
    I’m Dyslexic and I know that I don’t spell well so quit telling to learn my English because I do my best at it all right.

    Windows XP with Dev-C++ for now.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    if (GetAsyncKeyState(VK_ESCAPE) & 1)
    {
    exit(0);
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM