Need help with Palindrome program

Hi, I've started the euler.net problems and am currently working on problem 4, which states "Find the largest palindrome made from the product of two 3-digit numbers" I've created what look to be a successful function for both turning doubles into strings, and then comparing elements of those strings in order to discover whether the string is a palindrome.

However, the output simply isn't correct. Curious to figure out what the real answer was i looked it up and sure enough my program wasn't even close. When i plug in the correct answer into my 2 functions (but leaving it out of my nested for-loops) It indeed knows that it's a palindrome and outputs it correctly, which leads me to believe that the problem needs to be with the loops. If you guys could provide some input into why this is it would be greatly appreciated, feel free to criticize any part of my code, even things unrelated to my question.

Heres my 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Find the largest palindrome made from the product of two 3-digit numbers.

#include <iostream>
#include <string>
#include <cstring>
#include <boost/lexical_cast.hpp>
using namespace std;

string convert_double(double);
bool checkPalindrome(string);

int main(){

double num= 0;
string strNum;
bool drome = false;

for(int i=999; i>99; i--)
	for(int j=999; j>99; j--){
		num = (i*j);
		if(checkPalindrome((convert_double(num)))){
			drome= true;
			cout<< num;
		if(drome = true)
			return 0;
	}
	}



	return 0;
}

string convert_double(double num){

string str = boost::lexical_cast<string>(num);

return str;

}

bool checkPalindrome(string str){
	bool ans = true;
	int size = str.length();

	for(int i=0; i<size; i++)
		if(str[i]!= str[size-1-i])
			ans = false;
	
	return ans;
}
Line 24 doesn't do what you think it does. Look carefully.
Also, mind your blocks and your indentation. The if on line 21 encompasses the one on line 24.

There's a thread here where we covered that problem in-depth. You might want to check it out later: http://www.cplusplus.com/forum/beginner/75152/
Last edited on
going to bed now so i'll mess more with in the morning, but could you be a little less cryptic, what exactly is going on there, and how do I fix it?
= is the assignment operator. The check always evaluates to true. You need ==, the equality operator.
Why do you need this...
1
2
if(drome = true)
	return 0;

No need of this you are doing return 0 after for loop..
please
Alright, I got ride of that if statement since it was unnecessary. But my program is still outputting a palindrome that is far lower then the actual answer! Anyone have any ideas on that?
The first palindrome you find by iterating backwards is not necessarily the largest one in the range.
I didn't think of that, I'll rework it so it goes through all options and gives the highest.
Topic archived. No new replies allowed.