Hello,
I started learning C about 2 days ago, and now I have come to a problem that I can't figure out why I am getting the result I am getting. I bought a book, and I have proceeded quickly through it until now.
The problem I was given was this: "Write a program that reads input until encountering the # character and then reports the number of spaces read, the number of new line characters read, and the number of all other characters read."
Here is the source code I came up with:
#include <stdio.h>
#define SPACE ' '
int main(void)
{
char c, prev;
int space, newline;
long other;
space = 0;
newline = 0;
other = 0L;
printf("Enter text to be analyzed (# to terminate):\n");
while((c = getchar()) != '#')
{
if(c == (SPACE || '\n'))
{
if(c == SPACE)
space++;
if(c == '\n')
newline++;
}
else
other++;
}
printf("There are %d spaces, %d newline characters, and %d other characters.\n",
space, newline, other);
getchar();
getchar();
return 0;
}
Most everything works fine, except when I type Brent [ENTER] #, I get 6 other characters instead of just 5, one for each letter of Brent. I believe it might have something to do with the [ENTER] but I am not sure. Anyone have any ideas?