•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 430,108 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,163 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 2844 | Replies: 3
![]() |
•
•
Join Date: Aug 2004
Posts: 24
Reputation:
Rep Power: 5
Solved Threads: 0
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?
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?
if(c == (SPACE || '\n'))
{
if(c == SPACE)
space++;
if(c == '\n')
newline++;
}
else
other++;
}if ( c == SPACE ) space++; else if ( c == '\n' ) newline++; else other++;
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
api apple asm assembly x86 programming hla demo blogger blogging c c++ ccna cocoa code coding competition compiler compilers computer debugging developer development errors evaluation framework gdata google high-performance innovation java languages linerider mcse microsystems networking news next object online open oriented planning platform practices programming python rss software step steps sun tools tutorials xml
- "The Sims" visual C++ runtime error, what shall i do? (Windows Software)
- Programming Question Using PHP Language (PHP)
- Assembly Programming Question (Assembly)
- Encoding/Decoding (C)
- Sockets fundamentals (Java)
- Writing a Hangman game (C++)
Other Threads in the C Forum
- Previous Thread: access Windows Address Book
- Next Thread: correct errors in this program



Linear Mode