Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

Any help would be appreciated. Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
192 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...