| | |
help with my C++ program.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2007
Posts: 4
Reputation:
Solved Threads: 0
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:
any help would be greatly appreciated. or if anyone has a solution to this program please advise. THANK you very much!!!!
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:
c Syntax (Toggle Plain Text)
#include <iostream.h> #include <string.h> #include <ctype.h> #include <iomanip.h> const int sizeArray = 100; int word(int, int, int); int letter(); void main() { cout << "NAME: "<< endl; cout << "Course: CPSC241"<<endl; cout << "Assignment: A4Q1" << endl; cout << endl; } int word(int countword, int morewords, int yetmorewords) { int countword = 1; for (int i = 0; i < sizeArray; i++) //Checks the number of words on the first line. { if (i == ' ') countword++; else if (i == '.') //These characters indicate the end of a word countword++d; else if (i == ',') countword++; } if (i == ' ') countword++; else if (i == '.') //These characters indicate the end of a word countword++d; else if (i == ',') countword++; }
Last edited by Ancient Dragon; Feb 25th, 2007 at 9:50 pm. Reason: add code tags
You haven't told us what you're having trouble with. Do you get compiler errors, runtime errors, what?
And by the way:
And by the way:
- Use code tags
- Use iostream, not iostream.h, etc. C++ Syntax (Toggle Plain Text)
- #include <iostream>
- using namespace std;
- Use int main() instead of void main()
"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.
All my posts may be freely redistributed under the terms of the MIT license.
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.
•
•
Join Date: Feb 2007
Posts: 4
Reputation:
Solved Threads: 0
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)
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)
>>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 '+').
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.
•
•
Join Date: Feb 2007
Posts: 4
Reputation:
Solved Threads: 0
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:
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)
]
if I use int main I do not get errors on the bottom.
ok this is what I changed and got coulple errors:
c Syntax (Toggle Plain Text)
#include <iostream> #include <string.h> #include <ctype.h> #include <iomanip.h> const int sizeArray = 100; int word(int countword, int moreword, int yetevenmorewords); int letter(); int main() { cout << "NAME: "<< endl; cout << "Course: CPSC241"<<endl; cout << "Assignment: A4Q1" << endl; cout << endl; int word(int countword, int morewords, int yetmorewords) { int countword = 1; for (int i = 0; i < sizeArray; i++) //Checks the number of words on the first line. { if (i == ' ') countword++; else if (i == '.') //These characters indicate the end of a word countword++; else if (i == ',') countword++; } if (i == ' ') countword++; else if (i == '.') //These characters indicate the end of a word countword++; else if (i == ',') countword++; } } ]
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
>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.
Any particular reason why you're using the ctype.h header?
In your word() function:
This line redeclares a parameter. Remove
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...
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)
#include <iostream> #include <cstring> #include <iomanip> using namespace std;
In your word() function:
C++ Syntax (Toggle Plain Text)
int countword = 1;
int if you simply want to reset the variable, although it kind of destroys the purpose of the parameter... C++ Syntax (Toggle Plain Text)
for (int i = 0; i < sizeArray; i++)
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.
All my posts may be freely redistributed under the terms of the MIT license.
>>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.
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.
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.
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:
This line redeclares a parameter. Remove
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...
A few other things:
Your headers need a little bit of fixing.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> #include <iomanip> using namespace std;
In your word() function:
C++ Syntax (Toggle Plain Text)
int countword = 1;
int if you simply want to reset the variable, although it kind of destroys the purpose of the parameter... C++ Syntax (Toggle Plain Text)
for (int i = 0; i < sizeArray; i++)
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.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- Playing .Wav/MIDI files in a Visual Basic Program (Visual Basic 4 / 5 / 6)
- What's the HARDEST program you've written? (Computer Science)
- Cool little Program to disable startup programs (Windows NT / 2000 / XP)
- Program is shutting down right after program is executed (C++)
- 3d Program (Game Development)
Other Threads in the C++ Forum
- Previous Thread: Array and File Help
- Next Thread: Passing command line variable into already running c++ program
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib 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 rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






