#include #include #include "sentence.h" #include // prompt for a one char answer - given set of possible answers and prompt char GetValid (const string & ok, const string & prompt) { char answ; // ask and get answer cout << prompt << flush; cin >> answ; int found = ok.find(answ); // reprompt if answer not among ok answers while ((found < 0) || (found >= ok.length())) { // tell user they were wrong and what is ok cout << "Your answer was invalid. It must be one of "; for (int cnt = 0; cnt < ok.length(); cnt++){ cout << " " << ok[cnt]; } cout << endl; // reprompt and get answer cout << prompt << flush; cin >> answ; found = ok.find(answ); } return answ; } void Stats (Sentence & currSent) { cout << currSent << endl; int numWords = currSent.HowManyWords(); cout << "Words: " << numWords << endl; double aveLen = currSent.AveWordLen(); cout << "Mean Word Length: " << aveLen << endl; int longWordLen = currSent.LongestWordLen(); string longWord = currSent.LongestWord(); cout << "Longest Word: " << longWord << " Length: " << longWordLen << endl; } int main() { /* Sentence mySent(cin); cout << mySent << endl; int numWords = mySent.HowManyWords(); cout << "Words: " << numWords << endl; double aveLen = mySent.AveWordLen(); cout << "Mean Word Length: " << aveLen << endl; int longWordLen = mySent.LongestWordLen(); string longWord = mySent.LongestWord(); cout << "Longest Word: " << longWord << " Length: " << longWordLen << endl; */ Sentence stuff[10]; int count = 0; char ask; do { // ask for new sentence and get it and stick it in array Sentence curr(cin); stuff[count] = curr; // calculate stats for the sentence Stats(stuff[count]); // ask user if they want to go on (and ensure we get a valid answer) ask = GetValid("ynYN", "Do you want to enter another? "); // convert to lower case to make checking easier ask = tolower(ask); count++; } while (ask == 'y'); // stay in as long as they say they want another return 0; }