Conditional

Hello there,

Just a little bit of background on myself: I have some schooling experience in C, VB.Net, C#.net, Oracle, and MySQL. I have never taken a programming language into a great depth, and that is my current goal with C++. I have just started taking C++ seriously. I am working out of the book "C++ Without Fear". While I have been skimming over much of the early information, I try to work the examples and practice problems just to ensure that I am understanding the concepts. I am using Microsoft's Visual C++ 2010 Express IDE. Now, onto the problem:

Here is the program that I have written:

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
#include <iostream>
#include <cmath>
using namespace std;
bool prime(double input);


int main(){
	double input = 1;

	while(input > 0){
		bool primeNumber = 'true';
		cout << "Enter a number to test if it is prime: " << endl;
		cin >> input;

		primeNumber = prime(input);
		
		if(primeNumber){
			cout << "The number is prime." << endl;
		}
		else{
			cout << "The number is not prime." << endl;
		}
	}

	cout << "You have chose to exit the program." << endl;

	system("pause");
	return 0;
}

bool prime(double input)
{
	int intInput = input;
	double sRoot = sqrt(input);
	for(int i = 2; i <= sRoot; i++){
		if(intInput % i == 0){
			return false;
		}
	}
}



The program above runs with no problems. I know that the if statement on the top of the program is correct. But before getting it into this state, I had to modify my original conditional. It used to read if(primeNumber == 'true'). When I would run the program with that condition in the if statement, it will evaluate every prime number as "The number is not prime." Did I somehow create a double negative?

Thanks for anyones time,

jermainStain

PS - Is there anything that I could do to make my future posts more understandable? I apologize for the code not displaying indented. I was hoping there was a way for me to upload an image instead. Thanks again :)
Last edited on
Please use [ code ][ /code ] or "<>" button to make your post readable.
Thanks Hashimatsu!
Your post is nicely displayed, and the code presented properly (if you ask me). :)

I can not help you with your logical problem tho, sorry. Out of my league...

Hopefully someone else can :)
bool primeNumber = 'true';
You're trying to set primeNumber to a character literal, which would always be non-zero. true is a reserved word, so it should just be:
bool primeNumber = true;
However, that's irrelevant since you overlay primeNumber with the return value from prime().

You have a problem in your prime() function. You return false if the number is evenly divisible, but you never return true when you fall through the for loop.
Should be:
1
2
3
4
5
6
7
8
9
10
bool prime(double input)
{   int intInput = input;
    double sRoot = sqrt(input);
    for(int i = 2; i <= sRoot; i++)
    {  if(intInput % i == 0)
       { return false;
       }
    }
    return true;  // not evenly divisible
}







Thanks AbstractionAnon, this fixed my logical problem. Much appreciated!
Topic archived. No new replies allowed.