February 11th, 2013, 10:42 AM
Input taking spaces by default
In the below program when am reading input from keyboard its taking only 2 characters instead of 4 and remaining 2 characters its taking spaces by default.
why is it???
// reversestring.cpp : Defines the entry point for the console application.
/*program to take char input through pointers*/
int _tmain(int argc, _TCHAR* argv)
printf("enter the size of char arry:");
revstring = (char *)malloc(arrysize * sizeof(*revstring));
printf("enter the array elements:");
February 11th, 2013, 12:31 PM
This will just print the size of a pointer, not the amount of memory it points to (which you already know).
This will typically read the \n left behind by you previous scanf("%d" call.
February 11th, 2013, 12:58 PM
That means do you want me to take the input through getchar()
February 11th, 2013, 01:14 PM
Reading every line using fgets(), then parsing it however you want is generally a good way to go.
The code might be a few lines longer(*), but the outcome is far more predictable.
(*) actually, by the time you've added all the error checking to simple scanf calls, the amount of code is pretty much the same.
Comments on this post