I'm new to C/C++ and I'm doing this exercise in my course that requires a while loop, and this code supposedly counts characters without spaces. I'm really confused why this counts the spaces as well, because I used a similar syntax with fgets instead of getchar and it worked fine.
int main()
{
//method 2
char str[100];
int i = 0;
char c;
int count2 = 0;
printf("Enter characters.\n");
while (c = getchar() != '\n') {
if (c != ' ') {
count2++;
}
}
printf("There are %d characters\n", count2);
return 0;
}