I created a program based off this Project Euler problem.
https://projecteuler.net/problem=4
I managed to get my program to work, however it doesn't work the way I expect it to. I've had my teacher look over it and even he can't understand why it doesn't perform the way I expect it to. Perhaps the expert programmers here could help me figure out what's off.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
ostringstream convert;
string check, reversed;
const int largest3dignum = 999;
long currentnum;
long largestpalindromic;
for(int x=100; x<=largest3dignum; x++)
{
for(int y=100; y<=largest3dignum; y++)
{
currentnum=x*y;
convert << currentnum;
check = convert.str(); //converts the current number to string
reversed = string(check.rbegin(), check.rend()); //reverses string and stores in reversed
if(check==reversed) //if both strings are equal the current number becomes the largest palindrome
largestpalindromic = currentnum;
}
}
cout << "The largest palindrome is " << largestpalindromic << endl;
return 0;
}
As you see my primary method of identifying a palindrome is converting the current calculated value to a string, and the inverting that string (stored in another string variable) and compare both strings to see if they match. If so the number being calculated is named the largest palindrome until another is found.
I look forward to your responses.