I am at a beginner level of c++ programming, so this may be a very obvious question, but here goes:
I had to write a function that reads words from an input stream and stores them in a vector. That function is written in 'read_words.cpp'
I also had to write the functions interface which is in the file 'read_words.h'
Then I was asked to write two main programs that access 'read_words.h' and 'read_words.cpp' that counts the number of words in the input (program1) and counts how many times each word occurred (program2).
Here is the problem: all of my code compiles, so when I try to execute program 1 and program 2, I prompt the user to enter some words, followed by end of file. However, after I enter some words followed by end of file, nothing happens, the cursor just moves to a new line and I can continue to type words and characters and the compiler doesn't recognize that I entered 'end of file' indicating the end of the input stream. I have to type Ctrl Z to get out of the program. This happens when I try to run both programs.
So in short, it should be registering when the user writes 'end of file' after inputting however many words they like, which are supposed to be stored in a vector<string>. Then the rest of the code should be executed and it should print the output of how many words the user entered (program1) and how many times each word occurred (program2), but it is not doing this.
It simply prompts the user to enter words followed by end of file, and then nothing happens even if the user does enter end of file. My code for each file is below, can anyone tell me what I'm missing or what I'm doing wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
//read_words.h
#ifndef READ_WORDS
#define READ_WORDS
#include <iostream>
#include <vector>
#include <string>
std::istream& read_words(std::istream& in, std::vector<std::string>& words);
#endif
//read_words.cpp
#include "read_words.h"
using std::istream;
using std::string;
using std::vector;
//function that reads words and stores them in a vector
istream& read_words(istream& in, vector<string>& words)
{
if (in)
{
//get rid of previous contents, or empty the vector
words.clear();
//read the words
string word;
while (in >> word)
{
words.push_back(word); //place the inputed words into the vector
}
//clear the stream so that input will work for the next entry of words
//in.clear() resets the error state inside in
in.clear();
}
return in;
}
//main() program1.cpp
#include "read_words.h"
#include <vector>
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
vector<string> words;
cout << "Please enter some words, followed by end-of-file: " << endl;
if (read_words(cin, words))
{
cout << "There are " << words.size() << " words that were entered." << endl;
}
return 0;
}
//main() program2.cpp
#include "read_words.h"
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::sort;
using std::string;
using std::vector;
int main()
{
vector<string> words;
cout << "Please enter some words, followed by end-of-file: " << endl;;
if (read_words(cin, words))
{
typedef vector<string>::size_type vec_sz;
vec_sz size = words.size();
// Check that the user entered some words
if (size == 0)
{
cout << endl << "You didn't enter any words. "
"Please try again." << endl;
return 1;
}
// sort the words
sort(words.begin(), words.end());
string current_word;
int count;
// Set the first word to the first word in the vector
current_word = words[0];
// Set the first count for the first word
count = 1;
// Invariant: we have counted current_index of the total words in the vector so far
for (vec_sz current_index = 1; current_index < size; ++current_index)
{
// Print the count for the current word if it does not match
// the word at the current index in the vector, and reset the
// count to zero
if (current_word != words[current_index])
{
cout << "The word \"" << current_word << "\" occured "
<< count << " times." << endl;
current_word = words[current_index];
count = 0;
}
++count;
}
// Print the count for the final word
cout << "The word \"" << current_word << "\" occured "
<< count << " times." << endl;
}
else
{
cout << "An error occurred during input." << endl;
return 2;
}
//Exit the program
return 0;
}
|
Please help!