我不想在最后一个数字/值后输入逗号,我该如何删除它(C++,loop)

问题描述:

因此,我朋友的程序应该找到第三个乘法,然后用逗号数。而不是最后一个。我不想在最后一个数字/值后输入逗号,我该如何删除它(C++,loop)

int a; 
int b; 

cout<< "First Number = "; 
cin>>a; 
cout<< "Last Number = "; 
cin>>b; 

if(a<=b) 
{ 
    for(a;a<=b;a++) 
    { 
     if(a%3 == 0 && a%2 != 0) 
     { 
      cout<<a; 
     } 
     if(a<b && a%3==0 && a%2 != 0) 
     { 
      cout<< " , "; 
     } 
     else if(a==b) 
     { 
      cout<< "."; 
     } 
    } 
} 

后,我输入

一个= 1

B = 20

这就是我预期

3,9 15.

这就是我实际得到的

3,9,15,。

之前THX,希望你们理解

+4

的可能的复制[打印用逗号C++列出](https://*.com/问题/ 3496982 /打印列表与逗号-c) – Barmar

+1

检查如果一个== b之前cout

+0

这个答案可能有所帮助https://*.com/questions/36137997/convert-vectorunsigned-char -1-2-3 -in-string-1-2-3-as-digits/36138229#36138229 – Galik

我会做这样的事情:

char const* sep = ""; // item separator (nothing to begin with) 

for(a;a<=b;a++) 
{ 
    if(a%3 == 0 && a%2 != 0) 
    { 
     cout << sep << a; // output the separator before the next value 
     sep = ", "; // now change it to a comma 
    } 
} 

cout << "."; // finish with a full stop 
+0

它给了我“错误”sep'不命名类型“和”错误'sep'未在此范围内声明“ – FcIp3

+0

@ FcIp3也许您使用的是旧版本的标准。我编辑了代码,所以现在应该有希望工作。 – Galik

+0

哇,谢谢,现在它的工作 – FcIp3