Types, Integral Types, Bytes?!?!?!

This is a discussion on Types, Integral Types, Bytes?!?!?! within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I was reading the C++ Primer book, and I just got to Chapter 2, and I'm confused. . ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    19

    Types, Integral Types, Bytes?!?!?!

    Hey guys, I was reading the C++ Primer book, and I just got to Chapter 2, and I'm confused. . The beginning of the Chapter where it's talking about integral types, bytes, integers, etc. is really confusing. Like, I understand that different types store different information, but one of the practice questions asks what the range of integers are allowed in a 8 bit short type. What....?

    Hmm, maybe I'm too dumb to get into this . But no, I'm not giving up. If any of you can give me an explanation or a URL to an article or something talking about this in a clearer way, please do so, and thanks.

    Also, one of the exercises in the book asks you to write a program to let the user input a series of numbers, and then print out how many negative numbers were put in. I tried this, and all I could manage was for it to print out the value of the negative number, but not how many negative numbers there are. Could someone help me with this please? Since my computer that I write code on is different from this computer, I can't be sure that this is the code I wrote, but I think this is it.

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
                   cout << "Enter a series of numbers: " << endl;
                   int value;
                   cin >> value;
    
                   while (value <= 0)
                           cout << value << endl;
                   
                   cin.ignore(cin.rdbuf()->in_avail()+1);
                   return 0;
    }
    How do I do this..?

    -Many thanks, Kai.

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    An 8 bit variable type will have 255 possible values. The range depends on if the variable is singed or not

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int input = 0, nnegatives = 0;
    	while (input != 2000) //just an arbitrary escape code
    	{
    		cin >> input;
    		if (input < 0)
    			nnegatives++;
    	}
    	cout << nnegatives;
    }

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    Also, when I run a program (from the compiler), and there's any input at all, when you push enter to submit your input, the program will close automatically. Is there any piece of code to get around this?

    Thanks a lot madcow, but can you tell me how you got the number 255? Thanks again.

    -Kai

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Code:
    cin.ignore(cin.rdbuf()->in_avail()+1);
    That was in chapter 2? Is this C++ primer Plus?

    regarding bits it's not entirely important but the way bits work is pretty simple

    Let's say a number is 'n' bits.
    Signed (+ & -) 'n' bit number can represent a range from: -2^(n-1)...+2^(n-1)-1
    Signed 'n' bit numbers represent a range from: 0...+2^(n)-1

    That means an 8 bit signed integer ranges from -128...127, and an 8 bit unsigned int goes from 0 through 255

    In both cases they have 256 possible values (0 inclusive no?)

    I've only really had to know this for assembly which relies bit ranges and such.

    Quote Originally Posted by Kaidao
    Also, when I run a program (from the compiler), and there's any input at all, when you push enter to submit your input, the program will close automatically. Is there any piece of code to get around this?

    Thanks a lot madcow, but can you tell me how you got the number 255? Thanks again.

    -Kai
    you may at the end before (return 0; ) have to have a line that says
    Code:
    cin.get();
    or
    Code:
    System("PAUSE");
    for the last one I think you have to include the cstdlib.
    Last edited by indigo0086; 03-21-2006 at 08:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to convert integral types into pointer types?
    By rohit99 in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2008, 10:57 AM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 03:25 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 11:36 AM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 02:54 PM

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