Please complete the following programs utilizing C++ format (look at bold print
ID: 3626263 • Letter: P
Question
Please complete the following programs utilizing C++ format (look at bold print primarily..)
Growing a vector using push_back
Write a program that reads in words from the user and stores them in a vector until the user enters the sentinel word, done. The program should then ask the user for a letter and output all of the words that begin with that letter. Do not store the sentinel word done.
Sample run (user input is bold):
Enter words (enter the word done to quit):
Every man dies, not every man really lives. done
Enter a letter: m
man man
Sample run (user input is bold):
Enter words (enter the word done to quit):
The difference between the right word and the almost right word is the difference between lightning and a lightning bug. done
Enter a letter: b
between between bug
** Below is the program in its incomplete stage of development. When I initiate it nothing happens... Please tailor this program using push_back Thanks!! :)
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main ()
{
vector allWords; // holds all of the words
string word; // holds the word we are currently building
int i; // needed for traversal
char letter; // needed for letter choice
word = ""; // needed to set up the while loop
while (word != "done") {
while (!isalpha(cin.peek())) { // if its not a letter, just throw it away
cin.get();
}
while (isalpha(cin.peek())) { // while it's a letter, add it to the word string
word += cin.get();
}
if (word != "done") { // if it's not the sentinel, add it to the vector
allWords.push_back(word);
word = "";
}
}
cout << "Enter a letter: ";
cin >> letter;
for (i = 0; i < allWords.size(); i++) {
// the if statement is assuming case insensitive. For example if the user enters 'b' for the letter, this will print words that start with B or b. If this is not desired, remove the tolower from both sides of the ==.
if (tolower(allWords[i][0]) == tolower(letter)) {
cout << allWords[i] << " ";
}
}
cout << endl;
return 0;
}
Explanation / Answer
Your error is very simple. I managed to compile this after fixing the one bug in your code, I am not sure why the compiler didn't just alert you to the problem. You simply need to change this line: vector allWords; to vector allWords; Without the you are not declaring what type of vector you are making. This is the method used by template libraries such a the vector to make general STL usable content instead of making the function overloaded for different variable types or making a diff function for each type of variable. Its like declaring this: i = 5; What type of variable is i? int i = 5; Now you know i is an 'int' type if you had not already declared. So for vectors make sure you follow this format: vector NAME_OF_VARIABLE;Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.