Thread: scanf and blank input.

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    79

    scanf and blank input.

    I'm currently using scanf to read in numbers and then to use CTRL+D to signal to the program EOF... but how could I say put in numbers "2 34 23 22 19" and then when I press "ENTER", it automatically knows to stop scanning in numbers?

    I need to somehow make this program stop if there is 'empty input' and spit out an error message...

    Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The input should look like:
    "2 34 23 22 19\n"
    So read number. Read whitespace. Repeat until the whitespace is == '\n'.
    That should get you started.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Yeah okay, but scanf doesn't read in white spaces unless if you use %c I think...

    and that would be a pain too if I used %c instead of %d because it would also read in spaces...

    Am I missing something?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Who says you can't read a number first with %d and then a whitespace with %c? %c isn't a string, btw, it is a character. So it will read one and one character only. Not a string.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Okay,

    atm I have this

    Code:
       scanf("%c",&a);
       while(scanf("%d", &num[i])!=EOF && a!='\n') {
          n++;
          i++;
       }
    the idea being that if I just press "ENTER" it will show an error that no input was entered...

    However, the problem with this is that it removes the first number if I enter numbers seperated with spaces.

    What would be a better approach to this?

    Thanks

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How about...
    Code:
    int num;
    do
    {
        if (scanf("%d", &num) == 1)
        {
            // Read succeeded; do your logic.
        }
    }
    while (getchar() != '\n');
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Thanks for that... it is ingenious... but it still doesn't work if I just press "enter" without typing anything

    Would you have an alternative method now?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Annoying sscanf. Won't advance the buffer. Hah.
    This had to call for some more advanced parsing:

    Code:
    // Test.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        long num;
        char c;
        char buf[1024];
        char * pEnd;
        char * pStart = buf;
        fgets(buf, sizeof(buf), stdin);
    
        do
        {
            num = strtol(pStart, &pEnd, 10);
            if (pStart != pEnd)
            {
                // Read succeeded; do your logic.
                printf("YAY!");
            }
            pStart = pEnd;
            c = *pStart++;
        }
        while (c != '\n');
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    >>I need to somehow make this program stop if there is 'empty input' and spit out an error message...
    Then you would need something else.
    Do you want 12 23 10 as valid input or just one number per line?
    And you should read scanf man.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Blank Char
    By godingly in forum C Programming
    Replies: 3
    Last Post: 03-03-2010, 10:07 AM
  2. Replies: 2
    Last Post: 12-30-2009, 09:23 AM
  3. scanf question
    By panfilero in forum C Programming
    Replies: 1
    Last Post: 11-21-2005, 07:22 AM
  4. scanf problem
    By gsoft in forum C Programming
    Replies: 3
    Last Post: 01-05-2005, 12:42 AM
  5. 2 Q: about vars, and scanf
    By Nutka in forum C Programming
    Replies: 2
    Last Post: 10-17-2002, 06:43 PM