I wanted to use:
number being declared as char number[15].Code:if((*(number+i)=='\n'))
It doesn't work though and I can't understand why.
Using:
works but it doesn't do what I need.Code:if((*(number+i)-'\n'))
I wanted to use:
number being declared as char number[15].Code:if((*(number+i)=='\n'))
It doesn't work though and I can't understand why.
Using:
works but it doesn't do what I need.Code:if((*(number+i)-'\n'))
> if((*(number+i)=='\n'))
If this is true
> if((*(number+i)-'\n'))
Then this will be false (the result of the expression being zero).
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I probably should have explained better. What I meant with "it doesn't work" was that when I simply run it, then it won't go any further than this check, but it doesn't hang either.
When I compile it for debugging in DevC++ and then run step-by-step then pressing next step does nothing. It gets stuck for some reason.
My guess is that the address (number+i) may be going out of bounds, ensure that (i >=0 && i < 15)
Post more code, like the whole function, and a 1-line main which calls that function with whatever parameters make it fail.
If the function reads user input, then comment out those lines and replace with data,
so
scanf("%s",input);
becomes
char input[100] = "hello";
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
That's it! I had i++ before do-while while statement that checked for *(number+i)!='\n' but whenever the loop reached \n it replaced it with \0 then incremented i and checked whether next character in a string is \n. But there is only one \n in that string. Seems I was dealing with infinite loop .