I'm making a program that accept 1 character long strings (and ignores spaces " ") until a user presses return/enter.

So far I have,

int main(){
    
    char expression[1];

    while ((scanf("%s",expression)) != EOF){

    //code

    }

    return 0;
}

it is assumed that the user is entering single digit strings (such as "a 1 2 3 d"(enter))


its seems to not like the while condition as it will keep accepting input even after I hit enter. I have tried using -1 instead of EOF but its the same output.

Any ideas?

Recommended Answers

I'll wait to see what the other people reading this thread have to say before giving you a good solution. :)

Jump to Post

Number one I would change your expression to:

char expression;

and then call a %c in your scanf.

#include <stdio.h>

int main()
{
    
    char expression;

    while ((scanf("%c",&expression)) != EOF)
    {
      fprintf(stdout, "char->%c\n", expression);
    }

    return 0;
}
Jump to Post

It may be worthwhile to note that scanf ignores blank input, newline, and tab. (in the context you are using it)

What I mean by this, is that blank input is still input, not EOF.

Jump to Post

All 9 Replies

I'll wait to see what the other people reading this thread have to say before giving you a good solution. :)

Number one I would change your expression to:

char expression;

and then call a %c in your scanf.

#include <stdio.h>

int main()
{
    
    char expression;

    while ((scanf("%c",&expression)) != EOF)
    {
      fprintf(stdout, "char->%c\n", expression);
    }

    return 0;
}

I'll wait to see what the other people reading this thread have to say before giving you a good solution. :)

is it a quick fix or a major overhaul?

Number one I would change your expression to:

char expression;

and then call a %c in your scanf.

#include <stdio.h>

int main()
{
    
    char expression;

    while ((scanf("%c",&expression)) != EOF)
    {
      fprintf(stdout, "char->%c\n", expression);
    }

    return 0;
}

wow i feel really stupid right now (forgetting the & and what not).
But i need the while loop to exit when enter is selected. What you offered just loops after the enter is pressed.

It may be worthwhile to note that scanf ignores blank input, newline, and tab. (in the context you are using it)

What I mean by this, is that blank input is still input, not EOF.

Why scanf ? Are you familiar with getchar ? It's better suited for your needs. With getchar , it's as simple as

while(c = getchar() != '\n')

Also, if I remember correctly, using a char to check for EOF is not going to work, for char is an unsigned data type, while EOF is a negative one.

>it is assumed that the user is entering single digit strings (such as "a 1 2 3 d"(enter))
If all you want is single character input, you don't even need scanf. Just use getchar and test for whitespace manually:

int ch;

while ((ch = getchar()) != '\n' && ch != EOF) {
    if (isspace(ch))
        continue;

    printf("Processing '%c'\n", ch);
}

However, I have yet to see such a program that doesn't grow into more than single character words. In that case, scanf can be used without any problems provided the assumption holds that whitespace or end-of-file terminates each word.

One problem in this case is that scanf doesn't differentiate between newlines and other whitespace. It's all the same to scanf. But because scanf stops reading at whitespace, you can read the next character and see which one it was:

#include <stdio.h>

int main(void)
{
    char word[512];
    char next;

    while (scanf("%511s%c", word, &next) >= 1) {
        printf(">%s<\n", word);

        if (next == '\n')
            break;
    }

    return 0;
}

Thanks a lot guys! I don't specifically need to use scanf (I just thought it was useful that it skipped spaces). This cuts a lot of debugging out of my schedule.

I'm making a program that accept 1 character long strings (and ignores spaces " ") until a user presses return/enter.

So far I have,

int main(){
    
    char expression[1];

    while ((scanf("%s",expression)) != EOF){

    //code

    }

    return 0;
}

it is assumed that the user is entering single digit strings (such as "a 1 2 3 d"(enter))


its seems to not like the while condition as it will keep accepting input even after I hit enter. I have tried using -1 instead of EOF but its the same output.

Any ideas?

I don't think anyone mentioned the potential for buffer overflow in the original program...That's why I mentioned changing the variable from a character array to a single character.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.