printf not doing its job

word counting code where printf wont display

Page 1 of 1

6 Replies - 694 Views - Last Post: 06 December 2009 - 11:53 PM Rate Topic: -----

#1 Ricky_CommonSense  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 22-March 09

printf not doing its job

Post icon  Posted 06 December 2009 - 09:55 AM

I have this word counting program...

#include <stdio.h>

#define IN 1	 
#define OUT 0

main()
{
	  int nw , nl , nc , c , state;
	 
	  state=OUT;
	  nl=nw=nc=0;
	  while((c=getchar()) != EOF)
	  {
	   ++nc;
	   if (c == '\n')
		  ++nl;
	   if (c == ' ' || c == '\n' || c == '\t')
			 state = OUT;
	   else if(state == OUT){
			 state = IN;
			 ++nw;
			 }   
	   } 
	  printf("%d %d %d\n", nl, nw, nc);
	  system("PAUSE");  
}


...the printf will work if I put it inside the while loop but won't work if I put it outside the while loop. And if its inside the whlie loop it gives me false values. Whats the deal here?

Is This A Good Question/Topic? 0
  • +

Replies To: printf not doing its job

#2 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

Re: printf not doing its job

Posted 06 December 2009 - 11:01 AM

This line
int nw=nl=nc=0;



Doesn't even compile for me.
Was This Post Helpful? 0
  • +
  • -

#3 marcof3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-December 09

Re: printf not doing its job

Posted 06 December 2009 - 11:39 AM

you are probably not stopping the program the right way.

if you are using windows try ctrl+z, ctrl+d on linux.

It seems to be working fine here.

ps: you probably want to start your nl with 1 instead of 0.
Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: printf not doing its job

Posted 06 December 2009 - 11:42 AM

It's ctrl + c for Windows, and yes, the program works just fine .
Was This Post Helpful? 0
  • +
  • -

#5 Ricky_CommonSense  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 22-March 09

Re: printf not doing its job

Posted 06 December 2009 - 07:31 PM

Hmm, thats strange. Still doesn't work, its probly my compiler.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_c.user*


Reputation:

Re: printf not doing its job

Posted 06 December 2009 - 10:45 PM

try in the console
echo abcd efgh hijklm | prog.exe
Was This Post Helpful? 0

#7 Mercurial  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 178
  • Joined: 06-November 09

Re: printf not doing its job

Posted 06 December 2009 - 11:53 PM

If its not working outside the loop, it means you're not getting outta while at all :P Make sure you enter EOF to get outta while at the end of the input. Try changing the condition to see if its about the loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1