Hello. I am trying to accept a paragraph from the user in C using codeblocks. But, with the scanf statement, the program stops accepting once i hit space. Is there some way to get around this problem?

Recommended Answers

All 2 Replies

use fgets() instead of scanf() so that all the words get put into the string.

int main()
{
   char line[100];
   fgets(line, sizeof(line), stdin);
}

I completely agree with using fgets() over scanf()

sigh... I'm bored and there's a lack of new articles for me to post so here's a workaround for scanf that uses character classes

int main(void)
{
    char string[100];
    scanf("%[a-zA-Z0-9+-*/=%.,?!:; ]s", string);
    printf("%s\n", string);
return 0;
}

NOTE: It won't be as effective as fgets though ;)

Be a part of the DaniWeb community

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