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
#include <iostream>
#include <cstdlib>
#include <fstream>
int sentenceCount(char *);
int sylableCount(char *);
int wordCount(char *);
usingnamespace 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)
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