So I'm probably gonna fail this class because I'm having a hard time understanding everything, but I gotta try right? Anyways, for my lab, I'm using a txtfile filled with a crazy amount of words and here is what I have to do:
For this lab, you will use a list of English words to find out interesting things about our language. Please hard code the path of the file into your program and do not ask the user to input it - if you put it in the C drive, it would make my life easier. Mac/Linux people, don't worry about that. Also, if you could put all of your responses into one file (clearly commented!), that would be easier for both of us. This week, please do ALL THREE questions!
1. Find the largest word on the list, print it, and print it's length (the str.length() function will help here!).
2. Find all of the words with one letter and print them.
3. Find the number of words of every possible length and print how many there are in a table.
The table should start off looking like this:
Length Number of Words
1 2
2 140
. .
. .
. .
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// declare vars
string largestWord;
ifstream file;
int largest = 0;
// open file
file.open("C:/Users/haitr/Desktop/EnglishWords.txt");
// part 1
cout << "Part 1: ";
while (!file.eof)
// part 2
cout << "Part 2: ";
// part 3
cout << "Part 3: ";
// pause and exit
getchar();
getchar();
return 0;
}
I know I haven't done much at all, but an outline would help. Thanks.
I'll give you a hint for the first and second ones.
1) To find the largest word, you're going to have to compare each word to every other word.Think about working with indexes rather than actual words because you can always get the word from the index. Also think about setting a temporary index to compare the first element against.
2) if a word has one letter, you want to print it. So use the str.length() function that your teacher showed you to say "is the length of this word equal to one? If so, print it. Otherwise, do nothing"
The third one is going to take some thought. I think once you do the first and second ones, though, that it should become much easier.
std::vector<std::string> word_list{}; // to store all the words
std::string temp{};
while( file >> temp ) {
word_list.push_back( temp );
}
// loop through word_list
Why, I really want to help him of course. Private messages are better when we are going to solve something more general, not just a specific question.
Private messages are better if you want to engage in private communication unrelated to C++. Public messages are better if you value input which may be reviewed by multiple people so that potential mistakes and other approaches might be enumerated. Given your history of poor quality responses (and outright lies,) it seems likely a higher quality of response will be achieved with public messages.
[Edit: For anyone wondering where the quote came from: this was a response for the benefit of the OP to a post by SakurasouBusters which was deleted by him -- a common occurrence -- before this post was added to the thread.]
Private messages are better if you want to engage in private communication unrelated to C++. Public messages are better if you value input which may be reviewed by multiple people so that potential mistakes and other approaches might be enumerated. Given your history of poor quality responses (and outright lies,) it seems likely a higher quality of response will be achieved with public messages.
Yeah, but you just don't know how serious I am when it comes to 'private messages'. I can do much better even than myself when it comes to solving problems via 'private messages.'
You just plain underestimate me, cire.
Edit : This may work out or not work out, because I always ask the person first, if the person accepts the help I will help them with their own assignment.
@Revert
If you have any specific question or some problem with your code, you can post here so that everyone can give you a hand. Otherwise, unless you are completely unsure what you have to do, you can send me a private message.