strings

This is a discussion on strings within the C Programming forums, part of the General Programming Boards category; Hi, If I want to imput a string, but it has spaces in it, what function do I have to ...

  1. #1
    tweedle beetle
    Join Date
    Oct 2004
    Posts
    7

    strings

    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

  2. #2
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    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

  3. #3
    tweedle beetle
    Join Date
    Oct 2004
    Posts
    7
    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?

  4. #4
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    PHP Code:
    char *fgets(char *sint sizeFILE *stream); 
    use stdin for FILE *stream to receive input from the standard input device (normally the keyboard).

    PHP Code:
    fgets(bufsizeof(buf), stdin); 

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    stdin, stdout, and stderr are all special streams that are available to use with fgets(). so if you do:
    Code:
    fgets(buf, sizeof(buf), stdin);
    then it will grab input from the standard input device (usually the keyboard).
    If you understand what you're doing, you're not learning anything.

  6. #6
    tweedle beetle
    Join Date
    Oct 2004
    Posts
    7
    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?

  7. #7
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    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(sentencesizeof(sentence), stdin);

        
    printf ("Your sentence is:\n%s\n"sentence);

        return 
    0;


    Last edited by Scribbler; 10-21-2004 at 12:53 AM.

  8. #8
    tweedle beetle
    Join Date
    Oct 2004
    Posts
    7
    ok... now I'm just stumped...

    here's what I have... well part of it
    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;
    And it just skips movie name!!

    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..

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    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.

  10. #10
    tweedle beetle
    Join Date
    Oct 2004
    Posts
    7
    Thanks!, I never would have been able to figure that out..

    How can I solve this?
    Last edited by m23oose; 10-21-2004 at 01:58 PM.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    If you understand what you're doing, you're not learning anything.

  12. #12
    tweedle beetle
    Join Date
    Oct 2004
    Posts
    7
    It works fine now... thanks!!

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 03:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21