1. 程式人生 > 其它 >5.5 迴圈和文字輸入

5.5 迴圈和文字輸入

技術標籤:C++ Primer Plus迴圈文字輸入EOFgetcin

程式清單5.16 textin1.cpp
#include <iostream>

int main()
{
    using namespace std;

    char ch;
    int count = 0;

    cout << "Enter characters; enter # to quit:\n";
    cin >> ch;

    while (ch != '#')
    {
        cout << ch;
++count; cin >> ch; } cout << endl << count << " characters read\n"; return 0; }

在這裡插入圖片描述

程式清單5.17 textin2.cpp
#include <iostream>

int main()
{
    using namespace std;

    char ch;
    int count = 0;

    cout << "Enter characters; enter # to quit:\n"
; cin.get(ch); while (ch != '#') { cout << ch; ++count; cin.get(ch); } cout << endl << count << " characters read\n"; return 0; }

在這裡插入圖片描述

另一個cin.get()版本

在這裡插入圖片描述