2

So here's my code:

main()
{
        char *iochar;                                                                                   

        printf(" Enter a standard line: ");                                                             
        scanf ( "%s", &iochar);

        if (iochar != NULL)
        {
                printf(" Here's what you entered: %s ", &iochar);
        }
        else
        {
                printf(" Oops! Looks like you forgot to enter something! ");
        }
}

My problem is that I can scan the user entry and store it and if something exists it puts out the correct message. But if I just hit return to test for no input (null, empty, etc) the program neither quits nor outputs my error message. It just hangs until I input something.

I've been programming in Java for the last two semesters so C is totally lost on me right now. So any help would be much appreciated.

8
  • so you just want to validate whether input is a string or not? Commented Feb 22, 2017 at 18:37
  • 4
    Don't use scanf (ever). Use fgets, or, if you have it, getline. Then "just hitting return" will produce the string "\n" and typing control-D (which forces an EOF condition) will produce the string "". Incidentally, the spaces at the ends of your second and third printf strings should be \n instead. Commented Feb 22, 2017 at 18:37
  • @OusmaneDiaw At this point, yes. My code has to do other things, but I have to be able to validate that there is in fact some sort of input. Commented Feb 22, 2017 at 18:38
  • 2
    Where to begin? No memory allocation for char *iochar;. Not checking the function return value from scanf. Using & for string arguments to scanf and printf... I suggest the man pages for functions you use, and enabling full compiler warnings. Commented Feb 22, 2017 at 18:39
  • 1
    The %s conversion specification for scanf() skips white space, including newlines and blanks and tabs, and doesn't start saving anything until it comes across a non-white-space character. If you care about newlines, you can't use scanf() because it doesn't care about newlines (unless you work very hard — so hard that it isn't worth doing). If you care about lines, use a line-based input function (e.g. fgets()) and then maybe sscanf() to parse the line that it reads. Commented Feb 22, 2017 at 19:02

2 Answers 2

2

scanf ( "%s", &iochar); is not useful for detecting an empty line.

"%s" first directs scanf() to read and discard all leading white-space, including '\n' before proceeding to read non-white-space characters. So code has lost the '\n'.


Instead use fgets()

// Enough room for 80 characters + \n + \0
#define LINE_SIZE (80 + 1 + 1)
char buf[LINE_SIZE];

if (fgets(buf, sizeof buf, stdin) == NULL) {
  puts("end-of-file or input error detected");
} else if (buf[0] == '\n') {
  puts("Empty line entered");
} else {
  printf("Input line: <%s>\n", buf);
}

Input

Hello World (and <Enter>)

Output (Note the \n read in is still retained)

Input line: <Hello World
>

To be clear: In C, a string is a sequence of characters up to and including a terminating null character '\0'. Users do not enter strings. User input is usually a line of text up to and including a terminating line feed '\n'. fgets() reads a line of input and then appends a null character to the resultant buffer is a string`.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh my gosh thanks so much! It works like a dream now! Haha.
0

Here's my advice: Don't use scanf. Its rules are confusing, and it's very difficult to get the behaviour you want. Instead, I recommend you use getch to manually read each character into a buffer until the user enters a newline character '\n'. Then you can zero terminate the buffer with a 0, and then use the buffer as a normal string. For it to be empty, just check the length of the string with strlen(x).

2 Comments

You know of fgets?
Yes, but I prefer to not use standard library functions that I can implement myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.