Is it possible to read a name like Max Payne with scanf using a single array of characters? Thanks.
Is it possible to read a name like Max Payne with scanf using a single array of characters? Thanks.
scanf("%s%s", maxVar, payneVar);
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
I knew that. I said I wanted to do it using a single array of characters. I know I can use fgets and the like but I'm just curious
That's how I would do it.Code:char name[80], temp[80]; scanf("%s%s", name, temp); strcat(name, " "); strcat(name, temp);
scanf("%s%s"
Never user %s (or %[) without specifying a width.
http://www.eskimo.com/~scs/C-faq/q12.20.html
[edit]Do you mean to keep the space, like this?Is it possible to read a name like Max Payne with scanf using a single array of characters?Code:#include <stdio.h> #include <string.h> int main(void) { char name[80]; if ( scanf("%79[^\n]", name) == 1 ) { printf("name = \"%s\"\n", name); } return 0; } /* my output Max Payne name = "Max Payne" */
Last edited by Dave_Sinkula; 06-23-2005 at 04:26 PM.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*