calculating sentences/words/syllables from a file opened with command line

So I just started programming and im working on a program that will calculate how many sentences/words/syllables are in a .txt file and i have to use comand line to open. There are other questions similar to this but i still cant figure it out. here is what i have so far

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  #include <iostream>
#include <cstdlib>
#include <fstream>

int sentenceCount(char *);
int sylableCount(char *);
int wordCount(char *);

using namespace std;



int main (int argc, char* argv[]) {

    //int numSentences = 0;
	//const int INPUT_SIZE = 10000;
	//char input[INPUT_SIZE];

	//cout << "Please enter a phrase or sentence (up to 100 characters):  \n\n";
	//cin.getline(input, INPUT_SIZE);

//cout << "File Path: " ;
//char line[1000];
//cin.getline(line, 1000);
//ifstream infil;
//infil.open(line);

char c;
int n_sentences = 0;
int n_Sylables = 0;
int n_Words = 0;
ifstream infile;
infile.open(argv[1]);
while (not (infile.eof())) {
infile.get(c); // NOT infile >> c;
//cout.put(c); // OR cout << c;



//while(infil.getline(line, 1000)){
 //cout << line << endl;



	//cout << "The number of sentences in the entered string is: ";
	n_sentences = n_sentences + sentenceCount(c);

	//cout << "\n\n";

    //cout << "The number of sylables in the entered string is: ";
	n_Sylables = n_Sylables + sylableCount(c);
	//cout << "\n\n";

	//cout << "The number of words in the entered string is:  ";
	n_Words = n_Words + wordCount(c);
	//cout << "\n\n";

}
cout << "The number of sentences in the entered document is: " << n_sentences << endl;
cout << "The number of sylables in the entered document is: " << n_Sylables << endl;
cout << "The number of words in the entered document is: " << n_Words << endl;
cout << "\nThe flesch reading score of this text is: " << 206.835 - (1.015 * (double(n_Words) / double(n_sentences))) - (84.6 * (double(n_Sylables) / double(n_Words))) ;

}

int sentenceCount(char *string1)
{
    int n_sentences = 0;
    while(*string1 != '\0'){

        if (*string1 == '.' || *string1 == '!' || *string1 == '?'|| *string1 == ';' ||*string1 == ':'){
            n_sentences++;
            string1++;
        }

        else {
        string1++;
        }
    }
    return n_sentences;
    //cout << numSentence;
}


int sylableCount(char *string2){
    int n_Sylables = 0;
    while (*string2 != '\0'){
        if (*string2 == 'a' || *string2 == 'e' || *string2 == 'i' || *string2 == 'o' || *string2 == 'u' ||
             *string2 == 'y' || *string2 == 'A' || *string2 == 'E' || *string2 == 'I' || *string2 == 'O' || *string2 == 'U' || *string2 == 'Y'){

            string2++;

            if ((*string2 >= 'a' || *string2 >= 'A') || (*string2 <= 'z' || *string2 <= 'Z') || *string2 == '.' || *string2 == '!' || *string2 == '?'|| *string2 == ';' ||*string2 == ':'){
                n_Sylables++;
                string2++;
            }

        }
        else {
            string2++;
        }
    }
    return n_Sylables;
    //cout << numSylable;
}




int wordCount(char *string3)
{
	int n_Words = 1;
	while(*string3 != '\0'){

	    if (*string3 == ' '){
            string3++;

		if(((*string3 >= 'a' || *string3 >= 'A') && (*string3 <= 'z' || *string3 <= 'Z'))){
                n_Words++;
                string3++;
            }
	    }

		else {
	string3++;
		}
	}
	return n_Words;
	//cout << numWords;
}

i got it to work when i used the file path user input but i cant with the comand line way i left most of the old code i used just to show you were i have been
i got it to work when i used the file path user input but i cant with the comand line way

Well, it looks like you've tried the right function: it's infil.getline() that you need to read a line from your text file, rather than reading just a char with get().

Ths code you posted confised me a moment as you#re trying to pass a char (c) to functions (sentenceCount, etc.) which take a char*,

This just reads the file and writes it out line by line (it's mostly just some of the bits you commented out)

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
#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

int main (int argc, char* argv[]) {

	// check we have at least 2 command line arguments so
	// that argv[1] is valid
	if(2 > argc) {
		cout << "usage test <file path>" << endl;
		return 0;
	}

	ifstream infile;
	const int INPUT_SIZE = 10000;
	char input[INPUT_SIZE];

	infile.open(argv[1]);
	if(!infile.is_open()) {
		cout << "could not open file!" << endl;
		return 0;
	}

	while(infile.getline(input, INPUT_SIZE)) {
		cout << input << endl;
	}

	return 0;
}


Andy

PS Do ';' and ':' denote the end of a sentence? I thought they were used inside a sentence, like a ','. Also, your syllable counting needs to some work:
http://www.howmanysyllables.com/howtocountsyllables.html

Last edited on
Thanks for the input i finally got it to work. This was an assignment and the the lab said that ; : ended a sentence i thought that was a little weird to
Cool!

And yep -- a bit weird! :-(

btw - for future ref.

Being lazy I would code

if (*string2 == 'a' || *string2 == 'e' || *string2 == 'i' || /*etc*/ *string2 == 'Y'){

(but for all the vowels)

using something like strchr() or string::find()

1
2
3
const char vowels[] = "aeiouyAEIOUY";

if (NULL != strchr(vowels, *string2)) {


or even

1
2
3
const char vowels[] = "AEIOUY";

if (NULL != strchr(vowels, toupper(*string2))) {


or the std::string equivalent:

1
2
3
const string vowels = "AEIOUY";

if (vowels.npos != vowels.find(toupper(*string2))) {


this definitely helps if you need to extend your code to handle all the accented vowels -- like the French àâèéêïûùô -- as well.

Andy
Last edited on
Topic archived. No new replies allowed.