Hello there, its been a long time for a new post. This time I am here with the
Line Feed (ASCII value 10) and
input problem in C/C++ compilers. I faced this problem tonight and could not find where was the
bug!
Really I was feeling guilty, Oh! I cannot taking input a string...
Okay, now the problem is sometimes we need to take input an intejar followed a
string(can be a string / empty line / only
contains space). For this type of problem "scanf" followed
"gets" dosent works.
Why?
Now there is a ascii character (usually called "new line") "Line Feed" ('\n') ascii value 10, which caused the problem.
Now there is a ascii character (usually called "new line") "Line Feed" ('\n') ascii value 10, which caused the problem.
How to solve?
Take input the intejar followed by a False or Dummy input such as
scanf("%d%c", &Tc, &Fake); // Tc is intejar type and Fake is char type
Take input the intejar followed by a False or Dummy input such as
scanf("%d%c", &Tc, &Fake); // Tc is intejar type and Fake is char type
Note:
* "scanf" takes input without leading and trailing spaces.
* "scanf" takes input without leading and trailing spaces.
* "gets" takes the whole line until new line entered.
* "getline" does the same.
* "gets", previously used "scanf" dosent works. Better take
a dummy or fake character input with "scanf"
to get rid of Line Feed. (Discussed above)
* "gets" only takes character array.
* While taking input a single character with "scanf", its better to use a space before "%c"
like scanf(" %c ", &ch); // ch is character type.
* "gets" only takes character array.
* While taking input a single character with "scanf", its better to use a space before "%c"
like scanf(" %c ", &ch); // ch is character type.
Thanks a lot. Please comment bellow if you want to add something or make
correction.
Enjoy Safe Computing.
No comments:
Post a Comment