3

Program displays a list of numbers. For example:

1, 3, 5, 3, 4, 3,

I need to be like this:

1, 3, 5, 3, 4, 3

I tried the if/else statement, but its no good.

s.push_back(0);
cout << "s: ";
cout << 0 << ", ";
s.push_back(v[0]);
cout << s.back() << ", ";

for(int i=2; i<=10; i++) {
    s.push_back(alpha * v[i-1] + (1 - alpha) * s.back());
    if (i == s.back() - 1)
        cout << s.back() << endl;
    else
        cout << s.back() << ", " << "";

I'm getting the correct output. Just having trouble with getting rid of the last comma.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
WahidXD
  • 39
  • 2
  • 1
    Have a look at [`std::experimental::ostream_joiner`](https://en.cppreference.com/w/cpp/experimental/ostream_joiner), which solves this exact problem – Remy Lebeau Apr 18 '19 at 04:17

3 Answers3

4

Avoid the if by updating a delimiter varialbe

#include <iostream>
#include <vector>
#include <functional>

int main() {
  std::vector<int> example = {1,2,3,4,5};

  std::string delim = "";

  for(const auto& e:example){
    std::cout << delim << e;
    delim = ", ";
  }

  return 0;
}
Gardener
  • 2,591
  • 1
  • 13
  • 22
  • I agree that the `if` is more readable, but I find it interesting as we deal with this issue so many times in programming -- that to find a method of avoiding the `if` altogether is interesting. – Gardener Apr 18 '19 at 05:15
  • @StoryTeller: I have to agree with you, so I have edited it. – Gardener Apr 18 '19 at 05:31
3

If you place the comma before every element except the first you will achieve this

 if(first){
    out << n;
    first = false;
 }else{
    out << "," << n;
 }
Fire Crow
  • 7,499
  • 4
  • 36
  • 35
  • Pay attention to this answer because you'll encounter this problem many times in your career. I can't count how many times I've encountered it in different variations. – Carey Gregory Apr 18 '19 at 04:41
2

There are three ways:

  1. Print the comma before the element, unless it's the first:

    bool first = true;
    for ...
       if (!first) 
           cout << ", ";
       cout << s.back();
       first = false;
    
  2. Similar, but without a boolean:

    const char * prefix = "";
    for ...
          cout << prefix << s.back();
          prefix = ", ";
    
  3. Check the index:

     for ...  
         ....
         if (i > 0)
            cout << ", ";
         cout << s.back();
    

    Index checking can work also on the last element, when printing the comma at the end of each element.

  4. Print the first element outside the loop, and the remaining element will be prefixed by the delimiter in the loop (note that this does not work for empty lists).

Michael Veksler
  • 8,217
  • 1
  • 20
  • 33
  • A 4th option is to output the 1st vector element without the delimiter before entering the loop, then have the loop iterations output the delimiter in front of subsequent elements. `cout << s.back(); for (...) { cout << ", " << s.back(); }` – Remy Lebeau Apr 18 '19 at 06:18
  • @RemyLebeau good point – Michael Veksler Apr 18 '19 at 06:20