I need to allow the user to enter a writing sample in the console or as a file and have my program split that input into a word vector (one word per item of vector). This is my current code:
while(cin >> inputString) {
wordVector.push_back(inputString);
}
The trouble is, when I run this, it works fine until it reaches the end of the user's input. Then it seems to just endlessly loop.
inputString is type string.
wordVector is type string.
This is the full code: (the broken code is at the bottom)
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
// Debug message flag
const bool DEBUG = false;
// Prototypes
void splitToVectors(vector<string>&,vector<string>&,vector<int>&,int &);
double avSentLength(const vector<string>);
double avWordSyl(const vector<string>,const vector<char>);
double percentSentLong(const vector<int>,int);
int numSyllables(const vector<char>);
void nextScreen(int);
int main() {
// Initialize variables and vectors
bool validate;
int characters,words,sentences = 0,syllables;
string file;
string inputString;
char inputChar;
int input;
vector<string> wordVector;
vector<char> charVector;
vector<string> sentenceVector;
vector<int> numWordsInSent;
// Get writing sample
do {
// Request preferred location
validate = true;
cout << "Would you like to:" << endl;
cout << " 1. Enter the writing sample in the console" << endl;
cout << " 2. Read from a file" << endl << " > ";
// Validate
if(!(cin >> input)) { // This error checking condition functions as the cin
validate = false;
cin.clear();
cin.ignore(100, '\n');
}
if((input < 1) || (input > 2)) {
validate = false;
}
} while(!validate);
// Transfer selected source to wordVector
if(input == 1) {
// Request sample
cout << "Please enter the writing sample below:" << endl << endl;
// Input sample
while(cin >> inputString) {
wordVector.push_back(inputString);
}
}
}