Hi guys. I am trying to create a program where the user inputs an integer and the program will cout the nth prime number. Unfortunatly when I try to cout just the nth prime number it doesn't work. However theoretically if I have the program print out all of the prime numbers up to the nth prime number, it just prints out 3,4,5,6,7,8 etc. UI means userinput in my code.
#include<iostream>
using namespace std;
int main(){
unsigned int UI; // userinput
int counter1 = 1; // number of primenumbers
bool isprime = true;
int primenumber = 3; //number that it is checking to see if it is prime.
cout << "Enter in a positive integer" << endl;
cin >> UI;
while(UI == 0){
cout << "Invalid input. Please try again" << endl;
cin >> UI;
}
if(UI == 1){
cout << endl << "2"<< endl;
}
else if(UI == 2){
cout << endl << "3" << endl;
}
else if(UI > 2){
while(counter1 != UI){
isprime = true;
cout << primenumber << ",";
for(int checker = 2; checker < primenumber; checker++){
if(primenumber % checker == 0){
isprime = false;
break;
}
}
if(isprime == true){
counter1++;
}
primenumber++;
}
if(counter1 == UI){
cout << primenumber << endl;
}
}
}