big146's code solves the limit of 40 characters that InvariantLoop's code has.
However, it does have the same problem in that it will only get "hi" and stop at the first space.
To fix both, you would want to use getline which gets an entire line instead of >> which only reads to the next whitespace. One problem with mixing >> and getline (which you would be doing since InvariantLoop's code gets an integer first) is that the >> leaves a newline in the input stream, so the getline will stop there if you don't ignore that newline.
Try this:
Code:
#include <iostream>
#include <string>
#include <limits>
using namespace std;
int main()
{
//int counter = 0;
int num;
string theString;
cout << "Enter the number you want to loop"<<endl;
cin >> num;
cin.ignore(numeric_limits<int>::max(), '\n');
cout<< "Enter the phrase you want to loop"<<endl;
getline(cin, theString);
for (int i=0; i<num; i++)
{
cout <<theString<<endl;
}
cout<<"\nLoop executed: "<<num<<" time(s).\n";
cout <<"Do you really think that "<<theString<<" is true?\n";
return 0;
}
Note, you can also use a form of getline that works with char[40]. That getline is a member of cin, so you would use:
Code:
cin.getline(theChar, 40);