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:

#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++;
}

any help would be greatly appreciated. or if anyone has a solution to this program please advise. THANK you very much!!!!

Recommended Answers

All 8 Replies

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.
    #include <iostream>
    using namespace std;
  • Use int main() instead of void main()

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.

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)

>>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 '+').

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:

#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++;
}
}
]

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)
]

>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.

#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;

Any particular reason why you're using the ctype.h header?

In your word() function:

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...

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...

>>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.

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.

#include <iostream>
#include <cstring>
#include <iomanip>
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:

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...

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.