0
I need to get my program to print all the words in the file and the average length of all the words in said file but the while loop is a problem
Post Flair (click to view more posts with a particular flair)
Post Body
I have my c code below
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
ifstream infile("Test.txt");
string words;
cout << words << endl;
float wordCount = 0;
float totalWordsCharacters = 0;
while (infile >> words) {
wordCount ;
totalWordsCharacters = words.length();
}
cout << "Average length of all words: " << (totalWordsCharacters / 79) <<" letters" << endl;
return 0;
}
there's a separate text file with 79 words and 79 lines, each word is in its own line. If i take out the while loop and put an in-text string instead of "words" like 'String ("we are the best")', everything works fine. How could i get the string of text in the separate file to be registered in the code?
Author
Account Strength
100%
Account Age
6 years
Verified Email
Yes
Verified Flair
No
Total Karma
16,945
Link Karma
9,409
Comment Karma
7,334
Profile updated: 1 week ago
Posts updated: 2 months ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 5 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnprogra...