user3901281
user3901281

Reputation:

How do I prevent last vector item from printing?

I am trying to prevent the last item of a vector from printing. How do I modify my code to make this happen?

void lAverage(){
    cout << endl << "The average of: ";
    for (int i = 0; i < gAverage.size(); ++i){
        if (i == gAverage.size()) {
            cout << gAverage[i]; // I have to modify something here.
        } else{
            cout << gAverage[i] << ", ";

        }
    }
    cout << " = " << average << endl;
}

instead of printing: the average of: 3, 2, 1, 0 = 2

I'd rather: The average of : 3, 2, 1 = 2

No comma on the last one and the 0 removed.

Upvotes: 1

Views: 108

Answers (5)

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

If you want to print to an output stream with intervening commas, but at the same time exclude the last two item in the container (for whatever reason), here is a solution using the std::copy algorithm:

#include <algorithm>
#include <iterator>
#include <vector>

using namespace std;

void lAverage()
{
    // make sure there are at least 2 items
    if (gAverage.size() >= 2)
    {
        auto itLast = gAverage.end() - 2;
        cout << endl << "The average of: ";

        // copy item to output stream with intervening comma up until
        // two before the last item  
        copy(gAverage.begin(), itLast, ostream_iterator<int>(cout, ","));

        // print the item before the last and the average
        cout << *itLast << " = " << average << endl;
   }
}

Live Example

Upvotes: 1

Peter
Peter

Reputation: 36597

Two questions here: how to print one element less than the total number in the container, and how to make the last element printed not be followed by a comma.

To meet both requirements without using a conditional, I'd stop the loop two elements back

int last = gAverage.size() - 2;
for (int i = 0; i < last; ++i)
{
    cout << gAverage[i] << ", ";
}
cout << gAverage[last];

cout << " = " << average << endl;

The loop prints all values that will be followed by the comma. The next cout statement prints the second last element of the array.

This avoids the silliness of a condition in the loop to detect the last element being printed.

Upvotes: 0

omer727
omer727

Reputation: 7565

There is no case where

(i == gAverage.size())

Because i is always smaller than gAverage.size() because the range of the for loop is from 0 .. until gAverage.size()-1

You should check

if (i == gAverage.size()-1)

Upvotes: 1

Dimitri Mockelyn
Dimitri Mockelyn

Reputation: 1545

You need to iterate until you reach the element before the last one :

for (int i = 0; i < gAverage.size()-1; ++i){

and

if (i == gAverage.size()-2) {

careful, it's -2 because you'll never reach gAverage.size()-1

Upvotes: 0

Mureinik
Mureinik

Reputation: 311163

Just stop your iteration a step earlier:

int size = gAverage.size() - 1;
for (int i = 0; i < size; ++i){
    if (i == size - 1) {
        cout << gAverage[i];
    } else{
        cout << gAverage[i] << ", ";

    }
}
cout << " = " << average << endl;

Upvotes: 1

Related Questions