letter and word counter

Reply

Join Date: Dec 2004
Posts: 1
Reputation: chriswell is an unknown quantity at this point 
Solved Threads: 0
chriswell chriswell is offline Offline
Newbie Poster

letter and word counter

 
0
  #1
Dec 15th, 2004
i need to write a program that can count the number words the user enters and and the recurences of each letter.

ex: Have a nice day
Total words: 4
a = 3
d = 1
e = 2
...

Can't get the program i've writen so far to count the number of words.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;


int main()
{

char letters[] = "abcdefghijklmnopqrstuvwxyz";
int freq[sizeof letters - 1] = {0};
char text[60];
char input;
int numWords=0;
bool inword = false;
cout<<"Enter a line of text: \n";
cin>>input;
while((input=cin.get()) !='\n')
{ // Counts the number of words in the text.
if(input == ' ' || input== '\t')
{
inword = false;
}
else if (inword == false)
{
inword = true;
numWords++;
}

cout<<"The number of words in the text is: "<<numWords<<endl;

if ( cin.getline ( text, sizeof text ) )
{
for ( int i = 0; text[i] != '\0'; i++ )
{// Converts all letters to lower case.
if ( isalpha ( text[i] ) )
++freq[tolower ( text[i] ) - 'a'];
}

for ( int i = 0; letters[i] != '\0'; i++ )
{// Counts the frequency of all letters.
if ( freq[i] != 0 )
cout<< letters[i] <<": "<< freq[i] <<endl;
}
}

}
return 0;
}

thanks...
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: letter and word counter

 
0
  #2
Dec 15th, 2004
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++

Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: letter and word counter

 
0
  #3
Dec 15th, 2004
sizeof letters: needs brackets around letters, but that could be the absence of code tags (use [xCODE]paste code here[x/CODE] --> remove x's wen u post: i put that in so u could see the tags!!)
when initialiseing the frequency table you would need to set every element to zero to be sure, you have only set the first. using a loop is a much better idea.

have you tried cout'ing input back to the console to check the RIGHT chars are going in?? i have been through the loop a few times in my head and have tested it like so

  1. #include <iostream.h>
  2.  
  3. int main(void)
  4. {
  5.  
  6. bool inword = 0;
  7. unsigned int numwords = 0;
  8. char *str = "this is a test string \t \n"; // 5 words....
  9. char letter; // comparison letter
  10.  
  11. while (letter = *str++)
  12. {
  13. if(letter == ' ' || letter == '\t' || letter == '\n')
  14. {
  15. inword = 0;
  16. }
  17. else if(inword == 0)
  18. {
  19. inword = 1;
  20. numwords++;
  21. }
  22. }
  23. cout << "Found " << numwords << " words!\n"; // it works!!
  24. return 0;
  25. }

This counter works, so your code should... using this code will get you the number of words, see if you can get it working and post back with any results. hope this helps
Last edited by 1o0oBhP; Dec 15th, 2004 at 1:27 pm. Reason: Making Code tag visible
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,306
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 227
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: letter and word counter

 
0
  #4
Dec 15th, 2004
Originally Posted by 1o0oBhP
sizeof letters: needs brackets around letters
No, you don't. You need parentheses for a type, but not an object.

Originally Posted by 1o0oBhP
when initialiseing the frequency table you would need to set every element to zero to be sure, you have only set the first. using a loop is a much better idea.
Wrong again. The remainder of the array is also initialized to zero. Using a loop is not a better idea.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: letter and word counter

 
0
  #5
Dec 15th, 2004
The remainder of the array is also initialized to zero. Using a loop is not a better idea
Sorry its just a habit. Have done it with character strings and had junk data in each one. Playing it safe.

You need parentheses for a type, but not an object.
News to me, i always use brackets, then i dont forget. #

BTW: I tried your code chriswell, the cin.get() line seems to miss the first word (if you cout the input). I thought if you cout input you should get the same line you entered in, but when i tried it failed. It seems this is the critical flaw.
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,306
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 227
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: letter and word counter

 
0
  #6
Dec 15th, 2004
Originally Posted by chriswell
i need to write a program that can count the number words the user enters and and the recurences of each letter.
What you had was mostly there, you just had some things misplaced and tried to get user input too many times.
#include <iostream>
#include <cctype>
using namespace std;

int main()
{

   char letters[] = "abcdefghijklmnopqrstuvwxyz";
   int  freq[sizeof letters - 1] = {0};
   char text[60];
   int  numWords = 0;
   bool inword = false;
   cout<<"Enter a line of text: \n";
   if ( cin.getline ( text, sizeof text ) )
   {
      for ( int i = 0; text[i] != '\0'; i++ )
      {// Converts all letters to lower case.
         if ( isalpha ( text[i] ) )
         {
            ++freq[tolower ( text[i] ) - 'a'];
         }
         // Counts the number of words in the text.
         if ( isspace(text[i]) )
         {
            inword = false;
         }
         else if ( inword == false )
         {
            inword = true;
            numWords++;
         }
      }
      for ( int i = 0; letters[i] != '\0'; i++ )
      {// Counts the frequency of all letters.
         if ( freq[i] != 0 )
            cout<< letters[i] <<": "<< freq[i] <<endl;
      }
   }
   cout<<"The number of words in the text is: "<<numWords<<endl;
   return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,306
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 227
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: letter and word counter

 
0
  #7
Dec 15th, 2004
Originally Posted by 1o0oBhP
Have done it with character strings and had junk data in each one. Playing it safe.
I doubt you can show me an example. Unitialized arrays will "contain junk". Initialized arrays are different.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: letter and word counter

 
0
  #8
Dec 15th, 2004
something like this

  1. char text[256] = "1o0oBhP";

Most of the time its ok and the rest is blank. Occasionally its not and causes errors when you use strcat and other string functions. I recently had the same problem on a small log project using mainly fstream and strcat. Playing it safe. I realise numericals are initialised zero but i have seen in the past cout giving scientific numbers. Ive dealt with enough random errors that i always make sure everything is concrete.
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,306
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 227
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: letter and word counter

 
0
  #9
Dec 15th, 2004
Originally Posted by 1o0oBhP
Most of the time its ok and the rest is blank. Occasionally its not and causes errors when you use strcat and other string functions.
I would suspect the use of strcat, not the initialization.

Originally Posted by 1o0oBhP
Playing it safe. I realise numericals are initialised zero but i have seen in the past cout giving scientific numbers. Ive dealt with enough random errors that i always make sure everything is concrete.
Learn to capitalize on these errors and find the real reasons. Otherwise you may be burying bugs deeper.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: letter and word counter

 
0
  #10
Dec 15th, 2004
fair enough. Im used to Programming in VB (6 years and before i learned c++) where you can observe variables at run time (ie wats in the string). however my c++ IDE doesnt.... so i dont know what is going on i have to make certain nothing can go wrong
Do you use Dev-C++? I have 4.9.9.0 and if anyone knows how can i observe vaiables at run time??
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC