All right, I'll try to post my comments
(ignoring cin exceptions, 0
as input etc). Correct me if I'm wrong anywhere.
#include <iostream.h>
#include <stdlib.h>
First of, in C++ programs you should prefer the non-h versions of the
headers, like <iostream> and <stdlib>. The versions you used are
targeted at C programs, are deprecated, and are maintained in C++ only
for compatibility, so avoid it. Some compilers warn about this.
In C++, use int main(). Void might be accepted on some compilers, but
i think it officially is undefined behavior.
cout<<"Enter the number you want to check prime or not ";
cin>>num;
If compiled like that, cout/cin are not declared. These are included
in the standard library, so you should instead either use "std::cout"
and "std::cin" (very recommended) or after your #includes, add the
line "using namespace std;" which has a similar effect.
for (int i=2; i<num; i++){
...
}
if (num==i)
Here, the variable "i" is declared inside the for loop. This means it
is a valid identifier only until the } where the scope of the for
ends. When you try to use it in the if, which is outside its scope,
the compiler should complain that i is undeclared. For example, in my
gcc:
..../src/primetest.cpp:43: error: name lookup of 'i' changed for new
ISO 'for' scoping
..../src/primetest.cpp:36: error: using obsolete binding at 'i'
If your compiler accepts this code, you should probably try to find a
newer version. To work around this, the easiest way is to define i
outside the for loop, like:
int i=2;
for (; i < num; i++){/*...*/}
if (num==i){/*...*/}
I hope you are aware that this is very platform dependant, and not
standard C++. In my machine, when your program reaches this point i
get:
sh: cls: not found
Which I assume is not what you meant
"goto" is generally viewed as bad practice. C++ has better and more
easily comprehended mechanisms to achieve similar effects, so avoid it
unless you have a good reason not to. For example, you could use a
while() construct that exits when a certain flag becomes true. A
simplified similar approach:
/* ... */
bool flag=false;
while(flag==false){
/* read user input */
/* check if it's a prime and print the result */
cin >> restart;
if (restart=='y' || restart=='Y')
{
flag = true;
}
/* do anything else - like clearing the screen */
}
Actually, though, what this program does is not what I meant
You
read a number from the user and calculate whether it is prime or not,
and repeat. What I meant, was that the user would type a number (e.g.
6) and the program would calculate the first 6 prime numbers (e.g. 2,
3, 5, 7, 11, 13).
One way to do this, similar to this program, would be to have a
function that checks if any number is prime. Then have a variable that
stores the number of prime numbers you have found so far, and a while
loop that each time calculates the next prime number and ends when you
find all of them that the user asked for.
A better way, though, would be to have an array (see std::vector) of
consecutive ints: 2,3,4,5,6,... (1 doesn't count in this case). Mark
them all as possible-primes. Then, inside a loop, get the smallest
possible-prime value, and mark all its integer multiplicands(sp?) as
non-prime. When you loop as many times as the user typed (say X), then
the X first possible-primes is what you are looking for. For ease,
have the size of the array of consecutive ints be X*X initially.
(or use an std::list and remove the non-primes maybe)