Say that you have 15 strings but you only want to output 5 at a time and after each 5 you let the user press a key to show five more. How would you do that? Here is an output example.
#include <iostream>
#include <sstream>
#include <vector>
usingnamespace std;
int main()
{
char ans;
string value;
string line = "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness,";
vector<string> words;
stringstream ss( line );
while ( ss >> value ) words.push_back( value );
for ( int i = 1; i <= words.size(); i++ )
{
cout << words[i-1] << endl; // watch out for array index
if ( i%5 == 0 && i != words.size() ) // every 5, give the opportunity to break out of the loop
{
cout << "Show 5 more? (y/n)";
cin >> ans;
if ( ans != 'y' ) break; // may want to consider alternative responses
}
}
}
void coutlist((vector<Person> friends)
{
char ans;
string value;
stringstream ss( friends );
while ( ss >> value ) friends.push_back( value );
for(auto& e : friends)
{
cout << right << setw(20) << e.firstname;
cout << setw(20) << e.lastname << endl;
if ( i%5 == 0 && i != friends.size() ) // every 5, give the opportunity to break out of the loop
{
cout << "Show 5 more? (y/n)";
cin >> ans;
if ( ans != 'y' ) break; // may want to consider alternative responses
}
}
}
If you already have content in your friend structure you certainly don't need to put any more in with stringstream (which I doubt you can do anyway). I only used stringstream as a quick way of creating some strings for an example.
Your for () loop won't work with auto & e because it doesn't know what i is. If you insist on using auto rather than a counter then you will have to increment i deliberately within the loop (having set it to 0 before looping). You will also have to test for your last element, unless you want a redundant prompt at the end.
char ans;
int counter=0;
for(auto& e : friends)
{
counter++;
}
for( int i = 0; i <= counter; i++ )
{
cout << right << setw(20) << e.firstname;
cout << setw(20) << e.lastname << endl;
if ( i%5 == 0 && i != counter ) // every 5, give the opportunity to break out of the loop
{
cout << "Show 5 more? (y/n)";
cin >> ans;
if ( ans != 'y' ) break; // may want to consider alternative responses
}
}
void coutlist((vector<Person> friends)
This will create an entirely new copy of the vector. It would be better to pass the existing vector by const reference: void coutlist((cont vector<Person> &friends)
The problem you seem to be having is that you want to use a range-based for loop, but in lastchance's solution, you have to know if you're at the end of the input. The way to get around this is to ask if they want to see 5 more when you know that you already have one to display and to ask before displaying it, not after.
#include <iostream>
#include <sstream>
#include <vector>
usingnamespace std;
int
main()
{
char ans;
string value;
string line = "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness,";
vector<string> words;
stringstream ss( line );
while ( ss >> value ) words.push_back( value );
int i=5;
for (string & word : words) {
if (i-- == 0) {
i = 4;
cout << "Show 5 more? (y/n)";
cin >> ans;
if (ans != 'y') {
break;
}
}
cout << word << '\n';
}
}
Note that inside the loop I had to reset the counter to 4, not 5.
Also note that I'm just using a counter rather than %, which is slower. Most of the time this won't matter.
Well, I'm glad that you got it to work, @markusfurst.
It would be worth having a go to make it work with @dhayden's method as well, because that does two important things:
- it deals with the end of the more modern range-based for() loop;
- it uses a subset counter (actually, count-down); in itself, I don't think the % operation would take much time compared with writing to screen; however, a subset counter would be especially useful if, in the future, you decided to ask for "how many more to show", rather than the simple "y/n".