Results 1 to 5 of 5

Thread: [SOLVED] (C) scanf sucks!

  1. #1
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    [SOLVED] (C) scanf sucks!

    I need to get an input in a console application. Normally, this would be a simple task with scanf (I know scanf is old, but I'm learning in my University, and they teach us that, looks like later we will use iostream). So my problem begins that I need to ask for a name, last name and phone number, but that name should contain spaces.

    Normally, I use the following line:
    scanf("%[^\n]", &myvariable);
    But under ubuntu this doesn't work, so I started looking for other functions, or workarounds:

    gets(&myvariable);
    gets(); works nice, but for some reason, I have three gets' and the first is skipped, even if I use "fflush(stdin);".

    So my question, what's the best way to eat an empty space from the input? either windows or linux with C.

    Thanks in advance.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  2. #2
    Join Date
    Aug 2005
    Location
    san francisco
    Beans
    Hidden!
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: (C) scanf sucks!

    Hmm...

    Code:
    #include <stdio.h>
    
    int main(void)
    {
            char buffer[128];
    
            if(scanf("%[^\n]", buffer) == 1)
                    printf("I read: \"%s\"", buffer);
            return 0;
    }
    works fine for me when I enter input containing whitespace and press Enter. Are you sure it "doesn't work"?

  3. #3
    Join Date
    Dec 2006
    Location
    Australia
    Beans
    1,097
    Distro
    Xubuntu 15.10 Wily Werewolf

    Re: (C) scanf sucks!

    My gets() has no problem with spaces.

    For example, this works fine with spaces:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char firstname[30];
    	char lastname[30];
    
    	printf("Hi!  What is your first name? ");
    	gets(firstname);					// First gets.
    
    	printf("...and last name? ");
    	gets(lastname);						// Second gets.
    
    	printf("Hello, %s %s!\n", firstname, lastname);
    
    	return 0;
    }

  4. #4
    Join Date
    Oct 2005
    Location
    UK
    Beans
    242

    Re: (C) scanf sucks!

    Don't use gets. Quote from the libc manual
    - Deprecated function: char * gets (char *S)

    ...

    *Warning:* The `gets' function is *very dangerous* because it
    provides no protection against overflowing the string S. The GNU
    library includes it for compatibility only. You should *always*
    use `fgets' or `getline' instead. To remind you of this, the
    linker (if using GNU `ld') will issue a warning whenever you use
    `gets'.
    You should pay attention to warnings. In most cases, the compiler/linker knows best.
    Code:
    /tmp/ccSm8rHi.o: In function `main':
    test.c:(.text+0x24): warning: the `gets' function is dangerous and should not be used.

  5. #5
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: (C) scanf sucks!

    Thank you for all the replies:

    public_void:
    Yes, I know is unsafe to use that function. But in this assignment, this doesn't matter.

    For everyone else:
    I discovered what the issue was. I was using "fflush(stdin);" to clear the buffer, but apparently, this didn't. For example:

    The user comes from a menu screen where he selected a number and hit enter.

    Case 1: fflush(stdin);
    fflush(stdin);
    printf("Nombre del contacto (%d):\n", last);
    scanf("%[^\n]", mainlist->name);
    This, doesn't work at all. Instead of doing anything, it just jumps through out all the code.

    Case 2: Using a getchar();
    buffdump = getchar();
    printf("Nombre del contacto (%d):\n", last);
    scanf("%[^\n]", mainlist->name);
    Works surprisingly good! Looks like I'm gonna stick to getchar, create a special function that eats the \n.



    And I also wonder, why gets receives a warning, since scanf sucks twice as much. It leaves the linefeed around in the buffer to screw the next input.
    Last edited by Can+~; November 9th, 2007 at 10:08 PM.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •