trouble with word/letter count using files.

Playing with file input/output for practice and I don't get why the word count and letter count are the same number. I have found some information on closing the file in between but it doesn't seem to want to do that. I get a close is not part of stream error or something like that.

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
 /*/////////////////////////////////////////////////////
This file will let you create a file and add text to it. It will then let you get a word count and letter count.
/////////////////////////////////////////////////////////*/

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<algorithm>

using std::ifstream;
using std::ios;


int main()
{
    std::cout << "Lets have some practice and fun playing with txt files. \n";
/*////////////////////////////////////////////////////////////////////////////////
    Here we will create a file and add some words to it.
////////////////////////////////////////////////////////////////////////////////*/
    std::ifstream instream;
    std::string file_name;
    std::cout << "Enter a name for the file you want to work with: \n";
    std::cin >> file_name;

    instream.open(file_name.data(), ios::app);
if (instream.fail()){
        std::cout << "failed to open for create file! \n";
        exit(1);
}

    std::ofstream words_to_file(file_name.data(), ios::app);
    std::string user_typed_words;
    std::cout << "Enter words to put in this file. When done press 'ENTER'. \n";
    std::cin.ignore();
    std::getline (std::cin, user_typed_words);
    words_to_file << user_typed_words << std::endl;
    std::cout << "Your words have been put into the file called " << file_name << std::endl;
/*/////////////////////////////////////////////////////////////////////////////////////////////
    Gives the user a choice to count the words in the file.
/////////////////////////////////////////////////////////////////////////////////////////////*/
char y;
    std::cout << "Would you like to count how many words are in the file? y or n \n";
    std::cin >> y;

if ((y == 'y') || (y == 'Y')) {
    std::ifstream instream;
    instream.open(file_name.data(), ios::app);

if (instream.fail()){
        std::cout << "failed to open for create file! \n";
        exit(1);
}

int cnt = 0;
    std::string words_in_file[1000];
while (instream >> words_in_file[cnt]){
    cnt++;
}

int number_of_words = 0;
for (int i = 0; i < cnt; i++) {
    if (words_in_file[i] != " "){
        number_of_words++;
    }
}


    std::cout << "You entered " << number_of_words << " words into a file called " << file_name << std::endl;
}

else {
    std::cout << "Okay \n";
}
/*////////////////////////////////////////////////////////////////////////////////////////////////////////////
    Gives the user a choice to count the letters in the file.
////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
char z;
    std::cout << "Would you like to count how many letters are in the file? y or n \n";
    std::cin >> z;

if ((z == 'y') || (z == 'Y')) {
    std::ifstream instream;
    instream.open(file_name.data(), ios::app);

if (instream.fail()){
        std::cout << "failed to open for create file! \n";
        exit(1);
}

int cnt = 0;
    std::string letters_in_file [1000];
while (instream >> letters_in_file[cnt]) {
    cnt++;
}

int number_of_letters = 0;
for (int w = 0; w < cnt; w++) {
    number_of_letters++;

}

    std::cout << "There are " << number_of_letters << " letters in the file " << file_name << std::endl;
}
else {
    std::cout << "Okay \n";
}

}
Are you sure that the output file was opened successfully?
If you mean the file that the words are being put into then yes. After I type the words I can open that file and see them in there. it's only printing one word per line right now but at least they are going into the file.
1
2
3
4
5
int number_of_letters = 0;
for (int w = 0; w < cnt; w++) {
    number_of_letters++;

}
The purpose of the above is?

You need to iterate over each string in your array.
I was trying to make it count the letters, but you are right I can remove it and it does the same thing. Thank you I don't need extra code sitting in there doing nothing.
That was meant as a hint, granted not a very good one.

You do need the loop but you need an additional statement to iterate the strings in the array.

try this:
1
2
3
4
5
6
int number_of_letters = 0;
for (int w = 0; w < cnt; w++) {
    for(size_t i=0; i<letters_in_file[w].size(); ++i)
        number_of_letters++;

}
Thank you.
So I was in the right direction just not the right commands, ".size()" is a command? I'm guessing it measures the size of the string?
You're welcome.

I'm guessing it measures the size of the string?
Yep. The std::string class has lots of useful member functions: http://www.cplusplus.com/reference/string/string/
Topic archived. No new replies allowed.