using user-defined function>>help plzzz

I want to write a program that finds whether the number is narcissistic or no

this is what I did , I dont know what's wrong with it


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

using namespace std;
bool isNaracissistic (int );
int main()
{
  int num;

  cout<<"Enter a positive integer: ";
  cin>>num;

if (isNaracissistic(num))
cout<<num<<" is Naracissistic";

else
cout<<num<<" is no Naracissistic";


  return 0;
}




 bool isNaracissistic(int N );
 {

    int N,temp1,temp2,digit,i=0;
    cin>>N;
     temp=N;

    while(temp!=0)
    {
        digit=temp%10;
       temp=temp/10;

      i++;
      }
      temp2=N;
while (temp2!=0)
{
 digit= temp2%10;
 temp2=temp2/10;
 sum=sum+pow(digit,i);

}
if (sum==N)
return true;

else
return false;


 }
Last edited on
Suggestion: get rid of the stray semi-colon in line 25
Change:bool isNaracissistic(int N );
To: bool isNaracissistic(int N )

Line 28: You're redefining N - how can the compiler tell which N you're using (The one you illegally re-defined or the one in the parameter list)?
Line 30: You did not define temp (perhaps you mixed up your temp1?)
Line 44: You didn't define either sum or pow() (perhaps #include <cmath> )
suggestion, change temp1 to temp in line 28 and add a definition for sum
Change: int N,temp1,temp2,digit,i=0;
To: int N,temp,temp2,digit,i=0, sum = 0;

That should get you through compilation, but not the logic.

Line 31 is pointless and unexpected. You've already obtained the number in main() and passed it to this function. At the very least, add a message like you did in line 9, so it doesn't look like your program hung.
suggestion: delete line 29

And also it's better you state what the problem is, i.e. what you expect and what you actually observe.
Oh I just noticed that I mixed up temp and temp1 !

my mistakes are really silly -.- sorry


Thanks alot
now it works :)



Topic archived. No new replies allowed.