having a hard time with logic

So i've been struggling with this program and everytime I figure something out I get stuck on something new.The program is supposed to count the number of words, consonants, digits, and sentences in a file. I figured out the digits and consonants with some help but I'm struggling with the words and 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
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
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>

using namespace std;

bool at_endl(ifstream&);
void skip_blanks(ifstream&);
void reformat(string&);
void getfilename(string,string&);
void count_digit(string,int&);
void count_consonants(string, int&);


int main()
{
    string word, filename;
    char ch;
    int digits=0, numcons=0, words=0;
    ifstream input;
    getfilename("input",filename);
    input.open(filename.c_str());
    input >> word;
    while (!input.eof())
        {
            reformat(word);
            count_digit(word,digits);
            count_consonants(word,numcons);
            cout << word;
            skip_blanks(input);
            if (at_endl(input))
                {
                    cout << endl;
                }
            else
                cout << " ";
            input >> word;
        }
            cout << digits << endl;
            cout << numcons;
            return 0;
}

void getfilename(string filetype, string& filename)
{
    cout << "Enter the name of " << filetype << " file\n";
    cin >> filename;
}

bool at_endl(ifstream& input)
{
    return (input.peek() == '\n');
}

void skip_blanks(ifstream& input)
{
    char ch;
    while (input.peek() == ' ')
        input.get(ch);
}

void reformat(string& word)
{
    int wlength;
    wlength = word.length();
    word[0] = toupper(word[0]);
    for (int i=1; i<wlength; i++)
        word[i] = tolower(word[i]);
}

void count_digit(string word, int& digits)
{
    int wlength;
    wlength = word.length();
    for (int i=1; i<wlength; i++)
    {
        if (isdigit(word[i]))
            digits = digits + 1;
    }
}

void count_consonants(string word, int& numcons)
{
    int wlength;
    wlength = word.length();
    for (int i=0; i<wlength; i++)
    {
        word[i] = toupper(word[i]);
        if (word[i] >= 'A' && word[i] <= 'Z')
            {
                if (word[i] != 'A' && word[i] != 'E' && word[i] != 'I' && word[i] != 'O' && word[i] != 'U')
                    numcons = numcons + 1;
            }
    }
}


i wrote a function to count words but its not working:

1
2
3
4
5
6
7
8
9
10
void count_words(ifstream& input, int& words)
{
    string tempstring;
    int wlength;
    getline(input,tempstring);
    wlength = tempstring.length();
    for(int i=0; i<wlength; i++)
        if (tempstring[i] == ' ')
            words++;
}


Any help would be awsome!
An over simple and not quite right way to count sentances is simply to count the number of times '.' appears in the file. To do it right you're looking at A LOT of work.

For your word counting function getline(...) is your problem, this looks for a return carridge deliminator. Replace getline(...) with something like input >> tempstring. It looks like the count_words(...) function is trying to count the words in the sentence, is this the case? If it is we have to go about it in a differant way.

Also shouldn't most of these functions be returning int's?
no i have to count the number of words in the entire file, and in my case a word is defined as a series of any non-white space characters.

yes most of these return ints, does that mean i should have written them as value returning functions instead?

To count sentences you would have to count the following as valid delimiters,

? ! .

To count words it would probably be easiest (from my limited experience) to use a vector as in the following example from Thinking In C++.

1
2
3
4
5
6
7
8
9
10

int main() {
  vector<string> words;
  ifstream in("GetWords.cpp");
  string word;
  while(in >> word)
    words.push_back(word); 
  for(int i = 0; i < words.size(); i++)
    cout << words[i] << endl;
}
thanks Jahmbo, unfortunately i cant use vectors in this exercise :-(
Topic archived. No new replies allowed.