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.
/*/////////////////////////////////////////////////////
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";
}
}
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.
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.