First of, please follow the good advice of OriginalGriff: Get familiar with using a debugger. That's probably going to be the biggest step you will make in your programming skills so far!
Now to your question: You did not show in your code how the variable
sqrt
is being computed. The code as it stands would not even compile.
And second: You have used the variable
temp
as indicator that
i
is not prime. You start off with the assumption that it is prime and then test all integers
j
whether they are a divider of
i
. As soon as you have found a
j
that is a divider, then
i
cannot be prime and you set
temp
to 1. So far so good -- but then you are drawing the wrong conclusion:
if (temp==1)
ans++;
if (ans==n)
{
cout<<"\n "<<n<<"th prime no. is "<<i;
break;
}
In variable
ans
you count how many prime numbers you have found so far. So you should increase it, if
temp
is 0, i.e. no divider of i had been found!
And there is still one more bug lurking: You initialize
temp
at the very beginning of your function. Now consider what happens when
temp
has been set to 1 for the first time. What will happen to the next
i
you are testing? See the problem? It can easily be fixed, but that is left for you as it is your homework.