Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. 'LONG' to 'int' [modified]

'LONG' to 'int' [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Queeny
    wrote on last edited by
    #1

    I have a LONG variable, x, and I need to use the value as an int for my "for" loop. for (int i; imodified on Monday, June 2, 2008 8:47 PM

    M D K A 4 Replies Last reply
    0
    • Q Queeny

      I have a LONG variable, x, and I need to use the value as an int for my "for" loop. for (int i; imodified on Monday, June 2, 2008 8:47 PM

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Where's the code? :) Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      Q 1 Reply Last reply
      0
      • Q Queeny

        I have a LONG variable, x, and I need to use the value as an int for my "for" loop. for (int i; imodified on Monday, June 2, 2008 8:47 PM

        D Offline
        D Offline
        DQNOK
        wrote on last edited by
        #3

        It should work even as a type long (I'm not familiar with type LONG: if it's a composite type, then it won't work). Any reason you can't just cast it to int at the point you are actually using it within the loop? This should work (provided x is not a composite type)

        for( x = someval; x < someotherval; x++ )
        {
           ...
           i = (int)x; //cast x to int where it's actually needed.
        }
        

        David --------- Empirical studies indicate that 20% of the people drink 80% of the beer. With C++ developers, the rule is that 80% of the developers understand at most 20% of the language. It is not the same 20% for different people, so don't count on them to understand each other's code. http://yosefk.com/c++fqa/picture.html#fqa-6.6 ---------

        1 Reply Last reply
        0
        • M Mark Salsbery

          Where's the code? :) Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          Q Offline
          Q Offline
          Queeny
          wrote on last edited by
          #4

          oups...forgot to ignore HTML tags for (int i; i

          M 1 Reply Last reply
          0
          • Q Queeny

            I have a LONG variable, x, and I need to use the value as an int for my "for" loop. for (int i; imodified on Monday, June 2, 2008 8:47 PM

            K Offline
            K Offline
            Kwanalouie
            wrote on last edited by
            #5

            When I compile this code, it compiles with no errors (Visual C++ 6.0) void test(void) { LONG x; x = 800; for (LONG i=0; i < x; i++) { } }

            1 Reply Last reply
            0
            • Q Queeny

              I have a LONG variable, x, and I need to use the value as an int for my "for" loop. for (int i; imodified on Monday, June 2, 2008 8:47 PM

              A Offline
              A Offline
              Akt_4_U
              wrote on last edited by
              #6

              The below code will work. LONG lVal = 100; int nVal = static_cast<int>( lVal );

              akt

              1 Reply Last reply
              0
              • Q Queeny

                oups...forgot to ignore HTML tags for (int i; i

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                You shouldn't have to do anything special to use the LONG. There's no requirement to use int in a 'for' statement. You can actually put any valid code you want to in the three expressions (init/condition/loop). Mark

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups