Hi,
If I want to imput a string, but it has spaces in it, what function do I have to use to "scan" it, if i use:
scanf ("%s",string);
it only "scans" the first word of the sentence...
Thanks in advance
Hi,
If I want to imput a string, but it has spaces in it, what function do I have to use to "scan" it, if i use:
scanf ("%s",string);
it only "scans" the first word of the sentence...
Thanks in advance
gets() and fgets() will do what you want.
gets() is dangerous to use (and your compiler will probably warn you so) but you can take a look at it for learning purposes.
fgets() is much more secure. You can read about it here in the FAQ
Maybe I didn't understand correctly, but doesn't fgets get strings from a file?
Is there a way to use it to get a string from the keyboard?
use stdin for FILE *stream to receive input from the standard input device (normally the keyboard).PHP Code:
char *fgets(char *s, int size, FILE *stream);
PHP Code:
fgets(buf, sizeof(buf), stdin);
stdin, stdout, and stderr are all special streams that are available to use with fgets(). so if you do:
then it will grab input from the standard input device (usually the keyboard).Code:fgets(buf, sizeof(buf), stdin);
If you understand what you're doing, you're not learning anything.
ok, thanks...
I added that, but now it doesn't let me enter the string, and it gives it the value of "ÿw", I think it might be stdin, any ideas?
Here's some demo code to show how it works. This compiles and executes like it should...
PHP Code:
#include <stdio.h>
int main()
{
char sentence[80];
printf ("Enter a line of text\n");
fgets(sentence, sizeof(sentence), stdin);
printf ("Your sentence is:\n%s\n", sentence);
return 0;
}
Last edited by Scribbler; 10-20-2004 at 11:53 PM.
ok... now I'm just stumped...
here's what I have... well part of it
And it just skips movie name!!Code:switch (a) { case 1: printf("\n1.- Add a movie\n"); printf("2.- Delete a movie\n"); scanf ("%d", &b); switch (b) { case 1: printf("\nEnter movie name: "); fgets(movnam, sizeof(movnam), stdin); printf("\nEnter Director: "); scanf("%s", director); printf("\nEnter Year: "); scanf("%d", &y); printf("\nEnter Format: "); scanf("%s", format); sprintf(query,"insert into movie values ('%s','%s',%d,'%s')", movnam, director, y, format); if(mysql_query(connection,query) != 0) //execute the query printf("Error Executing the query:\n%s\n\n",mysql_error(connection)); else printf("New Movie Added\n"); break; break;
for some strange reason, and I can't find the mistake... if I put case 1: (the second one), into another file and compile it, it works fine!...
Thanks for all the help..
Your scanf() call is leaving a \n on the input buffer which is forcing fgets() to return immediately (it's acting like the user saw the movie name prompt and just pressed ENTER).
If you understand what you're doing, you're not learning anything.
Thanks!, I never would have been able to figure that out..
How can I solve this?
Last edited by m23oose; 10-21-2004 at 12:58 PM.
If you understand what you're doing, you're not learning anything.
It works fine now... thanks!!
In case you're not raiding the wealth of information in the FAQ by yourself, these two might also be beneficial to take a peek at.
- FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
- FAQ > How do I... (Level 1) > How do I get a number from the user (C)
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.*