unidentified problem with stl
I got the following 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
|
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
//checks whether the given string palindrome or not
bool is_palindrome(const string& str)
{
return equal(str.begin(), str.end(), str.rbegin());
}
//converts decimal integer to base B
char nums[]={"0123456789ABCDEFGHIJ"};
string to_base(int b, int num)
{
int n=num, q, r;
string nbase;
while(q!=0){
q=n/b; r=n%b;
nbase.push_back(nums[r]);
n=q;
}
reverse(nbase.begin(), nbase.end());
return nbase;
}
int main()
{
if(is_palindrome(to_base(10, 2*2))) cout<<to_base(10, 2);
getchar();
}
| |
th problem is:
|
if(is_palindrome(to_base(10, 2*2))) cout<<to_base(10, 2);
| |
program does not print output 4;
but if only use the
cout<<to_base(10, 2);
it prints 4;
or if I use
if(is_palindrome(to_base(10, 2*2))) cout<<"PALINDROME";
it prints to stndrt output
CAN ANYONE EXPLAIN ME WHY IT DOES NOT WORK? WHERE IS MY BUG? TO THOSE WHO FIND MY BUG BIG THANKS!!!
Last edited on
You're missing #include <string>
Also, q
in to_base
is uninitialised, making while(q!=0)
non-deterministic.
THANKS kbw VERY VERY VERY MUCH! for q!!!!!!!!!!!!
Topic archived. No new replies allowed.