8 Replies - 1788 Views - Last Post: 09 September 2012 - 09:32 AM Rate Topic: -----

#1 gino306   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-September 12

Stuck on a word length frequency program

Posted 09 September 2012 - 08:50 AM

I am trying to write a program that will keep track of word length frequency. The input can be from the keyboard or a file. This is just a rough draft I have been working on so far and I have not yet coded it to read from a file, but I can not get the program to work. I would greatly appreciate any help.

#include <iostream>
#include <ctype.h>
using namespace std;

void printArray (int a[], int s);
int wordLength (char x, int T[]);

void main()
{
	char Ch;
	int Total[16]={0};
	int i=0;
	
	cout <<"Please enter a sentence, phrase or file.\n";
	
	Ch = cin.get();

	while(wordLength!=0)
	{
		wordLength(Ch,Total);
		Ch =cin.get();
		++i;
		++Total[wordLength];
	}

	printArray (Total,i);

}

//
//
//

void printArray (int a[], int s)
{
	for (int i=0; i < s; ++i)
		cout << a[i] << " ";
		cout << endl;
}

int wordLength (char x, int T[])
{
	int i = 0;
	while (!isspace(x) && !ispunct(x))
	{
		if(isalnum(x))
			++i;
			return i;
	}
}



This post has been edited by jimblumberg: 09 September 2012 - 08:53 AM
Reason for edit:: Fixed Code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Stuck on a word length frequency program

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Stuck on a word length frequency program

Posted 09 September 2012 - 08:54 AM

Quote

but I can not get the program to work.

Why isn't it working? Please ask specific questions.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 gino306   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-September 12

Re: Stuck on a word length frequency program

Posted 09 September 2012 - 09:04 AM

Well my idea is that I have an array Total that would keep track of word length. So Total[1] keeps track of word length of 1, Total[2] keeps track of word length of 2 etc. So I wanted to return my value from my function wordLength into Total[wordLength+1] and increase Total. However, I get an error when I put wordLength into Total. Does that make sense?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Stuck on a word length frequency program

Posted 09 September 2012 - 09:07 AM

Quote

However, I get an error when I put wordLength into Total. Does that make sense?

I don't know, maybe if you told me the error it might make sense.

If you are getting compiler errors post the complete error messages exactly as they appear in your development environment. These messages have important information embedded within them to aid in locating and fixing the errors.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 gino306   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-September 12

Re: Stuck on a word length frequency program

Posted 09 September 2012 - 09:11 AM

Below is my error. I am using visual studio and it says that ++Total[wordLength]; wordLength expression must have integral or enum type.

1>------ Rebuild All started: Project: Homework1, Configuration: Debug Win32 ------
1>Build started 9/9/2012 12:08:14 PM.
1>_PrepareForClean:
1> Deleting file "Debug\Homework1.lastbuildstate".
1>InitializeBuilsdStatus:
1> Creating "Debug\Homework1.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1> homework1.cpp
1>c:\users\owner\desktop\homework1\homework1\homework1.cpp(29): error C2107: illegal index, indirection not allowed
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.99
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Stuck on a word length frequency program

Posted 09 September 2012 - 09:17 AM

The line number in the error, 29, doesn't match up with your code. Post your actual code.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Stuck on a word length frequency program

Posted 09 September 2012 - 09:21 AM

Is wordLength an integer or enum type? Answer: No wordLength is a function.

In the following snippet:
	while(wordLength!=0)
	{
		wordLength(Ch,Total);
		Ch =cin.get();
		++i;
		++Total[wordLength];
	}



You have several things wrong. Fist as already stated wordLength is a function, not a number. You probably want to use a variable to use for the return value from your function, then use that variable in both the while() and the index of Total[].

You may want to study the tutorials contained in my signature. These function tutorials should help you better understand how to properly use functions.

Jim
Was This Post Helpful? 1
  • +
  • -

#8 gino306   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-September 12

Re: Stuck on a word length frequency program

Posted 09 September 2012 - 09:22 AM

I will re-post it.

*****************************************************************************
*
*
*
*
*****************************************************************************/
#include <iostream>
#include <ctype.h>
using namespace std;

void printArray (int a[], int s);
int wordLength (char x, int T[]);

void main()
{
	char Ch;
	int Total[16]={0};
	int i=0;
	
	cout <<"Please enter a sentence, phrase or file.\n";
	
	Ch = cin.get();

	while(wordLength!=0)
	{
		wordLength(Ch,Total);
		Ch =cin.get();
		++i;
		++Total[wordLength];
	}

	printArray (Total,i);

}

//
//
//

void printArray (int a[], int s)
{
	for (int i=0; i < s; ++i)
		cout << a[i] << " ";
		cout << endl;
}

int wordLength (char x, int T[])
{
	int i = 0;
	while (!isspace(x) && !ispunct(x))
	{
		if(isalnum(x))
			++i;
			return i;
	}
}




MOD EDIT:
Please place your code between an opening and closing code tag, not between a pair of code tags:

:code:

.

This post has been edited by jimblumberg: 09 September 2012 - 09:25 AM
Reason for edit:: Fixed Code tags.

Was This Post Helpful? 0
  • +
  • -

#9 gino306   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-September 12

Re: Stuck on a word length frequency program

Posted 09 September 2012 - 09:32 AM

Thank you, I understand what you are talking about using a variable for my function return value. I fixed that. It still doesn't do what I want so I will keep working on it. Also, I will use the code tags properly next time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1