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. accessing a part of a char array

accessing a part of a char array

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structuresquestion
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.
  • J Offline
    J Offline
    Johpoke
    wrote on last edited by
    #1

    Im working in C++ w MFC. In my program i create a char that looks kinda like this: char data[11] = "1101110110"; and then later in the program i have a for which reads this binary data and does certain things depending on if its a 1 or 0: for ( short c=0; c<=10; c++ ) { if (data[c] == 1 ) { //do this } } But it doesn't work at all, the data[c] always seems to return 0 with the if.. any ideas? maybe theres a better way to store my binary data (which is actually a lot longer, like 64 chars) and then do things depending on 1 or 0? thanks! man ive gotten like loads of post errors now.. /Johannes

    J P S S J 5 Replies Last reply
    0
    • J Johpoke

      Im working in C++ w MFC. In my program i create a char that looks kinda like this: char data[11] = "1101110110"; and then later in the program i have a for which reads this binary data and does certain things depending on if its a 1 or 0: for ( short c=0; c<=10; c++ ) { if (data[c] == 1 ) { //do this } } But it doesn't work at all, the data[c] always seems to return 0 with the if.. any ideas? maybe theres a better way to store my binary data (which is actually a lot longer, like 64 chars) and then do things depending on 1 or 0? thanks! man ive gotten like loads of post errors now.. /Johannes

      J Offline
      J Offline
      Johan Pretorius
      wrote on last edited by
      #2

      you are testing ascii values ie. '1' == 49 for this to work you can test if (data[c] == 49) or you can test if (data[c] == '1') Hope this helps :)


      Artificial Intelligence is no match for Natural Stupidity
      No one can understand the truth until he drinks of coffee's frothy goodness. ~Sheik Abd-al-Kadir
      I can't always be wrong ... or can I?

      1 Reply Last reply
      0
      • J Johpoke

        Im working in C++ w MFC. In my program i create a char that looks kinda like this: char data[11] = "1101110110"; and then later in the program i have a for which reads this binary data and does certain things depending on if its a 1 or 0: for ( short c=0; c<=10; c++ ) { if (data[c] == 1 ) { //do this } } But it doesn't work at all, the data[c] always seems to return 0 with the if.. any ideas? maybe theres a better way to store my binary data (which is actually a lot longer, like 64 chars) and then do things depending on 1 or 0? thanks! man ive gotten like loads of post errors now.. /Johannes

        S Offline
        S Offline
        sunit5
        wrote on last edited by
        #3

        if ( 1==(int)data[c])

        never say die

        S 1 Reply Last reply
        0
        • J Johpoke

          Im working in C++ w MFC. In my program i create a char that looks kinda like this: char data[11] = "1101110110"; and then later in the program i have a for which reads this binary data and does certain things depending on if its a 1 or 0: for ( short c=0; c<=10; c++ ) { if (data[c] == 1 ) { //do this } } But it doesn't work at all, the data[c] always seems to return 0 with the if.. any ideas? maybe theres a better way to store my binary data (which is actually a lot longer, like 64 chars) and then do things depending on 1 or 0? thanks! man ive gotten like loads of post errors now.. /Johannes

          P Offline
          P Offline
          prasad_som
          wrote on last edited by
          #4

          Johpoke wrote:

          if (data[c] == 1 ) {

          change this to

          if (data[c] == '1' )
          {
          //do this
          }

          Prasad Notifier using ATL | Operator new[],delete[][^]

          1 Reply Last reply
          0
          • S sunit5

            if ( 1==(int)data[c])

            never say die

            S Offline
            S Offline
            sunit5
            wrote on last edited by
            #5

            i m not able to modify my posting so a better way int i=atoi(&data[c]); if ( 1==i) ;)

            never say die

            1 Reply Last reply
            0
            • J Johpoke

              Im working in C++ w MFC. In my program i create a char that looks kinda like this: char data[11] = "1101110110"; and then later in the program i have a for which reads this binary data and does certain things depending on if its a 1 or 0: for ( short c=0; c<=10; c++ ) { if (data[c] == 1 ) { //do this } } But it doesn't work at all, the data[c] always seems to return 0 with the if.. any ideas? maybe theres a better way to store my binary data (which is actually a lot longer, like 64 chars) and then do things depending on 1 or 0? thanks! man ive gotten like loads of post errors now.. /Johannes

              S Offline
              S Offline
              S Douglas
              wrote on last edited by
              #6

              Johpoke wrote:

              char data[11] = "1101110110";

              Are you really using

              data

              as a chars or as numbers? You can't directly compair a number to a char. If

              data

              is just an array of numbers and thats all its used for why not do an array of numbers then?


              I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:

              1 Reply Last reply
              0
              • J Johpoke

                Im working in C++ w MFC. In my program i create a char that looks kinda like this: char data[11] = "1101110110"; and then later in the program i have a for which reads this binary data and does certain things depending on if its a 1 or 0: for ( short c=0; c<=10; c++ ) { if (data[c] == 1 ) { //do this } } But it doesn't work at all, the data[c] always seems to return 0 with the if.. any ideas? maybe theres a better way to store my binary data (which is actually a lot longer, like 64 chars) and then do things depending on 1 or 0? thanks! man ive gotten like loads of post errors now.. /Johannes

                J Offline
                J Offline
                Johpoke
                wrote on last edited by
                #7

                Ah yes of course thank you all!

                /Johannes

                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