So I have a function that takes address of a variable and puts number in there(I need this function to make all checks on input as my teacher loves to put something like
123 ABV00000012
when it's asked to put just a number). The problem is that when number is negative it somehow changes address of my variable and gives me just zero(1-st screenshot). When I put positive number it works just fine(2-nd screenshot) and I find something else, when I put something too low like -1111111111111111111111 it gives me error that number is too big or too low, then we have to put number again and now it works just fine with non-positive numbers(3-rd screenshot). Also when put something too big 11111111111111111111111 it still don't work with non-positive numbers(4-th screenshot) Code is below. https://i.stack.imgur.com/eudyC.jpg (<- screenshots, sry can't attach screenshots in here yet)
#include <stdio.h>
int putInt(int*);
int main()
{
int a = 1;
printf("a address = %p\n", &a);
while(!putInt(&a))
{
printf("Error! %d - %p\n", a, &a);
}
printf("A == %d == %p\n", a, &a);
printf("%p\n", &a);
return 0;
}
int putInt(int *a)
{
char isMin = 0, isZero = 0, isSpc = 0, isNum = 'a', isEnt[2];
isEnt[0] = 0;
int num = 123;
scanf("%1[\040]", &isSpc);
if(isSpc == ' ')
{
printf("Incorrect input! No spaces allowed!\n");
rewind(stdin);
return 0;
}
scanf("%1[-]", &isMin);
scanf("%1[\n]", isEnt);
if(*isEnt == '\n')
{
if(isMin == '-')
{
printf("Incorrect input!\n");
rewind(stdin);
return 0;
}
else
{
printf("Number is not entered!\n");
rewind(stdin);
return 0;
}
}
scanf("%1[\040]", &isSpc);
if(isSpc == ' ')
{
printf("Incorrect input! No spaces allowed!\n");
rewind(stdin);
return 0;
}
scanf("%1[0]", &isZero);
scanf("%*[0]");
scanf("%1[\n]", isEnt);
if(*isEnt == '\n')
{
*a = 0;
rewind(stdin);
return 1;
}
scanf("%10d", &num);
scanf("%1[0-9]", &isNum);
if(isNum != 'a')
{
printf("Incorrect input! Too big or too low number!\n");
rewind(stdin);
return 0;
}
scanf("%1[\040]", &isSpc);
if(isSpc == ' ')
{
printf("Incorrect input! No spaces allowed!\n");
rewind(stdin);
return 0;
}
scanf("%1[\n]", isEnt);
printf("Num is %d\n", num);
if(*isEnt == '\n' && num > 0)
{
if(isMin == '-') num = num * -1;
*a = num;
printf("a = %d = %p\n", *a, a);
rewind(stdin);
return 1;
}
printf("Error! Too big or too low number!\n");
rewind(stdin);
return 0;
}