A third noobie question

This is a discussion on A third noobie question within the C++ Programming forums, part of the General Programming Boards category; In the below code, suppose the user enters 32907390247810758172897213892179; what will happen? For some reason, the code interprets that number ...

  1. #1
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167

    A third noobie question

    In the below code, suppose the user enters 32907390247810758172897213892179; what will happen? For some reason, the code interprets that number as being 2; and thus acts as if 2 were entered. However, since 2 was not entered that is a problem. How can it be fixed?

    Code:
    #include <iostream.h>
    
    
    int main()
    {
    
      char x='0';
    
    cout<<"Please choose  (1 or 2): ";
    
    while (x!='1' && x!='2')
       cin>>x;
    
    if (x=='1')
      cout <<"hi";
    if (x=='2')
      cout <<"bye";
    
    
    
    return 0;
    }

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    A char is only 1 byte (on all systems i've worked with) so the largest number it can hold is (2^8)-1. If you enter anything larger, its value loops back around to 0 or -127(?) depending on whether it's signed or unsigned. So if a user enters 4389437984343892 your variable will overflow to something between its max and min.

    Anyway, if you want to find the minimum value or maximum value for a specific type #include <limits> and use std::numeric_limits<TYPENAME>::max(); or ::min();
    Replace TYPENAME with char, int etc.

    For example
    unsigned int zero = std::numeric_limits<unsigned int>::max() + 1;
    zero should now hold 0
    Get it?

  3. #3
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    The first part of that I get, except I don't understand why the program isn't able to detect that an ungainly large number has been entered and thus decide to either somehow calculate the value and give an error message, or just give an error message; interpreting that number to be 2 is not acceptable.

    The second part of your message made absolutely no sense which should not surprise you seeing as how I only started learning c++ some few hours ago.

  4. #4
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    I believe the above code is searching through the numbers entered, so for instance, if it finds a 2, it thinks 2 was entered, for example, entering 12 executes as if 2 was entered.

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Variables can only occupy so much memory. The smallest amount of memory which can be allocated is 1 byte. 1 byte contains 8 bits.

    If you don't know what a bit is, it's a binary digit either a 1, or a 0. So a byte is composed of eight little 1's or 0's

    Suppose you declare a char variable:
    char mychar = 55;
    In memory, a char occupies 1 byte (8 bits). This may look like the following: [memory before mychar][00111011][memory after mychar]

    That string of bits [00111011] is not important. (It's 55 in binary if you're wondering) Okay, so suppose we do:
    mychar = 255;

    Since binary is a base 2 number system (there's only two digits, 1 or 0) we can determine the maximum value a variable can hold by the following: (2^bits)-1. In this case, we have (2^8)-1, which is 255.

    If you don't follow, here's an example of doing the same thing in the decimal (base 10, what you're used to) system:
    (10^digits)-1
    So, the maximum value we can represent with 4 digits is (10^4)-1, or 9999.

    Now, back to our variable. It can hold up to (2^8)-1 using 8 bits. So what happens when you add one? Same thing that happens in the decimal number system:
    (10^4)-1 = 9999
    9999 + 1 = 10000

    Look! All of our previous digits roll over to 0! Lets apply this to our situation in memory.

    mychar = 255;
    That's mychar = (2^8)-1! The maximum value you can hold with 8 bits.
    Now we have something like this:
    [memory before mychar][11111111][memory after char]
    So what happens when you add 1!?
    Well, 11111111 + 1 = 100000000
    So, we now have a 9 bit number that needs to be represented using 8 bits... what happens? Our 9 bit number gets its head lopped off! Our memory is only 8 bits wide, so the 9th bit is discarded. Hell, it never existed!

    Now, we have [memory before mychar][00000000][memory after mychar]


    I honestly hope you understand now, and if you don't I can always try again. If you already knew some of this stuff, please don't feel that I insulted your intelligence. Anyhow, i'm clicking submit!

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Eibro
    Well, 11111111 + 1 = 100000000
    So, we now have a 9 bit number that needs to be represented using 8 bits... what happens? Our 9 bit number gets its head lopped off! Our memory is only 8 bits wide, so the 9th bit is discarded. Hell, it never existed!
    Actually, it's not discarded. The number rolls over.

    11111111 + 1 = 00000000

    and

    11111111 + 2 = 00000001

    This is the basis for binary 2's compliment mathematics. Without that property, subtraction would have to be done a different way.

    Edit: Nevermind, miscounted the bits in your example. Thought you were saying that it stored the value 10000000 (discarding the lsb not the msb)
    Last edited by Polymorphic OOP; 01-21-2003 at 12:58 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 01:14 PM
  2. A fourth noobie question - namespace std?
    By Noobie in forum C++ Programming
    Replies: 24
    Last Post: 08-12-2005, 03:10 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. another noobie question
    By noobie in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 02:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21