| | |
I can't fix/identify the errors with this program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2007
Posts: 7
Reputation:
Solved Threads: 0
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:
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.
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:
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <stdio.h> #include <ctype.h> using namespace std; int main() { int i,h[80]={0},words=1; char s[15]; cout<<"Enter the String: "; gets(s); for(i=0;s!='\0';i++) { if(s==' ') words++; h[tolower(s)-'a']++; } cout<<"\n"<<words<<" words"; for(i=0;i<26;i++) if(h!=0) cout<<"\n"<<h<<" "<<(char)(i+97); system ("pause"); return 0; } /* limitaitons - will give wrong output for a statement with more than one space between words - wrong output for leading and trailing spaces */
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:
Problem#2: gets() -- just stop using it. Period. Forget it exists!
Problem#3: system ("pause"); -- see #2
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
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
And give h a better name. What is it's purpose? Create a name that indicates said purpose.
And your two posting problems:
Yes it would, but it's not going to happen. We'll help you fix it though
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

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++) vsfor (i = 0; s != '\0'; i++)Problem#2: gets() -- just stop using it. Period. Forget it exists!
Problem#3: system ("pause"); -- see #2
•
•
•
•
limitaitons
- will give wrong output for a statement with more than one space between words
- wrong output for leading and trailing spaces
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:
•
•
•
•
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.

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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- CHKDSK help - drive is dirty and I cannot fix any errors (Windows NT / 2000 / XP)
- Deitel's "C++ How To Program" exercise 2.18 (C++)
- Please help fix this lexer program (C++)
- errors make me close program (C#)
- Simple Programming Errors (Computer Science)
- Can I safely fix 02BHO links? (Viruses, Spyware and other Nasties)
- Application Launch Error (OS X)
Other Threads in the C++ Forum
- Previous Thread: problem!!!
- Next Thread: help me......
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






