Any advice on cleaning up my program

I have my program but it looks bulky to me.
I may be wrong but here it is tell me what you think.

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  // Identify and list the N prime numbers, starting with 2 (2 is a prime number), where N is a number obtained from the user.
// This number N must be in the range of 30 to 2000, inclusive.
// Display the first 20 primes on the screen and save N prime numbers to file "primes.txt"

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

using namespace std;

void primeFunction();
void showResults(int sequence, int& pnum);
void saveResults(int& num);
void fileResults(int sequence, int& pnum);

ofstream outfile;

int main()
{
	// Set data types
	int num = 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;
	saveResults(num);
	primeFunction();
	cout << endl;
	cout << endl;
	cout << "Please see primes.txt for your full results." << endl;
	cout << endl;

	system("pause");
    return 0;
}

void primeFunction() {
	int n = 1, status = 1, pnum = 2, count, c, sequence = 1;
	for (count = 1; count <= 20; ) //loop that will iterate through n numbers
	{
		for (c = 2; c <= (int)sqrt(pnum); 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 (pnum%c == 0)
			{
				status = 0;
				break;
			}
		}
		//if it is a prime number, print it
		if (status != 0)
		{
			showResults(sequence++, pnum);
			count++;
		}
		status = 1;
		pnum++;
	}
	return;
}
// Define function for output
void showResults(int sequence, int& pnum) {
	cout << "Prime #" << setw(4) << left << sequence << right << " = " << pnum << endl;
	return;
}
// Define function for output
void fileResults(int sequence, int& pnum) {
	outfile << "Prime #" << setw(4) << left << sequence << right << " = " << pnum << endl;
	return;
}
// Send user requested data to file "primes.txt"
void saveResults(int& num) {
	outfile.open("primes.txt");
	int n = 1, status = 1, pnum = 2, count, c, sequence = 1;
	outfile << "The first " << num << " prime numbers you requested are as follows" << endl;
	outfile << endl;
	for (count = 1; count <= num; ) //loop that will iterate through n numbers
	{
		for (c = 2; c <= (int)sqrt(pnum); 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 (pnum%c == 0)
			{
				status = 0;
				break;
			}
		}
		//if it is a prime number, print it
		if (status != 0)
		{
			fileResults(sequence++, pnum);
			count++;
		}
		status = 1;
		pnum++;
	}
	outfile.close();

}


Thanks ahead of time
Stop using the global variables and learn to pass the variables into your functions.

You seem to have a lot of code duplication. You should try to reduce this duplication.

For example your fileResults() and showResults() functions are basically the same, if you pass the stream into one of the functions you could eliminate the other.

Your primeFunction() and saveResults() are also almost identical and you could probably combine them in some way.
Hi,

The main program asks for a number to calculate primes until a given number, but the rest of it is hard coded to go to 20.

With the algorithm, it is very inefficient to do trial division. Instead consider removing multiples of numbers from a list.

So start out with all the numbers, then remove multiples of 2. The next number in the list is 3, so remove all multiples of that. Now the next is 5, so do the same and continue in that fashion. This is much more efficient, the amount of numbers in the list reduces quickly.

A further enhancement is to use a std::bitset. This is like an array of 0's and 1's, the index is your number and you can go through and set the positions to false if it is a multiple.
Topic archived. No new replies allowed.