- #1
nukeman
- 655
- 0
Homework Statement
Ok, I just finished my code for my program. And its not compiling, and I am not sure what's wrong.
Below is an image of the errors and the code.
http://i55.tinypic.com/in8x0y.png
And my full code is below here:
MOD NOTE: I indented the code below to make it easier to read.
Code:
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int x)
{
int divisor;
int maxDivisor;
if (x == 2)
return true;
else if (x % 2 == 0)
return false;
else
{
divisor = 3;
maxDivisor = (int)sqrt(x);
while (divisor <= maxDivisor)
{
if (x % divisor == 0)
return false;
else
divisor = divisor + 2;
}
return true;
}
}
int getIntBetween(int low, int high)
{
if (low<2 || high>2000)
{
return 0;
}
else{
return min + (rand() % (int)(high - low + 1));
}
}
int main()
{
const int MAXPRIME = 2000;
int min;
int limit1;
int limit2;
cout << "Enter the lower limit (2, "<< MAXPRIME << "): ";
cin >> limit1;
min = getIntBetween(2, MAXPRIME);
while (min == 0)
{
cout << "Please Enter a Number Between (2, "<< MAXPRIME << "): ";
cin >> limit1;
min = getIntBetween(limit1, MAXPRIME);
}
cout << "Enter the Upper limit (2, "<< MAXPRIME << "): ";
cin >> limit2;
min = getIntBetween(2, limit2);
while (min == 0)
{
cout << "Please Enter a Number Between (2, "<< MAXPRIME << "): ";
cin >> limit2;
min = getIntBetween(2, limit2);
}
String s = "The primes between "+limit1+" and "+limit2+" are ...\n";
int count = 0;
for (int i=limit1; i<=limit2; i+=2)
{
if (i % 2 == 0)
{
i++;
}
if (isPrime(i))
{
s = s + "i";
count++;
if (count % 5 == 0)
{
s = s + "\n";
}
}
}
cout << s << endl;
cout << "There are " << count << "prime numbers between " << limit1 << " and " << limit2 << endl;
return 0;
}
Homework Equations
The Attempt at a Solution
Last edited by a moderator: