Counting the number of vowels

In my programming class we were given a problem to do, which is to prompt the user to input a sequence of characters and output the number of vowels. I did all of that and my code worked perfectly but then my professor added additional requirements. Now i have to use input and output files as well as the functions read and print. Now I'm just a little confused about how to use the read and print functions and I'm having trouble getting the result to show up in my output file. (P.S. for the read and print functions i know I only have the prototypes listed)

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
void read();
void print();
bool DeterminIfVowel(char);

int main()
{
ifstream inFile;
ofstream outFile;
string phrase;
inFile.open("vowel_test.txt");
outFile.open("vowel_output.out");

cout << "Enter a sequence of characters or a sentence" << endl;
getline(inFile, phrase);

int count = 0, i;
for (i = 0; i < phrase.length(); i++)
{
if (DeterminIfVowel(phrase[i]))
count++;
}
outFile << "There are " << count << "vowels in " << phrase << endl;

system("pause");

inFile.close();
outFile.close();


return 0;
}

bool DeterminIfVowel(char c)
{
return (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U');
}
Let me put that between code tags, and do a little light formatting for you:

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

using namespace std;
void read();
void print();
bool DeterminIfVowel(char);

int main()
{
    ifstream inFile;
    ofstream outFile;
    string phrase;
    inFile.open("vowel_test.txt");
    outFile.open("vowel_output.out");

    cout << "Enter a sequence of characters or a sentence" << endl;
    getline(inFile, phrase);

    int count = 0, i;
    for (i = 0; i < phrase.length(); i++)
    {
        if (DeterminIfVowel(phrase[i]))
            count++;
    }
    outFile << "There are " << count << "vowels in " << phrase << endl;

    system("pause");

    inFile.close();
    outFile.close();


    return 0;
}

bool DeterminIfVowel(char c)
{
    return (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U');
}
Last edited on
Presumably you're trying to get it to work in main before you break out parts to different functions. I believe I would start here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    std::string in_name = "vowel_text.txt";
    std::string out_name = "vowel_output.out";

    std::ifstream inFile(in_name);
    if (!inFile.is_open())
        std::cerr << "Unable to open file \"" << in_name << "\" for input.\n";

    std::ifstream outFile(out_name);
    if (!outFile.is_open())
        std::cerr << "Unable to open file \"" << out_name << "\" for output.\n";

    // ... 


Check to see if you're actually opening the files.
Topic archived. No new replies allowed.