Nth prime number calculation

I only just recently started learning C++ a few months ago. So i'm not sure how to go about this. In the function int prime(num)the num value should be returned to int(main), I've tried multiple times to fix it, but simply could not figure it out. #include "stdafx.h"
#include <iostream>
using namespace std;


bool IsPrime(int num)
{
if (num != 2) {
if (num < 2 || num % 2 == 0) {
return false;
}
for (int i = 3; (i*i) <= num; i += 2) {
if (num % i == 0) {
return false;
}
}
}
return true;
}



int Prime(int n)
{
int checkprime;
int nthplace=0;
int num=0;
n;

IsPrime(num);
if (true)
for (int num = 2; num <= 10001; num++)
{
checkprime = 0;

for (int i = 2; i <= num / 2; i++)
{

if (num%i == 0)
{
checkprime = 1;
break;
}

}

if (checkprime == 0)
nthplace++;

if (nthplace == n)
cout << n << " Prime num is: " << num << endl;

}
return num++;
}


int main()
{
char yon;
int n;
int num=0;
do {

cout << "Which nth Placed Prime num Are You Looking For? ";
cin >> n;

Prime(n);
num; // num isn't being returned
cout << n << " Prime num is: " << num << endl;

cout << "Would You Like To Find Another Prime num? (Y/N) ";
cin >> yon;
yon = yon | 0x20;

while (yon != 'y' && yon != 'n') {
cout << "Invalid Response, please enter \"Y\" or \"N\": ";
cin >> yon;
yon = yon | 0x20;
}
} while (yon == 'y');

cout << "\nThank you for using this awesome program\n" << endl;
return 0;
}


I realize there is a redundancy in the functions, but I'm more focused on the problem of the value not returning.
Last edited on
Firstly, please use code tags. Your code is painful to read without.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "stdafx.h"
#include <iostream>
using namespace std;


bool IsPrime(int num)
{
  if (num != 2) {
    if (num < 2 || num % 2 == 0) {
      return false;
    }
    for (int i = 3; ( i*i ) <= num; i += 2) {
      if (num % i == 0) {
        return false;
      }
    }
  }
  return true;
}



int Prime(int n)
{
  int checkprime;
  int	nthplace = 0;
  int num = 0;
  n;

  IsPrime(num);
  if (true)
    for (int num = 2; num <= 10001; num++)
    {
      checkprime = 0;

      for (int i = 2; i <= num / 2; i++)
      {

        if (num%i == 0)
        {
          checkprime = 1;
          break;
        }

      }

      if (checkprime == 0)
        nthplace++;

      if (nthplace == n)
        cout << n << " Prime num is: " << num << endl;

    }
  return num++;
}


int main()
{
  char yon;
  int n;
  int num = 0;
  do {

    cout << "Which nth Placed Prime num Are You Looking For? ";
    cin >> n;

    Prime(n);
    num;	// num isn't being returned
    cout << n << " Prime num is: " << num << endl;

    cout << "Would You Like To Find Another Prime num? (Y/N) ";
    cin >> yon;
    yon = yon | 0x20;

    while (yon != 'y' && yon != 'n') {
      cout << "Invalid Response, please enter \"Y\" or \"N\": ";
      cin >> yon;
      yon = yon | 0x20;
    }
  } while (yon == 'y');

  cout << "\nThank you for using this awesome program\n" << endl;
  return 0;
}
Secondly, not addressing other issues, you aren't assigning the value to num. You're just discarding it.

num = Prime(n); will actually make the assignment.
Thank you, I really appreciate it. I'll learn how to use code tags the next time I post something, but how do I make the assignment?
Last edited on
Topic archived. No new replies allowed.