For loop prints an extra comma
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.
Printing extra comma in while loop - Python
This is one possible solution:
countWhile = 5
myList = []
while countWhile > 0:
if countWhile == 0:
break
else:
myList.append(str(countWhile))
countWhile -= 1
print(', '.join(myList))
print("While Loop Finished.")
I can try to look for another simpler one if you want.
How to remove extra comma after the last number in loop
For example
for(int i=0; i < something.length; i++){
System.out.print(i + i==something.length-1 ? "":" ,");
}
How to avoid last comma in python loop
Pass the results as separate arguments and specify the separator:
print(*result, sep=',')
How do I add a comma after every word in a for loop
You can produce a list with multiple elements by using the *
operator. Then you can join()
them together:
for i in range(10):
print(', '.join([user_input] * (i + 1)))
Related Topics
Does Std::Cout Have a Return Value
Find Two Elements in an Array That Sum to K
Overloading Operator<< for a Templated Class
How to Save Two Camera's Data But Not Influence Their Picture-Acquire Speed
Std::Remove_If - Lambda, Not Removing Anything from the Collection
Ways to Detect Whether a C++ Virtual Function Has Been Redefined in a Derived Class
What Is the Purpose of a Declaration Like Int (X); or Int (X) = 10;
When Is C++ Covariance the Best Solution
Is There a Standard Date/Time Class in C++
Two Classes That Refer to Each Other
How to Run the Preprocessor on Local Headers Only
Algorithm to Add or Subtract Days from a Date
Can a Variable Be Declared Both Static and Extern
Cmake: Project Structure with Unit Tests
How Does a Sentinel Node Offer Benefits Over Null