dont now how to make(function call)

Apr 28, 2014 at 3:28pm
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"








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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
 #include<iostream>
using namespace 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";


	}




help me plzzzzz =(
Apr 28, 2014 at 3:55pm
-Function implementations must come before main function, otherwise their prototypes.
-line 16 must be written like this: sPalindrome=sPalindrome();

For the palindrome, I will suggest you use: std::string with std::reverse(); included in the <algorithm> header.

eg:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <algorithm>
#include <iostream>

using namespace std;

bool checkPalindrome(string str){
  string tmp=str;
  reverse(tmp.begin(),tmp.end());
  if(
  if(str==tmp){
    return true;
  }
  return false;
}


Aceix.
Last edited on Apr 28, 2014 at 10:20pm
Apr 28, 2014 at 4:00pm
thank you very much
but i have error in line 11 i don't know how to write it =(
Apr 28, 2014 at 4:02pm
sPalindrome=bool sPalindrome ;

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



Last edited on Apr 28, 2014 at 4:10pm
Apr 28, 2014 at 4:13pm
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.

Aceix.
Apr 28, 2014 at 4:22pm
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
#include <algorithm>
#include <iostream>
using namespace 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()))
  {
    return true;
  }
  return false;
}
Apr 28, 2014 at 4:42pm
Line 5 makes no sense. You're trying to initialize a variable to itself.

You still haven't provided function prototypes for length and checkPalindrome.

Line 13 makes no sense. Again assigning a variable to itself. Did you mean to call your length function?

Line 18 makes no sense. Again assigning a variable to itself. Did you mean to call your checkPalindrome function?

Line 44: reverse returns nothing (void). You can't assign it to a string.

Apr 28, 2014 at 4:48pm
Visit: http://www.cplusplus.com/doc/tutorial/functions/

Compare your's with this:

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
#include<iostream>
#include <algorithm>

using namespace 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;
}


EDIT: Don't forget the <string> header

Aceix.
Last edited on Apr 28, 2014 at 4:53pm
Apr 28, 2014 at 5:55pm
Aceix
thank you very muchhh <3
Apr 28, 2014 at 6:08pm
AbstractionAnon
Line 5 makes no sense. You're trying to initialize a variable to itself.

You still haven't provided function prototypes for length and checkPalindrome.

Line 13 makes no sense. Again assigning a variable to itself. Did you mean to call your length function?

Line 18 makes no sense. Again assigning a variable to itself. Did you mean to call your checkPalindrome function?

Line 44: reverse returns nothing (void). You can't assign it to a string.


----------------

yaah this is my problem i was taring to call the function but i don't know how
Topic archived. No new replies allowed.