wads wrong with my programe

Apr 27, 2009 at 1:29pm
can some 1 help me solve this error and warning.. this is the programe


#include <iostream>
#include <cstdlib>
using namespace std;

bool palindromes(long no);

int main ()
{
long num;

if (palindromes(num)==true)
{
cout<<num;
cout<<" is a palindrome.";
}

else
{

cout<<num;
cout<<" is not a palindrome.";
}

cout<<endl;
system("pause");
return 0;
}

bool palindromes(long no)
{
long num, quation, remainder,RN;

cout<<"Input a number to check if it is palindrome -> ";
cin>>num;

quation=num;
RN=0;
do
{
remainder=quation%10;
quation=quation/10;

RN=RN*10+remainder;
}

while (quation!=0);

if (num==RN)
{
return true;
}

else
{
return false;
}
}

i cant cout the correct number imputed
Apr 27, 2009 at 1:38pm
You have a problem with scope. The num in your palindromes function is not the same and the num in main, and you never initialize num in main.
Apr 27, 2009 at 1:40pm
how to initialize num in the main .. can u help me
Apr 27, 2009 at 1:47pm
Heres' a short bit on scope of variables: http://www.cplusplus.com/doc/tutorial/functions/

You could use pass by reference, so that your function modifies the same variable. You could also move
1
2
cout<<"Input a number to check if it is palindrome -> ";
cin>>num;

into main and then pass the value to the function.
Apr 27, 2009 at 1:53pm
can u teach me how to pass the value to the function as i was nt taught b4
Apr 27, 2009 at 2:06pm
Again, I will refer you to the Functions tutorial. http://www.cplusplus.com/doc/tutorial/functions/
It has explanations and examples of parameter usage, which is how data are passed to functions.
Apr 27, 2009 at 2:11pm
oic is this example right ???

#include <iostream>
using namespace std;

void duplicate (int& a, int& b, int& c)
{
a*=2;
b*=2;
c*=2;
}

int main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}
Apr 27, 2009 at 2:16pm
Yes, that is a good example of pass by reference. Using that technique, you should be able to rewrite your original program so that it does exactly what you want.
Topic archived. No new replies allowed.