Count character/word/line program

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 853 Views - Last Post: 08 June 2013 - 11:56 PM Rate Topic: -----

#1 Zach786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 07-June 13

Count character/word/line program

Posted 07 June 2013 - 08:17 PM

Basically the program is suppose to output amount of characters, amount of word and amount of newlines that have been typed. Multiple consecutive spaces should not be counted as multiple words. I am getting error c2064: Term doesn't evaluate to a function taking 305 arguments. Any ideas?
#include <stdio.h>
#include <ctype.h>

void main ()
{
	char c=0;
	int countchar=0;
	int countline=0;
	int countword=0;

	while((c=getchar()) !=EOF&& (c!=26))
	{
		if ((c!=' ') && (c!='\n'))

			++countchar;

		if (c=='\n')
			++countline;

		else if ((isalpha(c)!=0) ((c ==' ')||(c!='\n')))
		++countword;
	}
	
	printf("Number of characters: %d\n",countchar);
	printf("Number of countline: %d\n",countline);
	printf("Number of countword: %\n",countline);

}


Is This A Good Question/Topic? 0
  • +

Replies To: Count character/word/line program

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3556
  • View blog
  • Posts: 11,027
  • Joined: 05-May 12

Re: Count character/word/line program

Posted 07 June 2013 - 08:24 PM

Take a closer look at your line 20.
Was This Post Helpful? 0
  • +
  • -

#3 Zach786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 07-June 13

Re: Count character/word/line program

Posted 07 June 2013 - 08:32 PM

Missing && operate. works now expect program doesn't count words and line correctly. how can I fix the code?
Attached Image
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3556
  • View blog
  • Posts: 11,027
  • Joined: 05-May 12

Re: Count character/word/line program

Posted 07 June 2013 - 08:33 PM

Have you even traced through the code to come up with a theory as to why it is not working correctly?
Was This Post Helpful? 0
  • +
  • -

#5 Zach786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 07-June 13

Re: Count character/word/line program

Posted 07 June 2013 - 08:41 PM

Newline variable works as expected.Yes, I have tried to figure out why it's not working. Isalpha is function looking for alphabetic character and it will exclude the values that are false (!=0) and the word should have space (c==10) or should have enter next to word c==\n) to qualify as statement.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3556
  • View blog
  • Posts: 11,027
  • Joined: 05-May 12

Re: Count character/word/line program

Posted 07 June 2013 - 08:54 PM

Did you also notice line 26?
Was This Post Helpful? 0
  • +
  • -

#7 Zach786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 07-June 13

Re: Count character/word/line program

Posted 07 June 2013 - 08:59 PM

my mistake. I changed that and ran the program. It still doesn't get anything for word count.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3556
  • View blog
  • Posts: 11,027
  • Joined: 05-May 12

Re: Count character/word/line program

Posted 07 June 2013 - 10:24 PM

Show us your new code with your fixes. I suspect that sticking in the && on line 20 has set up condition that will never be true.
Was This Post Helpful? 0
  • +
  • -

#9 Zach786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 07-June 13

Re: Count character/word/line program

Posted 07 June 2013 - 10:26 PM

Here is the new code
#include <stdio.h>
#include <ctype.h>

void main ()
{
	char c=0;
	int countchar=0;
	int countline=0;
	int countword=0;

	while((c=getchar()) !=EOF&& (c!=26))
	{
		if ((c!=' ') && (c!='\n'))

			++countchar;

		if (c=='\n')
			++countline;

		else if ((isalpha(c)!=0)&&((c ==' ')||(c!='\n')));
		++countword;
	}
	
	printf("Number of characters: %d\n",countchar);
	printf("Number of countline: %d\n",countline);
	printf("Number of countword: %\n",countword);

}

Was This Post Helpful? 0
  • +
  • -

#10 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: Count character/word/line program

Posted 08 June 2013 - 12:19 AM

You should use int main() instead of void main() as the C++ standard requires. Also, there are functions like isspace, isdigit and isalpha which I think they would make your life easier ^_^
And why char c = 0;
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3556
  • View blog
  • Posts: 11,027
  • Joined: 05-May 12

Re: Count character/word/line program

Posted 08 June 2013 - 08:52 AM

Look closely at line 20 again. What is that semi-colon doing there?
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon


Reputation: 4027
  • View blog
  • Posts: 12,434
  • Joined: 25-December 09

Re: Count character/word/line program

Posted 08 June 2013 - 09:04 AM

Also I notice a couple of issues in the following snippet:
	char c=0;
...

	while((c=getchar()) !=EOF&& (c!=26))

First if you want to detect EOF c should be an int, not a char, remember getchar() returns an int, not a char. The value of EOF is not guaranteed to fit into a char, in C/C++ a char can be either a signed or an unsigned integral value.

Next, what is the purpose of the (c != 26)?

What is the significance of that "magic number" 26?

Jim

This post has been edited by jimblumberg: 08 June 2013 - 09:06 AM

Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is offline

  • member icon


Reputation: 4027
  • View blog
  • Posts: 12,434
  • Joined: 25-December 09

Re: Count character/word/line program

Posted 08 June 2013 - 01:31 PM

Also posted here.

Jim
Was This Post Helpful? 1
  • +
  • -

#14 Zach786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 07-June 13

Re: Count character/word/line program

Posted 08 June 2013 - 02:36 PM

I am still not getting word count. I did change c variable to integer. !=26 basically signifies as end of line character.
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is offline

  • member icon


Reputation: 4027
  • View blog
  • Posts: 12,434
  • Joined: 25-December 09

Re: Count character/word/line program

Posted 08 June 2013 - 03:45 PM

Quote

!=26 basically signifies as end of line character


Really? On what operating system?

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2