943,962 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1452
  • C++ RSS
Feb 25th, 2007
0

help with my C++ program.

Expand Post »
this the actual assigment:
Write a program that will read in a line of text and output the number of words in the line and the number of occurences of each letter. Define a word that will be any string of letters that is delimited at each end by either a 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 uppercase and lowercase versions of a letter as the same letter. Output the letters in alphabetical order and list only those letters that occur in the inoput 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

========================================
this is what I go to far:
  1. #include <iostream.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <iomanip.h>
  5.  
  6. const int sizeArray = 100;
  7.  
  8. int word(int, int, int);
  9. int letter();
  10.  
  11.  
  12.  
  13. void main()
  14. {
  15. cout << "NAME: "<< endl;
  16. cout << "Course: CPSC241"<<endl;
  17. cout << "Assignment: A4Q1" << endl;
  18. cout << endl;
  19.  
  20. }
  21.  
  22. int word(int countword, int morewords, int yetmorewords)
  23. {
  24.  
  25. int countword = 1;
  26. for (int i = 0; i < sizeArray; i++) //Checks the number of words on the first line.
  27. {
  28. if (i == ' ')
  29. countword++;
  30. else if (i == '.') //These characters indicate the end of a word
  31. countword++d;
  32. else if (i == ',')
  33. countword++;
  34. }
  35.  
  36. if (i == ' ')
  37. countword++;
  38. else if (i == '.') //These characters indicate the end of a word
  39. countword++d;
  40. else if (i == ',')
  41. countword++;
  42. }
any help would be greatly appreciated. or if anyone has a solution to this program please advise. THANK you very much!!!!
Last edited by Ancient Dragon; Feb 25th, 2007 at 9:50 pm. Reason: add code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bosanac515 is offline Offline
4 posts
since Feb 2007
Feb 25th, 2007
0

Re: help with my C++ program.

You haven't told us what you're having trouble with. Do you get compiler errors, runtime errors, what?

And by the way:
  • Use code tags
  • Use iostream, not iostream.h, etc.
    C++ Syntax (Toggle Plain Text)
    1. #include <iostream>
    2. using namespace std;
  • Use int main() instead of void main()
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 25th, 2007
0

Re: help with my C++ program.

What are your question(s) ? We are not going to do you assignment for you but will gladly try to answer your questions. Do the assignment one small step at a time. For example, start out with the first requirement read in a line of text Is it supposed to read from the keyboard or a data file? If you use getline() function then the syntax will be pretty much the same whether reading from a file or the keyboard. So get that part going correctly before attempting to write any more of the program.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Feb 25th, 2007
0

Re: help with my C++ program.

thank you very much for your quick reply, I get the following compiller errors: Compiling...
A4Q1.cpp
C:\Documents and Settings\Owner\Desktop\C++ test\A4Q1.cpp(32) : error C2082: redefinition of formal parameter 'countword'
C:\Documents and Settings\Owner\Desktop\C++ test\A4Q1.cpp(38) : error C2146: syntax error : missing ';' before identifier 'd'
C:\Documents and Settings\Owner\Desktop\C++ test\A4Q1.cpp(38) : error C2065: 'd' : undeclared identifier
C:\Documents and Settings\Owner\Desktop\C++ test\A4Q1.cpp(39) : error C2181: illegal else without matching if
C:\Documents and Settings\Owner\Desktop\C++ test\A4Q1.cpp(46) : error C2146: syntax error : missing ';' before identifier 'd'
C:\Documents and Settings\Owner\Desktop\C++ test\A4Q1.cpp(47) : error C2181: illegal else without matching if
Error executing cl.exe.

A4Q1.obj - 6 error(s), 0 warning(s)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bosanac515 is offline Offline
4 posts
since Feb 2007
Feb 25th, 2007
0

Re: help with my C++ program.

>>error C2082: redefinition of formal parameter 'countword'

you have named a local variable the same name as a parameter -- that's a no-no. Use a different variable name.

Also look at lines 31 and 39 -- they are incorrect (extraneous 'd' at the end after the last '+').
Last edited by Ancient Dragon; Feb 25th, 2007 at 10:08 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Feb 25th, 2007
0

Re: help with my C++ program.

I am sorry, I did not get what code tags are so I enclosed my whole code with [].
if I use int main I do not get errors on the bottom.
ok this is what I changed and got coulple errors:
  1. #include <iostream>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <iomanip.h>
  5.  
  6. const int sizeArray = 100;
  7.  
  8. int word(int countword, int moreword, int yetevenmorewords);
  9. int letter();
  10.  
  11.  
  12.  
  13.  
  14.  
  15. int main()
  16. {
  17. cout << "NAME: "<< endl;
  18. cout << "Course: CPSC241"<<endl;
  19. cout << "Assignment: A4Q1" << endl;
  20. cout << endl;
  21.  
  22.  
  23.  
  24. int word(int countword, int morewords, int yetmorewords)
  25. {
  26.  
  27. int countword = 1;
  28. for (int i = 0; i < sizeArray; i++) //Checks the number of words on the first line.
  29. {
  30. if (i == ' ')
  31. countword++;
  32. else if (i == '.') //These characters indicate the end of a word
  33. countword++;
  34. else if (i == ',')
  35. countword++;
  36. }
  37.  
  38. if (i == ' ')
  39. countword++;
  40. else if (i == '.') //These characters indicate the end of a word
  41. countword++;
  42. else if (i == ',')
  43. countword++;
  44. }
  45. }
  46. ]
Compiling...
A4Q1.cpp
C:\Documents and Settings\Owner\Desktop\C++ test\A4Q1.cpp(30) : error C2601: 'word' : local function definitions are illegal
C:\Documents and Settings\Owner\Desktop\C++ test\A4Q1.cpp(50) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.

A4Q1.obj - 1 error(s), 1 warning(s)
]
Last edited by Ancient Dragon; Feb 26th, 2007 at 12:10 am. Reason: added code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bosanac515 is offline Offline
4 posts
since Feb 2007
Feb 25th, 2007
0

Re: help with my C++ program.

>I did not get what code tags are
Put [code] in front of your code, and [/code] at the end of the snippet.

>ok this is what I changed and got coulple errors
The reason that you're getting the first error is because you forgot a closing brace } at the end of main().

A few other things:

Your headers need a little bit of fixing.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstring>
  3. #include <iomanip>
  4. using namespace std;
Any particular reason why you're using the ctype.h header?

In your word() function:
C++ Syntax (Toggle Plain Text)
  1. int countword = 1;
This line redeclares a parameter. Remove int if you simply want to reset the variable, although it kind of destroys the purpose of the parameter...

C++ Syntax (Toggle Plain Text)
  1. for (int i = 0; i < sizeArray; i++)
You declare int here, and then try to use it later on. The problem is that 'i' only stays in scope for this one loop. So to use it later on in this function, you're going to have to declare 'i' before the loop.

Not to mention that you totally forgot to return a value in that function...
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 26th, 2007
0

Re: help with my C++ program.

>>error C2601: 'word' : local function definitions are illegal

That means you tried to put function word() inside function main(). You have to finish one function before you can start another one.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Feb 26th, 2007
0

Re: help with my C++ program.

OK, I'm having MAJOR post editing problems, so I guess I'll just repost the section that I intended to add on to my last post... (I can't see my post edit, so I'll assume that no one else can either)

A few other things:

Your headers need a little bit of fixing.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstring>
  3. #include <iomanip>
  4. using namespace std;
Any particular reason why you're using the ctype.h header? (and since you're using C++, it might be a better idea to use cctype instead of ctype.h)

In your word() function:
C++ Syntax (Toggle Plain Text)
  1. int countword = 1;
This line redeclares a parameter. Remove int if you simply want to reset the variable, although it kind of destroys the purpose of the parameter...

C++ Syntax (Toggle Plain Text)
  1. for (int i = 0; i < sizeArray; i++)
You declare int here, and then try to use it later on. The problem is that 'i' only stays in scope for this one loop. So to use it later on in this function, you're going to have to declare 'i' before the loop.

Not to mention that you totally forgot to return a value in that function...
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Array and File Help
Next Thread in C++ Forum Timeline: Passing command line variable into already running c++ program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC