0

"Run-Time Check Failure #3 - The variable 'sum' is being used without being initialized."

How can I fix this error?

#include <math.h>
#include <stdio.h>
int perfect_in_pow(int num) {
    int nn = 0;  // THE NUMBER OF THE DIGITS IN THE NUMBER
    int temp;  // USED TO SEPERATE A DIGIT FROM THE ENTIRE NUMBER
    int temp2; // USED TO CALCULATE TO NUMBER OF TIMES THAT WE NEED TO ADD 1 TO "nn"
    int temp3; // USED TO SAVE THE ORIGINAL ENTERED NUMBER'S VALUE
    int power; // USED TO CALCULATE EACH DIGIT TO THE POWER OF "nn"
    int sum;   // THE SUMMERY OF THE "power" CALCULATION FOR EACH DIGIT
    int ii;    // USED IN THE FOR LOOP
    temp2 = num;
    temp3 = num;
    do {    // USED TO CALCULATE THE NUMBER OF THE DIGITS IN THE NUMBER
        nn += 1;
        temp2 = temp2 / 10;
    } while (temp2 > 0);
    while (num>0) {    // USED TO CALCULATE THE SUMMERY 
        for (ii = 0; ii<nn; ii++) {
            temp = num % 10;
            power = pow(temp, nn);
            sum += power;
            num = num / 10;
        }
    }
    if (temp3 == sum) {
        return 1;
    }
    else {
        return 0;
    }
}

void main() {
    int num;
    printf("Enter a number ");
    scanf("%d", &num);
    printf("%d", perfect_in_pow(num));
}

Any assistance will be appriciated. Thanks.

1 Answers1

2

You have to initialize the variable "sum".You have only declared it. You should learn about initialization and declaration.

The thing is the compiler doesn't know how to add the "power" to "sum" variable when sum has not any value defined. what will you do? when some one will ask you to add 1000 with some value you don't know.

in your case just do this:

int sum=0;

That'll be it

Rajnish
  • 123
  • 8