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. What happened here????? Wrong variable type???

What happened here????? Wrong variable type???

Scheduled Pinned Locked Moved C / C++ / MFC
beta-testingtutorialtestinghelpquestion
5 Posts 2 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.
  • R Offline
    R Offline
    Richard Cheng
    wrote on last edited by
    #1

    Here is the codes float a,b; a = (( (AlphaTotal - PrevAlphaTotal) / PrevAlphaTotal ) * 100 ); b = (( (BetaTotal - PrevBetaTotal ) / PrevBetaTotal ) * 100 ); CString testing; CStringList ReturnedData; testing.Format("Alpha Percentage %f", a ); ReturnedData.AddTail(testing); testing.Format("Beta Percentage %f", b ); ReturnedData.AddTail(testing); (--->ReturnedData will be used to output in view class) The results would be: If a or b = 65.50...it displays 0.00 If a or b = 180.50...it displays 100.00 Why i know that because.....I try just type some number in the formula...for example, a = ((700-500)/500)*100; b = ((81-60)/60)*100; Then output is: Alpha Percentage 0.00 Beta Percentage 0.00 How to fix it?????????????????????? ;P

    M 1 Reply Last reply
    0
    • R Richard Cheng

      Here is the codes float a,b; a = (( (AlphaTotal - PrevAlphaTotal) / PrevAlphaTotal ) * 100 ); b = (( (BetaTotal - PrevBetaTotal ) / PrevBetaTotal ) * 100 ); CString testing; CStringList ReturnedData; testing.Format("Alpha Percentage %f", a ); ReturnedData.AddTail(testing); testing.Format("Beta Percentage %f", b ); ReturnedData.AddTail(testing); (--->ReturnedData will be used to output in view class) The results would be: If a or b = 65.50...it displays 0.00 If a or b = 180.50...it displays 100.00 Why i know that because.....I try just type some number in the formula...for example, a = ((700-500)/500)*100; b = ((81-60)/60)*100; Then output is: Alpha Percentage 0.00 Beta Percentage 0.00 How to fix it?????????????????????? ;P

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      Are AlphaTotal, PrevAlphaTotal, etc. all integral types? Probably so. Use floating point types (or cast the variables to doubles) otherwise the division operator does an integer divide, which throws away any remainder. That's why 200/500*1000 is 0 - 200/500 is an integer divide and returns 0. --Mike-- http://home.inreach.com/mdunn/ "Holding the away team at bay with a non-functioning phaser was an act of unmitigated gall. I admire gall."   Lt. Cmdr. Worf

      R 1 Reply Last reply
      0
      • M Michael Dunn

        Are AlphaTotal, PrevAlphaTotal, etc. all integral types? Probably so. Use floating point types (or cast the variables to doubles) otherwise the division operator does an integer divide, which throws away any remainder. That's why 200/500*1000 is 0 - 200/500 is an integer divide and returns 0. --Mike-- http://home.inreach.com/mdunn/ "Holding the away team at bay with a non-functioning phaser was an act of unmitigated gall. I admire gall."   Lt. Cmdr. Worf

        R Offline
        R Offline
        Richard Cheng
        wrote on last edited by
        #3

        Oh thanks!!!! It works now... but i have one more question. For example, a = 180.98763 I just want to display 180.99....and save it to a char or CString because i want to save to my CStringList........what can i do??? can i use: char Buffer[128]; sprintf(Buffer, " %f %f", a,b); I have a and b......I want to display liked: | 180.98 | 123.56 | ??????

        R M 2 Replies Last reply
        0
        • R Richard Cheng

          Oh thanks!!!! It works now... but i have one more question. For example, a = 180.98763 I just want to display 180.99....and save it to a char or CString because i want to save to my CStringList........what can i do??? can i use: char Buffer[128]; sprintf(Buffer, " %f %f", a,b); I have a and b......I want to display liked: | 180.98 | 123.56 | ??????

          R Offline
          R Offline
          Richard Cheng
          wrote on last edited by
          #4

          When i use this code to save the result to a char, char Buffer[128]; sprintf(Buffer, " %f %f", a,b); my program is forced to terminate....why?

          1 Reply Last reply
          0
          • R Richard Cheng

            Oh thanks!!!! It works now... but i have one more question. For example, a = 180.98763 I just want to display 180.99....and save it to a char or CString because i want to save to my CStringList........what can i do??? can i use: char Buffer[128]; sprintf(Buffer, " %f %f", a,b); I have a and b......I want to display liked: | 180.98 | 123.56 | ??????

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #5

            For example, a = 180.98763 I just want to display 180.99 A format string of "%.2f" will do that. Check the printf() documentation for a full list of the format strings. --Mike-- http://home.inreach.com/mdunn/ "Holding the away team at bay with a non-functioning phaser was an act of unmitigated gall. I admire gall."   -- Lt. Cmdr. Worf

            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