Проблема в том, что вы думаете, cont
- единственная переменная.
Правда в том, что у вас есть две cont
переменные, и единственное, что они разделяют, это одно и то же имя.Это две разные переменные с уникальными адресами.
Одна принадлежит основной функции, другая принадлежит функции perfectNumber.
Как насчет возврата этой уникальной переменной cont
?
#include <stdio.h>
char perfectNumber(int userInput) {
int divisor = 0;
int i;
int totalSum = 0;
char cont;
for (i = 1; i < userInput; i++) {
divisor = userInput % i;
if (divisor == 0) {
totalSum = totalSum + i;
}
}
if (totalSum == userInput) {
printf("Number %d is perfect\n", userInput);
}
else {
printf("Number %d is not perfect\n", userInput);
}
printf("Do you want to continue (y/n)? ");
scanf(" %c", &cont);
return cont;
}
int main(void) {
int userInput;
char cont = 'y';
while (cont == 'y' || cont == 'Y') {
printf("Enter a perfect number: ");
scanf("%d", &userInput);
cont = perfectNumber(userInput);
}
printf("Goodbye\n");
return(0);
}
Обратите внимание, что вам не хватает #include guard, я добавил его.