user863492
user863492

Reputation: 57

C++ Novice regarding Vectors and for/while loops

I’m trying to make something that will take lines of input from the user, separate them into strings in a vector, then print them one at a time (8 per line). so far this is what I’ve got:

#include <iostream>
#include <vector>
#include <string>
#include <sstream>

int main(void)
{
    using namespace std;

    vector<string> svec1;
    string temp;
    while(getline(cin, temp)) //stores lines of text in temp
    {
        if(temp.empty()) //checks if temp is empty, exits loop if so.
            break;
        stringstream ss(temp);
        string word;
        while(ss >> word) //takes each word and stores it in a slot on the vector svec1
        {
            svec1.push_back(word);
        }            
    }        
}

I’m stuck on getting it to print them 8 at a time, the solutions I’ve tried keep getting subscript out of range errors.

Upvotes: 1

Views: 346

Answers (3)

Vlad
Vlad

Reputation: 35584

Something like this:

for(int i = 0; i < svec1.size(); i++)
{
    cout << svec1[i];
    if ((i+1) % 8 == 0)
        cout << endl;
    else
        cout << " ";
}

?

EDIT:
the solution above outputs extra space/newline at the end. It can be avoided by something like this:

for(int i = 0; i < svec1.size(); i++)
{
    if (i == 0)
        /*do nothing or output something at the beginning*/;
    else if (i % 8 == 0)
        cout << endl; /*separator between lines*/
    else
        cout << " "; /*separator between words in line*/
    cout << svec1[i];
}

Upvotes: 2

David Hammen
David Hammen

Reputation: 33106

Walk over your vector with an index:

for (unsigned int idx = 0; idx < svec1.size(); ++idx) {
   std::cout << svec[idx] << sep(idx); // sep(idx) is conceptual; described below
}

What is this sep(idx)? It is the separator to print after the idxth word. This is

  • A newline after having printed eight words on a line. idx will be 7, 15, 23, etc: One shy of an integer multiple of 8. In code, (idx+1)%8 == 0.
  • A newline for the last item in the vector; you probably want the last item to be followed with a newline. In code idx+1 == svec.size().
  • A space otherwise.

An easy way to do this is with the ternary operator:

for (unsigned int idx = 0; idx < svec1.size(); ++idx) {
   const char * sep = (((idx+1)%8 == 0) || (idx+1 == svec.size())) ? "\n" : " ";
   std::cout << svec[idx] << sep;
}

If you don't like that,

for (unsigned int idx = 0; idx < svec1.size(); ++idx) {
   const char * sep;
   if (((idx+1)%8 == 0) || (idx+1 == svec.size())) {
      sep = "\n";
   }
   else {
      sep = " ";
   }
   std::cout << svec[idx] << sep;
}

Upvotes: 0

Constantinius
Constantinius

Reputation: 35039

Normally you iterate over a vector using a for loop clause. So if you want to print all elements of your vector<string> you have to make something like this:

for(vector<string>::iterator it = myvec.begin(); it != myvec.end(); ++it) {
    cout << *it;
}

EDIT: as Vlad has posted correctly, you can also use array indices, which are less efficient in lists, but equally efficient with vectors.

Upvotes: -1

Related Questions