I am trying to write a code that takes words from a text file, reads them, stores them into a string array, and counts the words of the string array. I have been able to count the words from the file and also store them into a string array, but I can't seem to do both. Any idea what I am dong wrong?
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main(){
int getWordCount = 0;
int finalCount = 0;
string filename;
string words;
string wordsArray[2000];
ifstream infile;
cout << "Enter filename: ";
getline(cin,filename);
infile.open(filename.c_str());
if(!infile)
cerr << "Sorry, filename unavailable." << endl;
else{
//this was what I originally wrote to get the word count from file
//while(infile >> words){
// getWordCount++;
// }
while(getline(nfile, wordsArray[getWordCount])){
cout << "Words in array: " << wordsArray[getWordCount] << endl;
getWordCount++;
}
//this is what I wrote to count after storing words into the array from the file
//I used finalCount because I don't think I can reuse getWordCount
//for(int i=0; wordsArray[i] != '\0'; i++){
if(wordsArray[i] == ' ')
finalCount++;
}
infile.close();
cout << "The array contains " << finalCount << " words."<< endl;
}
return 0;
}
I had to hand type this out because my copy/paste is messed up. It compiles file so if there's a typo, please ignore it lol
Note: The getline() function gets an entire line, not necessarily a single word.
What is the purpose of finalCount? You already have the total number of words in the array, getWordCount. Realize that that if() statement is not inside any loop construct so it will only happen once.
Remember if a stream enters an error state that no further processing of the stream is possible until the error state is cleared. So, for example, if you try to read the entire file more than once you need to take care about clearing the stream error state and repositioning the stream pointer before trying to read the subsequent times.
You should also consider using a std::vector instead of the array, then you will know how many "words" are read by looking at the size of the vector.
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <conio.h>
usingnamespace std;
int main()
{
string testline;
string word[16];
ifstream Test ( "filename.txt" );
if (!Test)
{
cout << "There was a problem opening the file. Press any key to close.\n";
getch();
return 0;
}
int i=0;
//store words in array while outputting, skipping blank lines
while( Test.good() )
{
getline ( Test, testline, ' ');
if (testline!="")
{
word[i]=testline;
cout << word[i] << endl;
i++;
}
}
//output whole array with spaces between each word
cout<<"\nArray contents:\n";
for (int k=0;k<i;k++)
cout<<word[k]<<"\n";
return 0;
}
//this was what I originally wrote to get the word count from file
//while(infile >> words){
// getWordCount++;
// }
That should work.
1 2 3 4 5 6
//this is what I wrote to count after storing words into the array from the file
//I used finalCount because I don't think I can reuse getWordCount
//for(int i=0; wordsArray[i] != '\0'; i++){
if(wordsArray[i] == ' ')
finalCount++;
}
That should not.
You can't compare a string to a character. Also, why would you add one to the count if the string is a space? Also, once you read the file, you have the final word count. If you insist on checking the array, then count up until you reach an empty string.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main(){
int getWordCount = 0;
int finalCount = 0;
string filename;
string words;
string wordsArray[2000];
ifstream infile;
cout << "Enter filename: ";
getline(cin,filename);
infile.open(filename.c_str());
if(!infile)
cerr << "Sorry, filename unavailable." << endl;
else{
// this was what I originally wrote to get the word count from file
while(infile >> wordsArray[getWordCount]){
getWordCount++;
}
cout << "getWordCount is " << getWordCount << '\n';
// Count the words the hard way
for(int i=0; !wordsArray[i].empty(); i++){
++finalCount;
}
infile.close();
cout << "The array contains " << finalCount << " words."<< endl;
}
return 0;
}
$ ./foo
Enter filename: foo.cxx
getWordCount is 101
The array contains 101 words.
After I posted this, I realized what I was trying to do and what I was actually doing was wayyyyy off. Thanks for your input! the counting part helped.