Hey guys.

This is really frustrating because it seems easy but I can't seem to find the correct function to remove this element of my string.

void set::display() const
// pre : none
// post : displays the elements of this set enclosed in braces and
//        separated by commas eg {1,2,3} or {} (do not use backspace, delete etc)
{
     string s1 = ",";
     cout << "{";
     for (int i = 0; i < setlist.size(); i++)
     {
         if (setlist.at(i) == true)
         {
            cout << i;
            cout << s1;
         }
     }
     cout << "}";
}

At the moment it will output something similar to this:
{1,2,3,} --> notice the , after the last digit?

It needs to be this:
{1,2,3} --> no ',' after the last digit.


Any help would be appreciated,
Regards.
:)

Member Avatar for iamthwee

Well at the end can't you just do an string erase on the last but one element?

string p = "{1,2,3,24,}";
p.erase(p.length()-2,1);

That's true, but how will I be able to declare string p? Because there is a for loop present.. so it would have to store the output as a string somehow which I'm not too sure how to go about :S

EDIT: The display() function is different every time because setlist is having elements added to it.. so the digits between the brackets will always be different.

Member Avatar for iamthwee

I think your easiest answer would be

vector<int>setlist;
    setlist.push_back(3);
    setlist.push_back(12);
    setlist.push_back(4);


     string s1 = ",";
     cout << "{";
     for (int i = 0; i < setlist.size(); i++)
     {
          cout << setlist[i];
         if (i!=setlist.size()-1)
         {

            cout << s1;
         }
     }
     cout << "}";

     cin.get();
commented: 5240 posts, and no code-tags! :P -1
Member Avatar for iamthwee

oops i meant

vector<bool>setlist;
    setlist.push_back(true);
    setlist.push_back(false);
    setlist.push_back(true);
    setlist.push_back(true);
    
    
     string s1 = ",";
     cout << "{";
     for (int i = 0; i < setlist.size(); i++)
     {
         
         if (setlist.at(i) == true)
         {
            cout << i;
            
         }

         if (i!=setlist.size()-1)
         {
           
            cout << s1;
         }
     }
     cout << "}";
     
     cin.get();

1.
vector<bool>setlist;
2.
setlist.push_back(true);
3.
setlist.push_back(false);
4.
setlist.push_back(true);
5.
setlist.push_back(true);
6.

7.
int i

8.
string s1 = ",";
9.
cout << "{";
10.
for (i = 0; i < setlist.size(); i++)
11.
{
12.

13.
if (setlist.at(i) == true)
14.
{
15.
cout << i;
16.

17.
}
18.

19.
if (i!=setlist.size()-1)
20.
{
21.

22.
cout << s1;
23.
}
24.
}
25.
i--;
cout << "}";
26.

27.
cin.get();

I'd switch the order of printing around. First print a comma, then print the number.
This does require you to find the first number beforehand, though.

void set::Display() const
{
    cout << '{';
    
    int i = 0;
    while(i < setlist.size() && setlist.at(i) != true)
        i++;
    if(i != setlist.size())
        cout << i;
    i++;
    for(i; i < setlist.size(); i++)
        if(setlist.at(i) == true)
        {
            cout << ", ";
            cout << i;
        }
    
    cout << '}';
}

Easy way out, just put a condition to check if "i" is at .size() - 1;
if not then print or else not print.

1. void set::display() const
2. // pre : none
3. // post : displays the elements of this set enclosed in braces and
4. // separated by commas eg {1,2,3} or {} (do not use backspace, delete etc)
5. {
6. string s1 = "";
7. cout << "{";
8. for (int i = 0; i < setlist.size(); i++, s1 = ",")
9. {
10. if (setlist.at(i) == true)
11. {
12. cout << s1;
13. cout << i;
14. }
15. }
16. cout << "}";
17. }

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.