简体   繁体   English

我如何摆脱最后一个逗号? c ++正在从文件中读取并向后读取数字

[英]How do i get rid of the last comma? c++ is reading from a file and reading the numbers backwards

C++ is reading from a file and printing out the numbers backwards. C++ 正在从文件中读取并向后打印出数字。 i got rid of the comma when it prints out forward.当它向前打印时,我去掉了逗号。 But I cannot figure out how to do the same for back wards.但我不知道如何为后院做同样的事情。

so far this is what I have:到目前为止,这就是我所拥有的:

cout<<"Forwards: ";
for (int i=0; i<cnt; i++){
    cout<<setprecision(2)<< fixed;
    if (i == cnt - 1)
    {
        cout << arr[i];
    }
    else
    {
        cout << arr[i] << ", ";
    }
}
cout<<endl;
//printing backwards 
cout << "Backwards: ";
for (int i=cnt-1; i>=0; i--){
    cout<<setprecision(2)<< fixed;
    if (i == cnt + 1)
    {
        cout << arr[i];
    }
    else
    {
        cout << arr[i] << ", ";
    }

the forwards part comes out correct: 1.00, 2.00, 3.00, 4.00, 5.00, 6.00, 7.00, 8.00, 9.00, 10.00转发部分正确:1.00、2.00、3.00、4.00、5.00、6.00、7.00、8.00、9.00、10.00

but the backwards is what prints out但倒退是打印出来的

10.00, 9.00, 8.00, 7.00, 6.00, 5.00, 4.00, 3.00, 2.00, 1.00, 10.00, 9.00, 8.00, 7.00, 6.00, 5.00, 4.00, 3.00, 2.00, 1.00,

how do I get rid of that last comma我如何摆脱最后一个逗号

Try something like this:尝试这样的事情:

bool first_item = true;
for (unsigned int i = 0u; i < LIMIT; ++i)
{
    if (! first_item)
    {
        std::cout << ", ";
    }
    first_item = false;
    std::cout << number;
}
std::cout << "\n";

The above code fragment works regardless of the direction, ascending or descending.上面的代码片段不管方向,升序还是降序都可以工作。

The important part is the "flag" to determine if the first item has been printed.重要的部分是确定第一项是否已打印的“标志”。

In the forward-moving loop, if (i == cnt - 1) is checking if i is on the last element of the array.在前向循环中, if (i == cnt - 1)正在检查i是否在数组的最后一个元素上。

In the backward-moving loop, you want to check if i is on the first element, but if (i == cnt + 1) will never be true for that check, as cnt+1 is out of bounds.在向后移动的循环中,您想检查i是否在第一个元素上,但if (i == cnt + 1)永远不会为该检查为真,因为cnt+1超出范围。 The first element is at index 0, so use if (i == 0) instead:一个元素位于索引 0 处,因此请改用if (i == 0)

cout << "Forwards: ";
cout << setprecision(2) << fixed;
for (int i = 0; i < cnt; ++i){
    if (i == cnt - 1){
        cout << arr[i];
    }
    else{
        cout << arr[i] << ", ";
    }
}
cout << endl;

cout << "Backwards: ";
cout << setprecision(2) << fixed;
for (int i = cnt-1; i >= 0; --i){
    if (i == 0){
        cout << arr[i];
    }
    else{
        cout << arr[i] << ", ";
    }
}
cout << endl;

Alternatively, print the 1st output value outside of the loop, and then start the loop on the 2nd output value:或者,在循环外打印第一个输出值,然后在第二个输出值上启动循环:

cout << "Forwards: ";
cout << setprecision(2) << fixed;
cout << arr[0];
for (int i = 1; i < cnt; ++i){
    cout << ", " << arr[i];
}
cout << endl;

cout << "Backwards: ";
cout << setprecision(2) << fixed;
cout << arr[cnt-1];
for (int i = cnt-2; i >= 0; --i){
    cout << ", " << arr[i];
}
cout << endl;

Just write写吧

if(i == 0)

instead of代替

if(i == cnt+1)

It will do the work.它会完成这项工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM