Thread: Password

  1. #31
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Haha, well I suppose morbidly geeky would fascinate me as well.

    Thank you for the response ^_^, I'm wondering though..how/why floats store values like this? I would think an innaccuracy like this wouldn't go un-fixed if it was fixable.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  2. #32
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    It's not an error, it's a way for the computer to handle high precision decimal numbers. Internally the number is stored in such a way that it can be used for either very large numbers (many places to the left of the decimal) or very small numbers (many places to the right of the decimal).

    Basically, the machine represents the numbers in exponential format for floats (one bit for the sign, 7-11 bits (depending on float or double) for another part, and the remaining bits for the mantissa) versus using the binary nature of storage for ints.

    Whereas ints are represented like such:
    00000000 = zero
    00000010 = two
    00000100 = four
    00000101 = five
    etc.

    A float is stored with a very different method (rather complex and can vary from compiler to compiler) in such a way that while it is not exact, it holds a much broader range. Internally it looks more like

    0 0100101 010010100101010010101000

    if you were to break up the bits into the different parts. (some compilers store them in different orders, but the result is the same)

    Most of this is academic, unless you want to get into compiler design or very advanced tricks and stuff. The main thing to remember is this: == with floats or doubles is most likely to be false, no matter what the values of the numbers, while != is most likely to be true with floats and doubles, no matter what the numbers.

    Like any tool, you use them when appropriate and remember how to use them properly. floats and doubles are useful for representing money, temperature, and anything else that might have a fractional part, ints are useful for most other things.

  3. #33
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I'm wondering though..how/why floats store values like this?
    Good stuff
    My best code is written with the delete key.

  4. #34
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    for representing money, temperature,
    Why?
    Due to the inaccuracy of floating-point types, isn't it better to simply use a larger integer type and divide by a set amount to get the real answer?

    i.e.
    Code:
    float fNumber = 150.25;//normal, but possibly inaccurate
    long long lNumber = 150250000;//just divide this by 6 and you can get the real value.  you can use == on the 
    long longs and just cast them to float and divide by 6 for output

  5. #35
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Fixed-point math is indeed popular, but 6 is a weird radix. You usually use powers of 2 or 10.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #36
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Thank you Jessycat and Prelude for your help I'm taking a look at that pdf right now infact.

    What do you mean by radix?
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  7. #37
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    radix == base, from what I remember of the docs on atoi() or itoa() (both nonstandard apparently.. *sigh*) or whatever
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #38
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    It was just an example; you can make it whatever you want (as long as it will fit in the variable).

  9. #39
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    atoi is standard, itoa is not. And yes, radix is the base of the number system. Decimal has radix 10, binary radix 2.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #40
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>atoi is standard, itoa is not
    Oh. Well, good to know at least one of them is usable
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #41
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    I use itoa a lot, and I really don't like all the compiler warnings I get.

    My solution is just to rewrite it and call it my_itoa

  12. #42
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    i Just thought i'd take a shot at the initial problem:



    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
         char password[20];
         cout<<"Enter Password: ";
         cin.getline(password, 20);
         if(strcmp(password, "PASSWORD")==0)  // your password in ""
         {
              cout<<"Correct!";
              .......Whatever you want it to do.....
         }
         else{
              cout<<"Wrong!";
         }
         return 0;
    }
    i think the (strcmp) thing is right, im new to this too.

  13. #43
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    As explained before, this is terrible protection because you can view it in a hex editor or disassembler (hell, even notepad!)

  14. #44
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Encryption is a very important aspect of computer programming. As prospective computer scientists we should strive to become encryption experts. Go here to learn more.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  15. #45
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >My solution is just to rewrite it and call it my_itoa
    Or you could use sprintf, or better yet, stringstreams. That would save you the trouble of potentially introducing unnecessary bugs by reinventing an existing solution.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading a password from a file.
    By medeshago in forum C Programming
    Replies: 15
    Last Post: 12-21-2008, 07:20 AM
  2. [Q]Hide Password
    By Yuri in forum C++ Programming
    Replies: 14
    Last Post: 03-02-2006, 03:42 AM
  3. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 08:42 PM
  4. Password prompt in unix w/o \b
    By rafe in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 08:54 AM
  5. password
    By hammers6 in forum C Programming
    Replies: 1
    Last Post: 10-10-2001, 12:14 AM