Совершенно очевидно, что ошибка b
не инициализируется повторно до 0
внутри детали внутри метки xyz
.Следовательно, значения просто продолжают складываться вместо того, чтобы начинать отсчет для нового ввода.поэтому исправление будет следующим:
xyz:
b = 0;
Но рекомендуется не использовать goto
, так как это приводит к созданию запутанного кода и может привести к бесконечным циклам.См. Эту статью ниже:
Почему вы должны избегать goto
?
Использовать while
или do-while
вместо ... следующим образом:
#include <stdio.h>
#include <stdlib.h>
int main(){
int a, b=0, choice; //declared choice here
printf("Hey welcome to how many digits your number has!\n");
do {
b = 0;
printf("To begin enter a Number = ");
scanf("%d",&a);
while (a!=0) {
a/=10;
++b;
}
// removed declaration of choice from here and placed it along with other declarations in main()
printf("your number has %d digits\n",b);
printf("do you want to use another number?\nIf yes enter \"1\"\nif no enter \"2\"\n");
scanf("%d",&choice);
}while(choice == 1); //repeat the process again if user inputs choice as 1, else exit
return 0;
}