溫馨提示×

c++ cin.getline如何配合循環(huán)使用

c++
小樊
95
2024-07-10 10:44:23
欄目: 編程語言

你可以使用cin.getline結合循環(huán)來不斷輸入多行數(shù)據(jù),具體步驟如下所示:

#include <iostream>
using namespace std;

int main() {
    const int MAX_SIZE = 100;
    char input[MAX_SIZE];

    int i = 0;
    while (i < 5) {  // 假設要輸入5行數(shù)據(jù)
        cout << "請輸入第" << i+1 << "行數(shù)據(jù):" << endl;
        cin.getline(input, MAX_SIZE);  // 輸入一行數(shù)據(jù)

        // 處理輸入的數(shù)據(jù)
        cout << "您輸入的數(shù)據(jù)是:" << input << endl;

        i++;
    }

    return 0;
}

在這個示例中,我們使用while循環(huán)來連續(xù)輸入5行數(shù)據(jù)。在每次循環(huán)中,使用cin.getline來獲取用戶輸入的一行數(shù)據(jù),并將其保存在input數(shù)組中。接著可以進行對輸入數(shù)據(jù)的處理或其他操作,然后繼續(xù)下一輪循環(huán)。

0