9.2 键盘输入

 

9.2 键盘输入

下面介绍几种常用的键盘输入方法。

9.2.1 使用预定义的提取符

最常用的键盘输入方法是将提取符作用在标准输入流对象 cin 上。其格式如下:

cin >> 表达式 >> 表达式 ...

提取符可以连续写多个,每个后面跟一个表达式,通常是获得输入值的变量或对象。

示例:使用预定义的提取符进行键盘输入
#include <iostream>
using namespace std;

int main() {
    int a, b;
    cout << "Please enter two integers: ";
    cin >> a >> b;
    cout << "(" << a << ", " << b << ")" << endl;
    return 0;
}

输出结果

Please enter two integers: 5 6
(5, 6)

在这个例子中,用户输入两个整数 56,程序将它们分别存储在变量 ab 中,并输出。

9.2.2 使用成员函数 get() 获取一个字符

get() 函数可以从输入流获取一个字符,并将其存放在指定变量中。

示例:使用 get() 获取字符
#include <iostream>
using namespace std;

int main() {
    char ch;
    cout << "Input: ";
    while ((ch = cin.get()) != EOF) {
        cout.put(ch);
    }
    cout << "ok!";
    return 0;
}

输出结果

abcu xyz
123
ok!

9.2.3 使用成员函数 getline() 读取一行字符

getline() 函数用于从输入流中读取一行字符。

示例:使用 getline() 读取一行字符
#include <iostream>
using namespace std;

const int SIZE = 80;

int main() {
    int lcnt = 0, lmax = -1;
    char buf[SIZE];
    cout << "Input...\n";
    while (cin.getline(buf, SIZE)) {
        int count = cin.gcount();
        lcnt++;
        if (count > lmax) lmax = count;
        cout << "Line #" << lcnt << "\t" << "chars read: " << count << endl;
        cout.write(buf, count).put('\n').put('\n');
    }
    cout << endl;
    cout << "Total lines: " << lcnt << endl;
    cout << "Longest line: " << lmax << endl;
    return 0;
}

输出结果

Input...
this is a string.
Line #1 chars read: 18
this is a string.

you are a student.
Line #2 chars read: 19
you are a student.

the four seasons of the year.
Line #3 chars read: 30
the four seasons of the year.

change to a No.332 bus.
Line #4 chars read: 25
change to a No.332 bus.

Total lines: 4
Longest line: 30

在这个例子中,getline() 函数从输入流中逐行读取字符,并统计每行的字符数。最后输出输入的总行数和最长行的字符数。

9.2.4 使用成员函数 read() 读取一串字符

read() 函数用于从输入流中读取指定数目的字符,并将它们存放在指定的数组中。

示例:使用 read() 读取一串字符
#include <iostream>
using namespace std;

int main() {
    const int S = 80;
    char buf[S] = "";
    cout << "Input...\n";
    cin.read(buf, S);
    cout << endl;
    cout << buf << endl;
    return 0;
}

输出结果

Input...
abcd
efgh
ijkl

abcd
efgh
ijkl

在这个例子中,read() 函数从输入流中读取指定数量的字符并存储在 buf 数组中,然后输出该数组的内容。

9.2.5 使用成员函数 peek() 返回下一个字符

peek() 函数从输入流中返回下一个字符,但不提取它。

示例:使用 peek() 返回下一个字符
#include <iostream>
using namespace std;

int main() {
    int ch, cnt = 0;
    cout << "Input...\n";
    while ((ch = cin.get()) != EOF) {
        if (ch == 'a' && cin.peek() == 'b')
            cnt++;
    }
    cout << endl;
    cout << cnt << endl;
    return 0;
}

输入

mabyababcabcdab
Ctrl+Z

输出结果

5

在这个例子中,peek() 函数检查字符 a 后面是否是字符 b,如果是则计数器 cnt 加1。程序最后输出字符串中包含的 ab 子串的数量。

结论

C++ I/O流库提供了多种方法来处理键盘输入,通过使用预定义的提取符、get()getline()read()peek() 等成员函数,可以灵活地读取用户输入的数据。这些功能使得C++程序能够高效地与用户交互,为开发提供了极大的便利。希望本文对C++ I/O流库中键盘输入方法的理解有所帮助,为编写更灵活、更易维护的C++程序提供支持。

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏驰和徐策

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值