List n Prime Numbers

Hello all,
I'm still new to C++ and I'm trying to write a program to list n prime numbers (n being user defined) and display the first 20 on the console and save to total n primes to a file. Right now I only need help with the function calls for my prime calculation and output formatting.

Here's the code I have so far

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

using namespace std;

// Define function for output
void showResults(int sequence, int prime) {
	cout << "Prime #" << setw(4) << left << sequence << right <<" = " << prime << endl;
	return;
}

int primeFunction() {
	int n=1, status = 1, num = 3, count, c, prime = 2;
	for (count = 2; count <= n; ) //loop that will iterate through n numbers
	{
		for (c = 2; c <= (int)sqrt(num); c++)// for each element check whether it is prime or not
		{
			//if number is completely divisible by a number other than 1 and itselft,then number is not prime
			if (num%c == 0)
			{
				status = 0;
				break;
			}
		}
		//if it is a prime number, print it
		if (status != 0)
		{
			//cout << prime << endl;
			count++;
		}
		status = 1;
		num++;
	}
	return prime;
}
ofstream outfile;
//void saveResults(int sequence, int prime) {
//	outfile << "Prime #" << setw(4) << left << sequence << right << " = " << prime << endl;
//	return;
//}




int main()
{
	// Set data types
	int num = 1; // , int s = 1, int p = 1;
	int sequence = 1;
	int prime = 1;

	// Get information from user
	cout << "How many prime numbers would you like to see? (0 to quit)" << endl;
	cout << endl;
	cout << "Please enter a whole number between 30 and 2000  > ";
	cin >> num;
	while (num == 0) // Allowing for user to quit program
	{
		cout << endl;
		cout << "Quitting program " << endl;
		cout << endl;
		system("pause");
		return 0;
	}
	while (cin.fail() || num < 30 || num > 2000) // Ensure amount entered is within acceptable range
	{
		cout << "Please enter a whole number between 30 and 2000  > ";
		cin >> num;
		cin.clear();
		cin.ignore(1000, '\n');
		while (num == 0) {
			cout << endl;
			cout << "Quitting program " << endl;
			cout << endl;
			system("pause");
			return 0;
		}
	}
	cout << endl;
	cout << endl;
	// Display confirmation message that input is valid and program is ready to proceed
	cout << "Thank you. \nProcessing your results." << endl;
	cout << endl;
	// Clear the screen to prepare for fresh output
	system("pause");
	system("CLS");

	cout << "The first 20 prime numbers are as follows" << endl;
	cout << endl;
	for (int x = 1; x <= 20; x++) {
		showResults(sequence++, prime);		
		}
	cout << endl;
	cout << endl;
	cout << "Please see primes.txt for your full results." << endl;

	// Save all N prime numbers to a file named “primes.txt”.
	//ofstream outfile;
	//outfile.open("primes.txt", ios::out);
	//for (int x = 1; x <= num; x++) {
	//	//saveResults(sequence++, prime);
	//	outfile << "Prime #" << setw(4) << left << sequence << right << " = " << prime << endl;
	//	sequence++;
	//	}

	//outfile.close();



	system("pause");
    return 0;
}


It will format to output style I want but won't display the actual prime numbers.
Thanks for any help
Line 17: How many times do you think this loop executes? Hint: 0
You start at 2 and continue while count <= 1. The condition is false the first time.

Line 15: Where do you actually call primeFunction()? No place that I could find.

Line 93: You ask the user how many primes they want, but here you assume 20.

Line 94: You call showResults() with prime, but prime was never changed from its initial value of 1 (line 53)

Thank you AbstractionAnon.
Those are the problems I'm having.

Here is the code I'm starting with:

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
int main()
{
	int n, status = 1, num = 3, count, c;
	//prompting user to enter number of prime numbers to print
	cout << "Enter the number of prime numbers to print\n";
	cin >> n;

	// input is valid display first prime number
	if (n >= 1)
	{
		cout << "First " << n << " prime numbers are :-" << endl;
		cout << 2 << endl;
	}

	for (count = 2; count <= n; ) //loop that will iterate through n numbers
	{
		for (c = 2; c <= (int)sqrt(num); c++)// for each element check whether it is prime or not
		{
			//if number is completely divisible by a number other than 1 and itselft,then number is not prime
			if (num%c == 0)
			{
				status = 0;
				break;
			}
		}
		//if it is a prime number, print it
		if (status != 0)
		{
			cout << num << endl;
			count++;
		}
		status = 1;
		num++;
	}
	system("pause");
	return 0;
}

This program on its own works fine

I'm trying to turn this into a function and plug the results into my output function.
I inserted the following around line 93 trying to call the primeFunction:

1
2
3
4
primeFunction(num); // trying to pass the user input number to function
	for (int x = 1; x <= 20; x++) {
		showResults(sequence++, prime);		
		}


Whenever I try to call in my modified code it will tell me arguments are invalid or that int prime is not defined and if I define prime then I run into the problem of it never changing from the value I define it with.

The reason for the assumption of 20 is because I'm going to display the first 20 primes on the console but send all the primes the user wants to a file later (see lines commented out for now) after I get the first part correct.

I hope this helps to clarify
Thank you AbstractionAnon for your advice.

I ended up reworking the code and it is operating within the parameters I need.

Topic archived. No new replies allowed.