Hey i am trying to print each word on a new line. My EOF is also not working and was wondering why this is. i have made it to scan for a space and then print new line.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char ch;
while ((ch = getchar()) != '#')
putchar(ch);
int nextChar;
nextChar = getchar();
while (nextChar != '\n' && nextChar != EOF);
{
if (ch== ' ')
{
printf("\n");
}
else
{
putchar(ch);
}
{
ch = getchar();
}
printf("\n");
{
scanf("%lc",&nextChar);
printf("%c",nextChar);
}
return 0;
}
}
just for example input: Stackoverflow is great
output:
Stackoverflow
is
great
You should REALLY start enabling compiler warnings. They can help you find many bugs. Look here when I compile with -Wall
and -Wextra
.
$ gcc ba.c -Wall -Wextra
ba.c: In function ‘main’:
ba.c:13:5: warning: this ‘while’ clause does not guard... [-Wmisleading-indentation]
while (nextChar != '\n' && nextChar != EOF);
^~~~~
ba.c:14:5: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘while’
{
^
Remove the ;
after the while loop.
But there are other problems too. As you can see from when I corrected the indentation for you, the return 0
statement is inside the while loop. I assume that's not what you want.