I'm just not seeing it.

Write your question here.
I do not see where my mistake is. When I run my program I get an error stating that numberIsPrime is not initialized. I tried to initialize numberIsPrime = numberToTest which stopped the error but it would return 4 as a prime number. Please help if you can.
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
#include<iostream>
using namespace std;

// TODO: Declare function prototype
//       - Name: isPrime
//         Parameters:
//         - 1 int for the number you are trying to determine "primeness" (pass by value)
//         Returns: a bool - true if number is prime, false otherwise

bool isPrime(int numberToTest);

// ********************************************
int main()
{
	// Declare program variables.
	char goAgain;
	int numberToTest;
	bool numberIsPrime;

	// Loop as many times as the user would like
	do {
		// Prompt the user to enter a number and read their answer
		do {
			cout << "What positive whole number would you like to test to see if it is prime: ";
			cin >> numberToTest;
		} while (numberToTest <= 0);     // the number to test must be greater than 0

		// TODO: Call function to determine if the user's number is a prime
		// Pass one argument representing the number to be tested
		// Assign the returned value to the correct variable
		
		if (isPrime(numberToTest))

			numberIsPrime = 
		
		// Display output
		if (numberIsPrime)
			cout << numberToTest << " is a prime number." << endl;
		else
			cout << numberToTest << " is NOT a prime number." << endl;
		cout << endl;

		// Ask the user if they want to test another number and read their answer
		cout << "Do you want to test another number (y/n): ";
		cin >> goAgain;
	} while (tolower(goAgain) == 'y');  // Repeat until the user answers something other than Y or y.

	cout << "End program - ";
	return 0;
}

// ********************************************
// TODO: Define isPrime()
// Refer to the function prototype above for parameter and return type info
//
//    Declare a variable to hold result, initialize to true (number is prime until we prove otherwise)
//
//    Loop from 2 to the number being tested (do NOT use =)
//       - If the number is evenly divisible by the loop counter (think about how to use % operator to do this)
//            Set the result variable to false (this number is NOT prime)
//
//	  Return the result variable

bool isPrime(int numberToTest)
{

	if (numberToTest == 2)
		return true;
	for (int i = 2; i * i <= numberToTest; i++) {
		if (numberToTest % i == 0)
			return false;
	}
	return true;

}

/* Sample Output

What positive whole number would you like to test to see if it is prime: -10
What positive whole number would you like to test to see if it is prime: 0
What positive whole number would you like to test to see if it is prime: 3
3 is a prime number.

Do you want to test another number (y/n): y
What positive whole number would you like to test to see if it is prime: 4
4 is NOT a prime number.

Do you want to test another number (y/n): y
What positive whole number would you like to test to see if it is prime: 5
5 is a prime number.

Do you want to test another number (y/n): y
What positive whole number would you like to test to see if it is prime: 67
67 is a prime number.

Do you want to test another number (y/n): n
End program - Press any key to continue . . .
*/
Hello AlexPlanks,

on line 34 you are missing something on the rhs of the'='. I am guessing this would a function call that returns a bool.

It is ALWAYS a good practice and programming to initialize your variables. If your compiler is using the C++11 standards or after the easiest way to initialize variables is with empty {}s, e.g., int num{};. This will initialize the variable to 0 (zero) or 0.0 for a double. A "char" will be initialized to "\0". "std::string"s are empty to start with an do not need initialized. Should you need to you can put a number between the {}s.You can also Initialize an array, e.g., int aNumbers[10]{};. This will initialize all elements of the array to 0 (zero). A use of
int aNumbers[10]{ 1 }; will initial the first element to "1" and the rest of the array to "0".. Following the "1" with ", 2 ..." will initialize the array with the numbers that you have used up to the entire array.

I refer to lines 16 - 18.

Hope that helps,

Andy
Well yes in my question I stated that I attempted to initialize numberIsPrime = numberToTest and while it got rid of the error, an erroneous result of every number entered being considered a prime number (For example the number 4, would be registered as a prime number) was created in the output. I was hoping someone could possibly identify where my mistake is.

Last edited on
On line 18, there is no initialiser for numberIsPrime

Till the condition in the if statement on line 32 is true, numberIsPrime would remain an uninitialised value.
Once given a value, if the condition in the if statement on line 32 is false numberIsPrime would retain its old value.

Something like this, perhaps:

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

// TODO: Declare function prototype
//       - Name: isPrime
//         Parameters:
//         - 1 int for the number you are trying to determine "primeness" (pass by value)
//         Returns: a bool - true if number is prime, false otherwise

bool isPrime(int numberToTest);

// ********************************************
int main()
{
    // Declare program variables.
    char goAgain;
    int numberToTest;
    /////////////////////////////////////////////////////////////////////////////
    // bool numberIsPrime; *** postpone declaring this variable till we know how to initialise it
    ////////////////////////////////////////////////////////////////////////////////

    // Loop as many times as the user would like
    do {
        // Prompt the user to enter a number and read their answer
        do {
            cout << "What positive whole number would you like to test to see if it is prime: ";
            cin >> numberToTest;
        } while (numberToTest <= 0);     // the number to test must be greater than 0

        // TODO: Call function to determine if the user's number is a prime
        // Pass one argument representing the number to be tested
        // Assign the returned value to the correct variable
        
        ///////////////////////////////////////////////////////////////////////////////
        // if (isPrime(numberToTest)) // ******

        const bool numberIsPrime = isPrime(numberToTest) ; // *** initialised to either true or false
        //////////////////////////////////////////////////////////////////////////////////

        // Display output
        if (numberIsPrime)
            cout << numberToTest << " is a prime number." << endl;
        else
            cout << numberToTest << " is NOT a prime number." << endl;
        cout << endl;

        // Ask the user if they want to test another number and read their answer
        cout << "Do you want to test another number (y/n): ";
        cin >> goAgain;
    } while (tolower(goAgain) == 'y');  // Repeat until the user answers something other than Y or y.

    cout << "End program - ";
    return 0;
}

// ********************************************
// TODO: Define isPrime()
// Refer to the function prototype above for parameter and return type info
//
//    Declare a variable to hold result, initialize to true (number is prime until we prove otherwise)
//
//    Loop from 2 to the number being tested (do NOT use =)
//       - If the number is evenly divisible by the loop counter (think about how to use % operator to do this)
//            Set the result variable to false (this number is NOT prime)
//
//	  Return the result variable

bool isPrime(int numberToTest)
{

    if (numberToTest == 2)
        return true;
    for (int i = 2; i * i <= numberToTest; i++) {
        if (numberToTest % i == 0)
        return false;
    }
    return true;

}

/* Sample Output

What positive whole number would you like to test to see if it is prime: -10
What positive whole number would you like to test to see if it is prime: 0
What positive whole number would you like to test to see if it is prime: 3
3 is a prime number.

Do you want to test another number (y/n): y
What positive whole number would you like to test to see if it is prime: 4
4 is NOT a prime number.

Do you want to test another number (y/n): y
What positive whole number would you like to test to see if it is prime: 5
5 is a prime number.

Do you want to test another number (y/n): y
What positive whole number would you like to test to see if it is prime: 67
67 is a prime number.

Do you want to test another number (y/n): n
End program - Press any key to continue . . .
*/
Last edited on
Hello AlexPlanks,

I stand by what I said earlier.

I changed the variable definitions to:
1
2
3
char goAgain{};
int numberToTest{};
bool numberIsPrime{};  // <--- Could also be written as bool numberIsPrime{ false }; 

I put a comment on lines 32 and 34 and then changed line 37 to if (isPrime(numberToTest)). Since the function returns a bool, e.g., true or false, that is all you need for the if statement. Tested and working.

I believe this would be the easiest fix for what you have.

If you do not understand something let me know.

Hope that helps,

Andy
Topic archived. No new replies allowed.