How to force newline when inputting an array in C++?

In C++, to force a new line when inputting array elements, you can use a loop to input each element one by one, and at each input, force a new line using std::endl. Here is an example code:

#include <iostream>

int main() {
    const int SIZE = 5;
    int arr[SIZE];

    // 输入数组元素并强制换行
    for (int i = 0; i < SIZE; i++) {
        std::cout << "Enter element " << i+1 << ": ";
        std::cin >> arr[i];
        std::cout << std::endl;
    }

    // 输出输入的数组元素
    std::cout << "Array elements:" << std::endl;
    for (int i = 0; i < SIZE; i++) {
        std::cout << arr[i] << std::endl;
    }

    return 0;
}

In the example code above, a loop iterates through the array elements to input using std::cin, and then forces a newline using std::cout << std::endl; after each input. Finally, another loop is used to output the input array elements, once again forcing a newline using std::cout << std::endl;. This achieves the effect of forcing a newline while inputting array elements.

bannerAds