Error checking for Prime numbers

Hello. For my class I had to write a program that asks for user input of a number. It then checks to see if this number is prime or not. If it is not prime, the program says so and will close.

If it is prime, however, the program will tell you and then go on to ask the user to enter all prime numbers from 1-100. I used an array of 25 elements to store all possible combinations of prime numbers from 1-100. It then saves these elements to a file titled PrimeNumbers.txt

This part of the program is working fine. However, I realized that there is no error checking in the second part of my program. The first part will check if the number is prime or not but the second part does not. Technically you could enter 1, 1, 1, etc... and it would save them as if they were prime.


I need a way to check if the numbers are prime in the second part of my program and I need to be able to break out of the array if the number is not prime and ask the user to start over again.


But I do not know how to go about doing this.

I am also very inexperienced with programming so a detailed description would be much appreciated!

Thank you in advance.

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 <iostream>
#include <fstream>
using namespace std;

bool isPrime(int);    //Function prototype


 bool isPrime(int number) //Passes the variable to function isPrime
{
    //Array variables
     const int PRIME_NUMBERS = 25;    //Number of Prime numbers
     int numbers[PRIME_NUMBERS];      //Each user input
     int count;                       //Loop counter

    //Variables
     ofstream outputFile;
     bool status;
     int i=2;

    while(i<=number-1)
    {
        if(number%i==0)
        {
            status=false;
            cout << number << " is not a prime number." << endl;
            break;
        }
        i++;
    }
    if(i==number)
    {
        status=true;

        //Opens file for output
        outputFile.open("PrimeNumbers.txt");

         cout << number << " is a prime number.\n"; //Returns true value

    //Input the prime numbers (1-100)

     for (count = 0; count < PRIME_NUMBERS; count++)
     {
     cout << "\nGreat. Now, please enter all prime numbers (1-100): \n"
          << (count + 1) << ": ";
     cin  >> numbers[count];
     }

     //Display contents of array
     cout << "The numbers you entered are: ";
     for (count = 0; count < PRIME_NUMBERS; count++)
        cout << " " << numbers[count];
        cout << endl;

    //Write contents of array to a file
        outputFile << "List of prime numbers: \n";
    for (count = 0; count < PRIME_NUMBERS; count++)
        outputFile << numbers[count] << endl;

         cout << "This data has been saved to a file.\n";


        //Closes the file.
         outputFile.close();

    }

    return status;
}

 int main()
 {
     //Local variables
     int number;
     bool status;


    //Asks for user input
     cout << "\nPlease enter a number to find if it is prime: \n";
     cin  >> number; //Stores user input

     //Calls isPrime function
     (isPrime(number));

    return 0;
 }
Last edited on
You are facing difficulties because your function isPrime() does not do what functions are meant to do.
A function is meant to serve a particular purpose and accomplish 1 task.

Fix:

write a standalone bool isPrime(int) .

You can now use this function to check for each user input in the next stage of your program.
For the purposes of this assignment, we were specifically asked to use the same isPrime function for both checking to see if the number is prime and for file output. Sorry about that. I know it doesn't really make sense but that's what we were asked to do.

This being said, how can I fix this issue with my current program being the way that it is?
Then you will need to add a prime checker to validate the input before putting it in array.

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
#include <iostream>
#include <fstream>
using namespace std;

bool isPrime(int);    //Function prototype


 bool isPrime(int number) //Passes the variable to function isPrime
{
    //Array variables
     const int PRIME_NUMBERS = 25;    //Number of Prime numbers
     int numbers[PRIME_NUMBERS];      //Each user input
     int count;                       //Loop counter

    //Variables
     ofstream outputFile;
     bool status;
     int i=2;

    while(i<=number-1)
    {
        if(number%i==0)
        {
            status=false;
            cout << number << " is not a prime number." << endl;
            break;
        }
        i++;
    }
    if(i==number)
    {
        status=true;

        //Opens file for output
        outputFile.open("PrimeNumbers.txt");

         cout << number << " is a prime number.\n"; //Returns true value

    //Input the prime numbers (1-100)
    
    
    /*************************************************/
    for (count = 0; count < PRIME_NUMBERS; count++)
    {
        i = 2;   //previously declared in line 18
        int test;
        bool prime = false;

        cout << "\nGreat. Now, please enter all prime numbers (1-100): \n";
        do
        {
            cout<<"Enter prime number "<<count + 1 <<": ";
            cin>>test;
            while(i < test/2)
            {
                if(test%i==0)
                {
                    cout << test << " is not a prime number." << endl;
                    break;
                }
                i++;
            }
            (i>=test/2) ? prime = true : prime = false;
        }while(prime == false);

        numbers[count] = test;
    }
/*********************************************/

     //Display contents of array
     cout << "The numbers you entered are: ";
     for (count = 0; count < PRIME_NUMBERS; count++)
        cout << " " << numbers[count];
    cout << endl;

    //Write contents of array to a file
    outputFile << "List of prime numbers: \n";
    for (count = 0; count < PRIME_NUMBERS; count++)
        outputFile << numbers[count] << endl;

    cout << "This data has been saved to a file.\n";

    //Closes the file.
    outputFile.close();

    }
    return status;
}

 int main()
 {
     //Local variables
     int number;
     bool status;


    //Asks for user input
     cout << "\nPlease enter a number to find if it is prime: \n";
     cin  >> number; //Stores user input

     //Calls isPrime function
     (isPrime(number));

    return 0;
 }


I did not test for upto 25 prime numbers.
Also, the code does not check for repeating a prime number.
Hope this helps
Thank you for your help.

However, I tested this code by inputting 1 for all 25 elements.

It did not tell me 1 is not a prime number.

Any ideas as to why this is happening?
Replace this section of the code.
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
 /*************************************************/
    for (count = 0; count < PRIME_NUMBERS; count++)
    {
        i = 2;   //previously declared in line 18
        int test;
        bool prime = false;

        cout << "\nGreat. Now, please enter all prime numbers (1-100): \n";
        do
        {
            do
            {
                cout<<"Enter prime number "<<count + 1 <<": ";
                cin>>test ;
            }while(test == 1 || test == 2);

            while(i < test)
            {
                if(test%i==0)
                {
                    cout << test << " is not a prime number." << endl;
                    break;
                }
                i++;
            }
            (i == test ) ? prime = true : prime = false;
        }while(prime == false);

        numbers[count] = test;
    }
/*********************************************/
I changed the test variable to number and now it is working.

Thank you for your help. I appreciate it!
Topic archived. No new replies allowed.