I know it probably looks pretty bad but im very new to c++ here is the prompt:
Given a number, reverse its digits and add the resulting number to the original number. If the result isn't a palindrome, repeat the process. I thought my logic was pretty good but for some reason my output isnt coming out right. please help..thanks
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <locale>
#include <sstream>
#include <string>
#include <cstdlib>
#include <list>
#include <algorithm>
usingnamespace std;
void palin(std::string a);
bool check_the_result(string a);
int main()
{
string number;
cout<<"enter the number:"<<endl;
cin>>number;
int numm = 0;
int length = number.length();
int Num = length - 1;
palin(number);
}
void palin(std::string a)
{
int length = a.length();
int b = length-1;
int origNUM = atoi(a.c_str());
for(int begin = 0; begin<length/2;begin++,b--)
{
std::swap(a[begin],a[b]);
}
int newNum = atoi(a.c_str());
int newNUM = newNum + origNUM;//number to be converted to string
std::string Result; // string which will contain the result
ostringstream convert; // stream used for the conversion
convert <<newNUM; // insert the textual representation of 'Number' in the charactes in the stream
Result = convert.str(); // set 'Result' to the contents of the stream
//now we have the added number from the two previous numbers stored in "RESULT"
cout<<Result<<endl;
check_the_result(Result);
if(check_the_result==false)
{
palin(Result);
}
else
{
cout<<Result<<endl;
}
}
bool check_the_result(string a)
{
int length = a.length();
int counter = 0;
int b = length - 1;
while(counter<length/2)
{
if(a[counter]!=a[b])
{
returnfalse;
break;
}
else
{
counter++; b--;
returntrue;
}
}
}
My first suggestion is insure your compiler is generating the maximum number of warnings and never ignore these warnings.
Here are the warnings my compiler reports about your code:
main.cpp||In function ‘int main()’:|
main.cpp|22|warning: unused variable ‘numm’ [-Wunused-variable]|
main.cpp|24|warning: unused variable ‘Num’ [-Wunused-variable]|
main.cpp||In function ‘void palin(std::string)’:|
main.cpp|62|warning: the address of ‘bool check_the_result(std::string)’ will never be NULL [-Waddress]|
main.cpp||In function ‘bool check_the_result(std::string)’:|
main.cpp|98|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build finished: 0 errors, 4 warnings (0 minutes, 1 seconds) ===|
thanks for responding..
im using an online compiler so thats may be why i wasnt getting those warnings..thanks for showing them to me. Im working on them now