hello !
i have problem in function call i try to do this program
A. Write a value-returning function called length() that takes a positive integer and
returns the number of digits in that integer.
B. Using the written function in part A, write a value-returning function called
isPalindrome() that checks if a positive integer is a palindrome or not. A number is
palindrome if it reads the same left to right and right to left. For example 23432 is a
palindrome, while 5675 is not palindrome. The function takes a positive integer and
returns true if the integer is a palindrome and false otherwise.
Test your functions by writing a main function that prompts the user to input an integer
and outputs its number of digits and "It Is a Palindrome" message if it is a palindrome,
otherwise "It Is not a Palindrome"
#include<iostream>
usingnamespace std ;
int main (){
int n , num , sPalindrome ,length, number ;
cout<<"enter an enteger";
cin >> n ;
num=length(number);
cout<<"the number of digets is : "<< num <<endl ;
sPalindrome=bool sPalindrome ;
cout<<"It Is a Palindrome" ;
cout<<"It Is not Palindrome" ;
return 0;
}
int length(int number)
{
if (number < 10) {
return 1;
}
int count = 0;
while (number > 0) {
number /= 10;
count++;
}
return count;
}
bool sPalindrome()
{
int i,temp,d,revrs=0;
cout<<"enter the number to check :";
cin>>i;
temp=i;
while(temp>0)
{
d=temp%10;
temp/=10;
revrs=revrs*10+d;
}
if(revrs==i)
cout<<i<<" is palindorme";
else
cout<<i<<" is not palindrome";
}
First thing you need to do is not make a variable with the exact same name as the function. This will end up confusing anyone reading your code, including yourself.
Second a function call ends with () parenthesis which contain any parameters that are specified by the declaration of the function. If none just leave the parenthesis empty.
Thirdly do not include a return type in a call to the function, the compiler gets confused and thinks you are declaring a new function.
bool isPalindr = sPalindrome() ;
looks like this.
Lastly you need a declaration of the function before the call in main. bool sPalindrome(); // <-- a declaration
As CodeGoggles said, you would want to make the variable have a different name as the function.
shaikha wrote:
but i have error in line 11 i don't know how to write it =(
Check to see if the length function is in scope(visible to the compiler before line 11). Also you could post the updated code if your problem remains unsolved.
#include<iostream>
#include <algorithm>
#include <iostream>
usingnamespace std ;
bool sPalindrome=sPalindrome ;
int main (){
int n , num , sPalindrome ,length, number ;
cout<<"enter an enteger";
cin >> n ;
length=length;
cout<<"the number of digets is : "<< num <<endl ;
sPalindrome= sPalindrome ;
cout<<"It Is a Palindrome" ;
cout<<"It Is not Palindrome" ;
return 0;
}
int length(int number)
{
if (number < 10) {
return 1;
}
int count = 0;
while (number > 0) {
number /= 10;
count++;
}
return count;
}
bool checkPalindrome(string str){
if(str==reverse(str.begin(),str.end()))
{
returntrue;
}
returnfalse;
}
#include<iostream>
#include <algorithm>
usingnamespace std ;
//these are what I meant by function prototypes/signatures
bool checkPalindrome(string);
int length(int);
int main () {
int n,len;
cout<<"enter an enteger";
cin >> n;
len=length(n);
cout<<"the number of digits is: "<< len <<endl ;
if(checkPalindrome(to_string(n))) //requires C++11 compatible compiler for to_string() function
cout<<"It Is a Palindrome";
else
cout<<"It Is not Palindrome";
return 0;
}