如何从循环中的cout中删除不需要的文本? C ++(图片)

时间:2014-08-04 00:38:54

标签: c++

截图:

enter image description here

如果显示“2 + 1 + 3 +的总和为6”,我想在3之后摆脱最后的“+”符号,因为它显然看起来不太好。有没有人知道如何摆脱它?

提前谢谢!

    cout << "The sum of ";

    for (i = 1; i <= InputLimit; i++) // This loop shows all user entered 'elements' of the array

        cout << numbers[i] << " + ";

    cout << " is " << sum << ". ";

1 个答案:

答案 0 :(得分:-1)

只需提出条件:

for (i = 0; i <= InputLimit; i++){ // This is just the loop to show the numbers with '+' or no.
        if (i != InputLimit){
                cout << numbers[i] << " + ";
        }
        else {
                cout << numbers[i];
        }
}

我希望它适合你XD