C char not matching \n with return key -
I'm running a program to read in a string in C. I have a strange problem in storing the string. My code is:
Zero matching (four * text) {char a, * p; Int b = 1; P = text; Scanf ("% c", & amp; a;); If (a == '\ n') {* p = '\ 0'; Return; } While (a! = '\ N') {* (p + b-1) = a; P = reel (p, size (four) * (b + 1)); B ++; Scanf ("% c", & amp; a;); } * (P + B) = '\ 0'; }
So it reads correctly in letters, but when it reaches the end of the string, then it does nothing, but instead of keeping the loop current. If I print a
as the last command in the loop, when it reaches the end of the string, it prints the blank space, but the program does not leave the loop.
You can use getchar ()
function Instead of scanf ()
. Unless the user accepts the string ENTER
. Please see ()
function
getting zero (four * text) {int a; Four * p; Int b = 1; P = text; A = getchar (); If (a == '\ n') {* p = '\ 0'; Return; } While (a! = '\ N') {* (p + b-1) = a; P = reel (p, size (four) * (b + 1)); B ++; A = getchar (); } * (P + B) = '\ 0'; }
Comments
Post a Comment