help with my C++ program.

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

Join Date: Feb 2007
Posts: 4
Reputation: bosanac515 is an unknown quantity at this point 
Solved Threads: 0
bosanac515 bosanac515 is offline Offline
Newbie Poster

help with my C++ program.

 
0
  #1
Feb 25th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: help with my C++ program.

 
0
  #2
Feb 25th, 2007
You haven't told us what you're having trouble with. Do you get compiler errors, runtime errors, what?

And by the way:
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with my C++ program.

 
0
  #3
Feb 25th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 4
Reputation: bosanac515 is an unknown quantity at this point 
Solved Threads: 0
bosanac515 bosanac515 is offline Offline
Newbie Poster

Re: help with my C++ program.

 
0
  #4
Feb 25th, 2007
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)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with my C++ program.

 
0
  #5
Feb 25th, 2007
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 4
Reputation: bosanac515 is an unknown quantity at this point 
Solved Threads: 0
bosanac515 bosanac515 is offline Offline
Newbie Poster

Re: help with my C++ program.

 
0
  #6
Feb 25th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: help with my C++ program.

 
0
  #7
Feb 25th, 2007
>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.
  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:
  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...

  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...
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with my C++ program.

 
0
  #8
Feb 26th, 2007
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: help with my C++ program.

 
0
  #9
Feb 26th, 2007
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.
  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:
  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...

  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...
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
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