I have been given this question:

Develop an array of characters that contains the phrase "Wow, it is really hot in this room.". Then create a vector of strings where each string contains two letters more than the previous one. Prompt the user for the length of the initial string which must be 1,2 or 3 letters and the number of strings to print. Your program should work even if the character array changes so it must check to make sure it is long enough for a given input request. For example, if the input values are 2 and 4, the output will be (Note: Spaces are included):
w, i
t is r
ally hot

And for the life of me I can't figure it out. Here is the code i have so far, but I don't know what to do to correct it

#include <iostream>
#include <vector>
#include <string>
#include <cmath>

using namespace std;

void printVector( vector<string> v )
for(unsigned int i = 0; i < v.size(); i++)
cout << v[i] << endl;
cout << endl;


int main (void)

string Phrase = "Wow, it is really hot in this room.";
int csize = Phrase.length();
int n= 0;
int m = 20;
int j=0;
int x=0;
cout << "Enter 1,2, or 3 for the first string size" << endl;
cin >> n;
vector<string> vOut(csize);

for ( int i = n; i <= csize; i++)


vOut.push_back(Phrase.substr( 0 ,i));


printVector (vOut);

return 1;


Your program is meant to take in a second input which establishes the number of strings stored in the vector and thereby controls the printout.
As you are aware your program simply prints the first 2 letters then the first 3 letters of the complete string (from the vector) and so on until the complete array is printed. Fix this and post again if you have a problem.
Topic archived. No new replies allowed.