I'm just looking for a few tips here on how to keep my homework assignment from killing me. Not asking for answers here just someway to go in the right direction. I have to read a txt. file in line by line and go through and find how many words are in each line and add them to the total number of words in the file. I have to write a function as well to find the number of words in each line and this is where I'm completely lost. Any step in the right direction would help me out greatly. Thank you very much.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
// This function will read in a current line and find out how many words are in it
void wordsInCurrentLine (string line)
{
string delimeters = " .,?:;!()";
return;
}
int main ()
{
ifstream inFile; // Declares the file that holds all the data
int countline; // This is used to keep count
string currentLine; // This is the string being read from the file
int wordCount;
// Open the file
inFile.open("story.txt");
// Check to make sure the file opened correctly
if (!inFile)
{
cout << "File did not open correctly" << endl;
return 1;
}
// Set the count equal to zero
countline = 0;
// Get the first line of the file
getline(inFile, currentLine);
// Check the number of lines in the file
while (inFile)
{
wordsInCurrentLine (currentLine, wordCount);
countline ++;
getline(inFile, currentLine);
}
// Display the number of lines in the file
cout << "There are " << countline << " lines in the file" << endl;
return 0;
}
I talked to my teacher a little more the other day and I think I'm on the right track but I've obviously done something very wrong.
I've got a .txt file with at most 25 words in it but I keep getting a number of words returned in the hundreds of millions. If anyone can please help me decipher what I've done wrong it will be a great help. My character count is off as well.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
// This function finds the number of words and characters in a given line
void wordsInFile (string line, int& wordCount, int& charCount)
{
string delimiters = " .,?:;!()"; // Sets the delimiters of a word
char currentChar; // Keeps track of the current character
bool inWord = false; // Keeps track of whether you're in a word or not
// Tack a delimiter on at then end of the line to make word ID easier
line = line + '.';
// Initialize the wordCount to zero
wordCount = 0;
// For loop to count the characters in the line
for (charCount == 0; charCount < line.length(); charCount++)
{
// Set the current character
currentChar = line.at (charCount);
// If current character is not a delimiter
if (delimiters.find (currentChar) == string::npos)
{
// If we're inside a word
if (inWord)
// Increment the character count
charCount++;
// If it's the beginning of the word then set inWord to true and increment
// the character and word counts
else
{
inWord = true;
charCount++;
wordCount++;
}
}
// else the current character is a delimiter
else
{
if (inWord)
{
// Set inWord to false and increment the character counter
inWord = false;
charCount++;
}
else
charCount++;
}
}
return;
}
int main ()
{
ifstream inFile; // Declares the file that holds all the data
int countline; // This is used to keep count
string currentLine; // This is the string being read from the file
int totalWordCount; // This is the total word count of the file
int totalCharCount; // This is the total character count of the file
int wordCount; // A word count of each line
int charCount; // A character count of each line
// Open the file
inFile.open("story.txt");
// Check to make sure the file opened correctly
if (!inFile)
{
cout << "File did not open correctly" << endl;
return 1;
}
// Set the count equal to zero
countline = 0;
// Get the first line of the file
getline(inFile, currentLine);
// Check the number of lines in the file
while (inFile)
{
// Call the function to find words and characters in the file
wordsInFile (currentLine, wordCount, charCount);
// Add the wordCount from the function to the total word count
totalWordCount += wordCount;
// Add the charCount from the function to the total character count
totalCharCount += charCount;
// Increment the line counter
countline ++;
// Get the next line
getline(inFile, currentLine);
}
// Display the number of lines in the file
cout << "There are " << countline << " lines in the file" << endl;
// Display the number of words in the file
cout << "There are " << totalWordCount << " words in the file" << endl;
// Display the number of characters in the file
cout << "There are " << totalCharCount << " characters in the file" << endl;
return 0;
}
I made my self a small function to count the number of words in a string a while ago.
1 2 3 4 5 6 7 8 9
int Words(char* s) {
int w = 0;
int l = Lenght(s);
for(int i = 0; i < l; i++) {
if ((s[i-1] == ' ' || s[i-1] == NULL) && (s[i] != ' ')) w++;
if (s[i] == NULL) break;
}
return w;
}
The algorithm may look a bit confusing, but it definitely works.
Turns out that I had two things wrong. I didn't initialize totalWordCount in the main function and like andwestken said, I was double counting character in the for loop in the first function. Appreciate the help everyone once again.
This place is awesome btw. Everyone seems to be really nice and helpful to us newbs.