User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Aug 2004
Posts: 24
Reputation: Brent_Ritterbec is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Brent_Ritterbec Brent_Ritterbec is offline Offline
Newbie Poster

C programming question

  #1  
Sep 1st, 2004
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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2004
Posts: 24
Reputation: Brent_Ritterbec is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Brent_Ritterbec Brent_Ritterbec is offline Offline
Newbie Poster

Re: C programming question

  #2  
Sep 1st, 2004
Looks like the code in my post didn't quite get formatted correctly when I copied my code over to the forum. Does anyone know how to simply copy what I have in my IDE, or do I have to do all the indentations again?
Reply With Quote  
Join Date: Apr 2004
Posts: 3,658
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 145
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C programming question

  #3  
Sep 1st, 2004
if(c == (SPACE || '\n'))
         {
                 if(c == SPACE)
                                 space++;
                 if(c == '\n')
                                 newline++;
         }
         else
         other++;
         }
The highligted code likely is not what you intend: it is a logical OR of ' ' with '\n' -- which results in true, a value of 1 (not the character '1', BTW). So you will only increment space or newline if c is equal to 1. Compare and contrast with this.
	  if ( c == SPACE )
 		 space++;
 	  else if ( c == '\n' )
 		 newline++;
 	  else
 		 other++;
Reply With Quote  
Join Date: Aug 2004
Posts: 24
Reputation: Brent_Ritterbec is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Brent_Ritterbec Brent_Ritterbec is offline Offline
Newbie Poster

Re: C programming question

  #4  
Sep 1st, 2004
Thank you very much. This helps a lot. It also shows me how to write more compact code, which I figure I will need when I start writing real software.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 3:06 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC