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

• 03-21-2006
Kaidao
Types, Integral Types, Bytes?!?!?!
Hey guys, I was reading the C++ Primer book, and I just got to Chapter 2, and I'm confused. :( :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.
• 03-21-2006
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; }```
• 03-21-2006
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
• 03-21-2006
indigo0086
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.