Hello, first time poster long time reader.
The idea of the program is to accept a
file_name
from the user and find it within the "text" folder. To count the words and characters then display the info to the user organized in some way.
all the comments are for testing or unimplemented.
Here's the code:
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
|
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
//long long int letter_count = 0;
long long int word_count = 10000;
long long int final_word_count = 0;
string word_array[word_count];
//char letter_array[letter_count];
string file_name = "";
getline (cin, file_name);
cout << "\n";
ifstream subject_text(file_name);
//ifstream test("hum");
//cout << file_name << "\n";
if (!subject_text.is_open()){
cout << "couldnt open subject text file stream.\n";
return 1;
}
for(word_count = 0; word_count < 10000; word_count++)
{
subject_text >> word_array[word_count];
if (word_array[word_count] != "")
{
cout << "\t" << word_count << ": " << word_array[word_count] << "\n";
}
else if (word_array[word_count] == "")
break;
final_word_count = word_count;
}
cout << "\n\t" << final_word_count << " strings in " << file_name << "\n";
subject_text.clear();
//test.clear();
subject_text.close();
//test.close();
return 0;
}
|
Let me mention the "int"s are "long long" because eventually I would like to count a book. And I would like to feel confident I will have enough room in the
word_array
to not worry about any problems that may arise from not having enough room for each word.
This works but there's some problems I would like to address.
First of all, I can't figure out how to set a path to the folder for
file_name
without declaring the file name.
these don't work:
1 2
|
ifstream subject_text("../text", file_name)
ifstream subject_text("../text" file_name)
|
Also, what should I do to allow the program to count the words of a text file without knowing how many words are in it. Without changing
word_count < 10000
every time I count a bigger text file.
I don't remember what exactly I did to try that out but it was something like this
1 2 3 4 5 6
|
word_count = 1;
while (word_count > 0)
{
subject_text >> word_array[word_count];
word_count++;
}
|
but breaks down after I cin
file_name
And how would I go about sorting my strings according to rate of occurrence or alphabetically. maybe qsort? I plan to implement an option for either based on user input.
Lastly Should I use gcount() for counting my characters?
I'm not fluent in C++ so spoon feed it to me if you will.
Thanks for the help.