Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <cstdlib>
- using namespace std;
- void print_list(int length)
- {
- //step1
- ifstream data_store;
- int counter = 0;
- //step2
- data_store.open("dict.txt");
- if (!data_store)
- {
- cout << "Error loading file\n";
- exit(0);
- }
- else
- {
- //step3
- while (!data_store.eof())
- {
- string name;
- getline(data_store, name);
- if(name.length() == length)
- {
- cout << name << endl;
- counter++;
- }
- }
- //step4
- data_store.close();
- }
- cout << "Found " << counter <<" matching words\n";
- return;
- }
- void is_word(int length)
- {
- bool flag = false;
- //step1
- ifstream data_store;
- //step2
- data_store.open("dict.txt");
- if (!data_store)
- {
- cout << "Error loading file\n";
- exit(0);
- }
- else
- {
- //step3
- while (!data_store.eof())
- {
- string name;
- getline(data_store, name);
- if(name.length() == length)
- {
- flag = true;
- break;
- }
- }
- //step4
- data_store.close();
- if (flag == true)
- {
- print_list(length);
- }
- else
- {
- cout << "Found 0 matching words\n";
- }
- }
- return;
- }
- int main()
- {
- int word_length = 15;
- cout << "How long should the words be?\n";
- //cin >> word_length;
- cout << "Words of length " << word_length << endl;
- is_word(word_length);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement