溫馨提示×

C++輸入數(shù)組如何強制換行

c++
小億
142
2024-02-23 16:39:28
欄目: 編程語言

在C++中,要強制換行輸入的數(shù)組元素,可以使用循環(huán)結構逐個輸入數(shù)組元素,并在每次輸入后使用std::endl強制換行。以下是一個示例代碼:

#include <iostream>

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

    // 輸入數(shù)組元素并強制換行
    for (int i = 0; i < SIZE; i++) {
        std::cout << "Enter element " << i+1 << ": ";
        std::cin >> arr[i];
        std::cout << std::endl;
    }

    // 輸出輸入的數(shù)組元素
    std::cout << "Array elements:" << std::endl;
    for (int i = 0; i < SIZE; i++) {
        std::cout << arr[i] << std::endl;
    }

    return 0;
}

在上面的示例代碼中,循環(huán)遍歷數(shù)組元素并使用std::cin進行輸入,然后在每次輸入后使用std::cout << std::endl;強制換行。最后通過另一個循環(huán)輸出輸入的數(shù)組元素,同樣使用std::cout << std::endl;強制換行。這樣就能實現(xiàn)強制換行輸入數(shù)組元素的效果。

0