Program runs, but returns "inf"

I've made a program that calculates different reading indices of a text typed by the user, when they type "###" on a new line, that means they are done inputting text.
once I get to the calculations and chosen which index I want solved, it displays

"reading index is-inf"

Compilation turns up no errors though.

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
#include "flesch.h"
#include <iostream>
#include <string>
using namespace std;

double fre(double syl, double w, double sen) {
	double a=84.6, b=1.015;
    	double index=206.835-(a*(syl/w))-(b*(w/sen));
    	return index;
}
double fk(double syl, double w, double sen) {
double a=0.39, b=11.8;
double index= ((a)*(w/sen))+((b)*(syl/w))-10.59; //years old
return index;
} 

double psk (double syl, double w, double sen) {
double a=0.0778, b=0.0455;
double index=(a*(w/sen))+(b*(syl/w))-2.7971;
return index;
} 


int main() {
    //gets input
    bool done=false;
    string tmp,s;
    while (done==false) {
        cout<<"enter text";
        getline(cin, tmp);
        if (tmp!="###") {
            s+=(" "+tmp);
        }
        else {
            done=true;
        }
    }
    //copying string to be a constant
	const string arr=s;
    
    //processing
    double syllcount, wordcount, sentcount, sen100, syl100, hund=100.0;
    unsigned int i;
    for (i=1;i<=s.length(); i++) {

            if (sentencepunct(arr[i])==true){sentcount++;}
            //Not a sentence
            else if (isspace(arr[i-1])==false && isspace(arr[1])==true) { //prev is not whitespace and current is whitespace
                wordcount++;
	    }//closes not a sentence  
            //did not add to wordcount
            else if( isvowel(arr[i-1])==false && isvowel(arr[i])==true) {syllcount+=1.0;} //syllable counter
                    //not a syllable
                    else {continue;}
        if (wordcount==hund) { //
            sen100=sentcount;
            syl100=syllcount;
	}
     }//closes processing aka for loop
    
    
    
    
    
    //menu for kind of calculations
    
    cout<<"Which formula would you like to use to solve for the index?"<<endl<<"1.) Flesch Reading Ease\n2.)  Flesch Kincaid \n3.) Powers Sumner Kearl";
    int choice;
    cout<<"Use formula number: ";
    cin>>choice;
    switch (choice) {
        case 1:
        cout<<"The Flesch Reading Ease index is"<<fre(syllcount, wordcount, sentcount);
        break;
        case 2:
        cout<<"The Flesch Kincaid index is"<<fk(syllcount, wordcount, sentcount); 
        break;
        case 3:
        cout<<"The Powers Sumner Kearl index is"<<psk(syl100, 100, sen100);
        break;
    }
}//closes main
Last edited on
Could you provide the values you are plugging in to your formulae (e.g., syl, w, syn, a, and b for fre()) and what your expected result is?
For fre and fk:
(double syl, double w, double sen)
the arguments I pass are syllcount, wordcount,sentcount; how many syllables, words, and sentences there are at the end of processing.
a and b are just the constants for the formulae.

for psk:
sen100, and syll100 represent the sentence and syllable counts when word count is at 100.
again, a and b are just the constants given by the formula.

The expected result is the recomended reading age
I'd like to know the values you are passing (e.g., syl = 50, w = 20) and the result that you should be getting (e.g., 19.2 or whatever).
I've isolated the problem, formula functions work just fine. The issue is in the counting.
It compiles just fine, but the returned values are weird.

for instance, "the quick brown fox jumps over the lazy dog" returns sentences=65536, words= 24641422, and syllables= 1074073238, which are clearly wrong. I know something's wrong, and I know the general area of where it's wrong, but I don't know how to proceed.

here's the modified part of the program:
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
 //processing
    int syllcount, wordcount, sentcount, sen100, syl100;
    double hund=100.0;
    unsigned int i;
    for (i=1;i<=s.length(); i++) {
            if (sentencepunct(arr[i])==true){sentcount++; continue;}
            //Not a sentence
            else if (isspace(arr[i-1])==false && isspace(arr[1])==true) { //prev is not whitespace and current is whitespace
                wordcount++;
		continue;
	    }//closes not a sentence  
            //did not add to wordcount
           	 else if(isvowel(arr[i-1])==false && isvowel(arr[i])==true) {syllcount+=1;} //syllable counter
                    //not a syllable
                    else {continue;}
            if (wordcount==hund) { //
            	sen100=sentcount;
            	syl100=syllcount;
	}
     }//closes processing aka for loop
    
    
    
    
    
    //menu for kind of calculations
    
    cout<<"sentence count= "<<sentcount<<endl<<"wordcount= "<<wordcount<<endl<<"syllable count"<<syllcount<<endl<<"Which formula would you like to use to solve for the index?"<<endl<<"1.) Flesch Reading Ease\n2.)  Flesch Kincaid \n3.) Powers Sumner Kearl";
    int choice;
    cout<<"Use formula number: ";
    cin>>choice;
    switch (choice) {
        case 1:
        cout<<"The Flesch Reading Ease index is"<<fre(syllcount, wordcount, sentcount);
        break;
        case 2:
        cout<<"The Flesch Kincaid index is"<<fk(syllcount, wordcount, sentcount); 
        break;
        case 3:
        cout<<"The Powers Sumner Kearl index is"<<psk(syl100, 100, sen100);
        break;
    }


also; here's the implementation file the the flesch library I made:
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
#include "flesch.h"
#include <iostream>
#include <string>
using namespace std;


//a version of ispunct that only tests for sentence ending characters
bool sentencepunct (char prev) {
    if (strchr(".:;?1", prev) != NULL) {
        return true;
    }
    else {
        return false;
    }
}

// if it's a vowel, it returns true
bool isvowel (char given) {
    if (strchr("aeiou", given) != NULL) {
        return true;
    }
    else {
        return false;
    }
    
}
Last edited on
I checked over your code again, and it doesn't look like you ever initialize syllcount, wordcount, sentcount, sen100, or syl100. What are their values before you start the loop?
Topic archived. No new replies allowed.