boolean functions problem

May 12, 2013 at 10:06pm
I am trying to write a code that will read a .txt file and then output the number of words.I am trying to accomplish this by having a boolean variable inWord and two boolean functions, isWordStarting and isWordEnding. I will initially start the inWord variable out as false and then once the program encounters a letter, isWordStarting becomes true and so does inWord while isWordEnding is false. When the program encounters a whitespace or a period, isWordStarting becomes false and isWordEnding becomes true. By having the isWordStarting false and isWordEnding true, this causes inWord to become false and the numWord to increase by one.

however the problem I am encountering is that the compiler is telling me "error c2601: 'isWordStarting' : local function definitions are illegal" I don't understand this since the example for bool functions in my textbook uses if statements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <fstream>
#include <ctype.h>
using namespace std;
int main (int argc, char* argv[]){
	char c;
	bool inWord = false;
	bool inSyllable = false;
	bool inSentence = false;
	bool isSyllableStarting(char);
	bool isSyllableEnding (char);
	bool isSentenceStarting(char);
	bool isSentenceEnding (char);
	bool isWordStarting(char);
	bool isWordEnding(char);
	int numSentences = 0;
	int numWords = 0;
	int numSyllables = 0;
	
	ifstream infile;
	infile.open(argv[1]);
	while (infile.good()) {
		infile.get(c);
		bool isWordStarting (char c)
		{
			if (isalpha(c))
			{
				return true;
			} else if (isspace(c) || c == '.')
			{
				return false;
			}
			
		}
		bool isWordEnding (char c)
		{
			if (isspace(c) || c == "." && isWordStarting == false)
			{
				return true;
			} else{
				return false;
			}
		}
		if (isWordStarting == true)
		{
			inWord = true;
		} else if (isWordStarting == false && isWordEnding == true)
		{
			inWord = false;
			numWords= numWords +1;
		}					
	}
return 0;
}
May 12, 2013 at 10:54pm
In C/C++ you may not define a function inside another function. In your code you defined two functions, isWordStarting and isWordEnding, inside function main.
Last edited on May 12, 2013 at 10:55pm
May 12, 2013 at 11:48pm
so I moved the function declaration outside of main and now my program works. I'll now use this format to figure out the number of syllables and the number of sentences.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <fstream>
#include <ctype.h>
using namespace std;
bool isWordStarting (char c)
	{
		return (isalpha(c));
			
	}
bool isWordEnding (char c)
	{
		if (isspace(c))
			{
				return true;
			} else if( isalpha(c) || c == '.'){
				return false;
				}
	}
	
int main (int argc, char* argv[]) {
	char c;
	bool inWord = false;
	bool inSyllable = false;
	bool inSentence = false;
	int numWords = 0;
	ifstream infile;
	infile.open(argv[1]);
	while (!(infile.eof())) {
		infile.get(c); 
		cout.put(c);
		if (isWordStarting (c) == true)
		{
			inWord = true;
		} else if (isWordStarting (c) == false && isWordEnding(c) == true)
		{
			inWord = false;
			numWords++;
		}	
		cout<<numWords<<inWord<<endl; 
	}
infile.close();
cout<<numWords-1<<endl;
return 0;
}


I have the code outputting what the word count is and what the state of inWord is for each particular character as the program runs and then finally just outputting the word count.

I am now wondering if there is a way to specify "not an alphabetical character".

I was wondering if it is syntactically correct to right if (!isalpha(c)){ //whatever whatever}
Last edited on May 12, 2013 at 11:53pm
Topic archived. No new replies allowed.