我不想在最后一个数字/值后输入逗号,我该如何删除它(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,希望你们理解
答
我会做这样的事情:
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
的可能的复制[打印用逗号C++列出](https://*.com/问题/ 3496982 /打印列表与逗号-c) – Barmar
检查如果一个== b之前cout
这个答案可能有所帮助https://*.com/questions/36137997/convert-vectorunsigned-char -1-2-3 -in-string-1-2-3-as-digits/36138229#36138229 – Galik