I can't fix/identify the errors with this program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 7
Reputation: ice_tea_lemon is an unknown quantity at this point 
Solved Threads: 0
ice_tea_lemon ice_tea_lemon is offline Offline
Newbie Poster

I can't fix/identify the errors with this program

 
0
  #1
Mar 15th, 2007
Hey all,

Erm given the problem

Write a program that will read in a line of text and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma, or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespace, commas, and periods. When outputting the number of letters that occur in a line, be sure to count upperand lowercase versions of a letter as the same letter. Output the letters in alphabetical order and list only those letters that do occur in the input line. For example, the input line
I say Hi.
should produce output similar to the following:
3 words
1 a
1 h
2 i
1 s
1 y

I've tried to write the program source code for this one but I've been alot of errors which I cant fix. If any of you can tell what's wrong with my program and fix it for me I'd really appreciate it. My source code thusfar is as follows:
  1. #include <iostream.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. using namespace std;
  5. int main()
  6. {
  7. int i,h[80]={0},words=1;
  8. char s[15];
  9. cout<<"Enter the String: ";
  10. gets(s);
  11. for(i=0;s!='\0';i++)
  12. {
  13. if(s==' ')
  14. words++;
  15. h[tolower(s)-'a']++;
  16. }
  17. cout<<"\n"<<words<<" words";
  18. for(i=0;i<26;i++)
  19. if(h!=0)
  20. cout<<"\n"<<h<<" "<<(char)(i+97);
  21. system ("pause");
  22. return 0;
  23. }
  24.  
  25. /*
  26. limitaitons
  27. - will give wrong output for a statement with more than one space between words
  28. - wrong output for leading and trailing spaces
  29. */
I seriously cannot understand what's wrong with this code. If you guys can fix it it'd be really nice. Also, please please please give me comments along the way, otherwise I'll get lost halfway through.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: I can't fix/identify the errors with this program

 
0
  #2
Mar 15th, 2007
OK, since you are new, allow me to correct things in your program first before tackling the coding of the problem. By the way, what's an erm?

Problem#1: Format your code! You need to learn to indent your code properly so it can be followed, and it's better to learn it now than unlearn a bad habit later. This includes spacing:
for(i=0;s!='\0';i++) vs
for (i = 0; s != '\0'; i++)

Problem#2: gets() -- just stop using it. Period. Forget it exists!

Problem#3: system ("pause"); -- see #2


Originally Posted by ice_tea_lemon View Post
limitaitons
- will give wrong output for a statement with more than one space between words
- wrong output for leading and trailing spaces
That's because all you are doing is counting spaces, not words. You need to decide what represents a word. Each time you see the beginning of a word, add one to the word count and set a flag that indicates you are in a word. Each time you see the ending of a word, set that same flag to indicate you are not in a word. A simple flag = 0; and flag = 1; (false=0 and true=1) would work fine for the flag. Now you have to figure out what constitutes the beginning and end of a word.

Since you already understand that characters are just values (very good for a new programmer!) you can simply set up your h array to be 127 instead of 80 and count every character that you see. It takes less code in your initial loop. Still use the tolower() function because it only works on letters. Then you have a count of all characters.

In your 2nd loop isn't h an array? If so, what does if (h != 0) do would you suppose?

And give h a better name. What is it's purpose? Create a name that indicates said purpose.


And your two posting problems:
Originally Posted by ice_tea_lemon View Post
I seriously cannot understand what's wrong with this code. If you guys can fix it it'd be really nice.
Yes it would, but it's not going to happen. We'll help you fix it though

Originally Posted by ice_tea_lemon View Post
Also, please please please give me comments along the way, otherwise I'll get lost halfway through.
Then you should have commented the code yourself... You can't ask us to do more than you are willing to do for yourself.

Please read the the Rules
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC