looping commas

here i have portion of code where im supposed to list data with commas but i am having a lot of difficulty getting rid of the beginning comma for some reason.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	else if (choice == 2)
		{
			cout << "Show teams with at least how many wins?";
			cin >> ww;
			for (int i = 0; i < x; i++)
			{
				if ((list[i].wins >= entry))
				{
				
					if (list[i].name > mark[0].name)
					{
						cout << ", " << list[i].name;
					}
					else
					{
						cout << list[i].name;
					}
				}
			}
		}
Last edited on
I dont have your whole code so I cant accurately guess the output of the rest of code, but

in line 21

cout << ", " << list[i].name;

could be placed cout << list[i].name << ", "; there and it might work (it depends on your previous cout statement)

also your else statement misses a comma

14
15
16
17
else
					{
						cout << list[i].name;//no comma here
					}

might be helpful

that could work but it has to print where the comma will loop but it cannot end wit a comma
okay

then you could add a flag and if the code is executing for the first time then don't display the comma

somewhat like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
else if (choice == 2)
		{
int flag=0;
			cout << "Show teams with at least how many wins?";
			cin >> ww;
			for (int i = 0; i < x; i++)
			{
				if ((list[i].wins >= entry))
				{
				
					if (list[i].name > mark[0].name)
					{
                                               flag++;
                                             if (flag==1)
						cout << list[i].name;
                                                        else
                                                                 cout << ", " << list[i].name;
					}
					else
					{
						cout << list[i].name;
					}
				}
			}
		}


the same thing you can do with your else statement (if you want a comma there too )

hope it helps
Topic archived. No new replies allowed.