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; }



LinkBack URL
About LinkBacks



