# Thread: sizeof(): why the problem?

1. ## sizeof(): why the problem?

I just want to prevent the user from entering too large a value for my integer variable "max". Even a max == 300 triggers the warning. When I removed these warning statements, I could do well with max == 300 or larger. Could you please help? Thanks a lot!

Code:
```// Exercise 12 - Chapter 4
// Find all prime numbers between 1 - max,
// where, max is given by the user
// Primes found using the simpliest method

/*
Given n, keep dividing n by 2, 3, ..., (n-1). If divisible by any of these,
then it's not a prime. Otherwise, it's a prime. By definition, 1 is not
a prime, and 2 is the only even prime.
*/

#include "std_lib_facilities.h"
#include <cmath>

bool is_prime(int);

int main()
{
cout << "Enter a terminal number to which to find all";
cout << " prime numbers from 1: ";
int max;
cin >> max;
if(max > sizeof(max)) {
cout << "Number exceeding defined ranged!\n";
}

while (true) {
cout << "Do you want to try again (Y/N): ";
string choice;
cin >> choice;
if (choice == "Y" || choice == "Yes" || choice == "yes" || choice == "y") {
cout << "Enter a terminal number to which to find all";
cout << " prime numbers from 1: ";
cin >> max;
break;
}
else if (choice == "N" || choice == "No" || choice == "no" || choice == "n") {
return 0;
}
}

vector <int> myprimes;

for (int i = 1; i <= max; ++i) {
if (is_prime(i)) {
myprimes.push_back(i);
}
}

for (unsigned i = 0; i < myprimes.size(); ++i)
cout << myprimes[i] << endl;
}

bool is_prime(int x)
{
bool found;

if (x < 2)
found = false;
if (x == 2)
found = true;

for (int i = 2; i < x; ++i) {
if ((x % i) == 0) {
found = false;
break;
}
else
found = true;
}
return found;
}```

2. sizeof results in the number of bytes an object of its operand, or operand's type, takes up.

In this case, it should suffice to check the state of the stream after cin >> max. Strictly speaking, this results in undefined behaviour in the case of an overflow, but it appears to work nicely on the systems I tested.

3. Originally Posted by laserlight
sizeof results in the number of bytes an object of its operand, or operand's type, takes up.

In this case, it should suffice to check the state of the stream after cin >> max. Strictly speaking, this results in undefined behaviour in the case of an overflow, but it appears to work nicely on the systems I tested.
Sorry, but I don't get it. I'm still at a very elementary level (I'm learning C++ entirely by self-study as a first programming language and using the book "Programming: Principles and Practice using C++" by BS). I'm now at Chapter 4 (Control Statements).

What I want is somehow use some built-in function to determine the max value that an integer can take, say, an integer in C++ is assumed to take up to a maximum value of N. I don't want to refer to the reference and fill in the value itself, just want to call some function. How can I do this?

Thanks a lot for your time and help!

4. Even if sizeof() worked the way you think it does, the code still makes no sense. You seem to be treating sizeof(max) as the maximum value that could ever be achieved by the max variable. But if that's so, then how could the comparison "max > sizeof(max)" ever possibly be true? It's basically saying, "If the value of max is greater than the largest possible value of max" which is asking for the impossible.

5. Originally Posted by brewbuck
Even if sizeof() worked the way you think it does, the code still makes no sense. You seem to be treating sizeof(max) as the maximum value that could ever be achieved by the max variable. But if that's so, then how could the comparison "max > sizeof(max)" ever possibly be true? It's basically saying, "If the value of max is greater than the largest possible value of max" which is asking for the impossible.
Thank you very much. It's very clear to me now.