1
cout<<"Set B : {";
for(i=0;i<b;i++)
{
    cout<<setB[i];
    cout<<",";
}
cout<<" }"<<endl;

The code above is not printing correctly. It should print Set B : {1,2,3} but it prints an extra comma ==> Set B : {1,2,3,}

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Max Smith
  • 13
  • 2
  • 4

9 Answers9

15

Use

cout << "Set B : {";

for (i = 0; i < b; ++i) {
  if (i > 0) cout << ",";

  cout << setB[i];
}

cout << " }" << endl;

I changed your algorithm :

Before it meant : "Put the number and then put a comma"

Now it means : "If there is a number behind me put a comma, then put the number"

Before, you always printed a comma when you printed a number so you had an extra comma.

Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
4

For each iteration of the for loop, the program is going to execute -everything- inside the for loop. So, your loop runs through and prints each number in your set and then a comma.

The problem is that even on your last run through the loop, it is going to print a comma, because it's part of the loop.

cout << "Set B : {";
for(i = 0; i < b; i++){
    cout << setB[i];
        if (i < (b-1))
            cout << ",";
}
cout << " }" << endl;

This code will run the exact same, except the second to last time it runs through the loop, it will not print a comma. No need to get too fancy. :)

2

Personally I like this solution better. You first print out the first element and then a , [second element].

cout <<"Set B : {" << setB[0];
for(i = 1; i < b; i++)
{
    cout << ",";
    cout<<setB[i];
}
cout  << " }" << endl;

Warning!: This will NOT work if the array is empty.

yizzlez
  • 8,757
  • 4
  • 29
  • 44
  • 2
    +1 Works nicely for this case, but have to be careful in general - `setB[0]` would be undefined behaviour if `setB` were empty. – Tony Delroy Mar 28 '14 at 02:14
1

The loop code prints a pair of number and comma. Try using this one:

cout<<"Set B : {";
for(i=0;i<b;i++)
{
    cout<<setB[i];
    if(i < b-1) cout<<",";
}
cout<<"}"<<endl;
Kenneth Bastian
  • 843
  • 1
  • 8
  • 10
1

You're loop is executing the cout << "," 3 times. The following will give what you want:

#include <iostream>

using namespace std;

int main(){

int setB[] = {1,2,3};

cout<<"Set B : {";
for(int i=0;i<3;i++)
{
    cout<<setB[i];
    if ( i < 2 )
        cout<<",";
}
cout<<" }"<<endl;

return 0;
}
RichS
  • 913
  • 4
  • 12
  • Agreed. The code I laid out here was not optimized by any means. Just to clarify my explanation. – RichS Mar 28 '14 at 02:20
1

The way I often deal with these loops where you want to put something like a space or a comma between a list of items is like this:

int main()
{
    // initially the separator is empty
    auto sep = "";

    for(int i = 0; i < 5; ++i)
    {
        std::cout << sep << i;
        sep = ", "; // make the separator a comma after first item
    }
}

Output:

0, 1, 2, 3, 4

If you want to make it more speed efficient you can output the first item using an if() before entering the loop to output the rest of the items like this:

int main()
{
    int n;

    std::cin >> n;

    int i = 0;

    if(i < n) // check for no output
        std::cout << i;

    for(++i; i < n; ++i) // rest of the output (if any)
        std::cout << ", " << i; // separate these
}
Galik
  • 47,303
  • 4
  • 80
  • 117
1

An other way, without extra branch:

std::cout << "Set B : {";
const char* sep = "";
for (const auto& e : setB) {
    std::cout << sep << setB[i];
    sep = ", ";
}
std::cout <<" }" << std::endl;
Jarod42
  • 203,559
  • 14
  • 181
  • 302
1

I really like to promote the use of a range library to write declarative code instead of nested for-if statements in an imperative style.

#include <range/v3/all.hpp>
#include <vector>
#include <iostream>
#include <string>

int main()
{
    using namespace ranges;
    std::vector<int> const vv = { 1,2,3 };

    auto joined = vv | view::transform([](int x) {return std::to_string(x);}) 
                     | view::join(',');
    std::cout << to_<std::string>(joined) << std::endl;

    return 0;
}
Jens
  • 9,058
  • 2
  • 26
  • 43
0

If you can use STL, try the following:

#include <iterator>
#include <iostream>

int main() {
    int setB[]{1,2,3};
    std::cout << "Set B : { ";
    for(auto i = std::begin(setB), e = std::end(setB); i != e;) {
        std::cout << *i;
        for(++i; i !=e; ++i) { std::cout << ", " << *i; }
    }
    std::cout << " }" << std::endl;
    return 0;
}
mtadd
  • 2,495
  • 15
  • 18