c语言getline函数什么意思,详解C++ cin.getline函数
cin
虽然可以使用 cin 和 >> 运算符来输入字符串,但它可能会导致一些需要注意的问题。
当 cin 读取数据时,它会传递并忽略任何前导白色空格字符(空格、制表符或换行符)。一旦它接触到第一个非空格字符即开始阅读,当它读取到下一个空白字符时,它将停止读取。
例:
// This program illustrates a problem that can occur if
// cin is used to read character data into a string object.
#include
#include // Header file needed to use string objects
using namespace std;
int main()
{
string name;
string city;
cout << "Please enter your name: ";
cin >> name;
cout << "Enter the city you live in: ";
cin >> city;
cout << "Hello, " << name << endl;
cout << "You live in " << city << endl;
return 0;
}
预期结果:
Please enter your name: John Doe
Enter the city you live in: Chicago
Hello, John Doe
You live in Chicago
实际结果:
Please enter your name: John Doe
Enter the city you live in: Hello, John
You live in Doe
在这个示例中,用户根本没有机会输入 city 城市名。因为在第一个输入语句中,当 cin 读取到 John 和 Doe 之间的空格时,它就会停止阅读,只存储 John 作为 name 的值。在第二个输入语句中, cin 使用键盘缓冲区中找到的剩余字符,并存储 Doe 作为 city 的值。
cin.getline()
cin.getline 允许读取包含空格的字符串。它将继续读取,直到它读取至最大指定的字符数,或直到按下了回车键。
此函数会一次读取多个字符(包括空白字符)。它以指定的地址为存放第一个读取的字符的位置,依次向后存放读取的字符,直到读满N-1个,或者遇到指定的结束符为止。若不指定结束符,则默认结束符为'\n'。
这个函数有三个参数,其语法为:cin.getline(字符指针(char*),字符个数N(int),结束符(char));
第一个参数为第一个读取的字符的位置,通常为数组名。
第二个参数为读取的字符的个数。
第三个参数是结束符,可以省略,省略则默认为回车键结束。
例:
// This program demonstrates cinT s getline function
// to read a line of text into a C-string.
#include 、
using namespace std;
int main()
{
const int SIZE = 81;
char sentence[SIZE];
cout << "Enter a sentence: ";
cin.getline (sentence, SIZE);
cout << "You entered " << sentence << endl;
return 0;
}
输出结果:
Enter a sentence: To be, or not to be, that is the question.
You entered To be, or not to be, that is the question.
可以看到,使用cin.getline函数输入带有空格的字符串。
在网络编程中,写一个简单的回射程序时,可以使用cin.getline来输入数据。
#define MAX_LINE 10000
char SendBuffer[MAX_LINE];
cin.getline(SendBuffer, sizeof(SendBuffer));
以上就是详解C++ cin.getline函数的详细内容,更多关于cin.getline函数的资料请关注得牛网其它相关文章!
c语言getline函数什么意思,详解C++ cin.getline函数相关推荐
- python get函数用法_详解python中get函数的用法(附代码)
描述 Python 字典 get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法:dict.get(key, default=None) 参数 key – 字典中要查找 ...
- c语言memcopy_C语言中memcpy 函数的用法详解
C语言中memcpy 函数的用法详解 memcpy(内存拷贝函数) c和c++使用的内存拷贝函数,memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址 ...
- 函数中{}输出格式详解(C#)
Console.WriteLine()函数中{}输出格式详解(C#) Console.WriteLine()函数的格式一直没怎么注意.今天同事问起Console.WriteLine({0:D3},a) ...
- php的可变函数,php之可变函数的实例详解
php之可变函数的实例详解 php的可变函数,今天大概的了解下,是看php手册总结的,觉得用处不大: PHP 支持可变函数的概念.这意味着如果一个变量名后有圆括号,PHP 将寻找与变量的值同名的函数, ...
- php simplexml_load_file 详解,php中simplexml_load_file函数用法实例讲解
php中simplexml_load_file函数用法实例讲解 发布于 2015-02-07 06:53:40 | 136 次阅读 | 评论: 0 | 来源: 网友投递 PHP开源脚本语言PHP(外文 ...
- strncpy()函数用法及其详解
strcpy()函数用法及其详解 strcpy()和strcat()函数都有相同的问题,他们都不能检验目标空间是否能够容纳源字符串的副本. 所以,拷贝字符串用strncpy()函数更加安全 描述: C ...
- pthread_create函数的详细讲解(包括向线程函数传递参数详解)
pthread_create是UNIX环境创建线程函数 头文件 #include<pthread.h> 函数声明 int pthread_create(pthread_t*restrict ...
- php函数find的用法,c语言find函数的用法详解
c语言find函数的用法详解 C语言之find()函数 find函数用于查找数组中的某一个指定元素的位置. 比如:有一个数组[0, 0, 5, 4, 4]: 问:元素5的在什么位置,find函数 返回 ...
- c语言memset函数用法,C语言memset函数使用方法详解
C语言memset函数使用方法详解 一.函数原形 void * memset(void*s, int ch,size_t n) 二.函数作用 将以s内存地址为首的连续n个字节的内容置成ch,一 ...
最新文章
- Linux下rz/sz安装及使用方法
- Java:ThreadPoolExecutor解析续--Executors
- 【CI】CN.一种多尺度协同变异的微粒群优化算法
- Light OJ 1007
- c语言volatile关键字的作用是什么?
- OpenLayer学习之矢量地图
- Oracle数据库重启后密码失效的问题(r12笔记第91天)
- Kubernetes v1.10.x HA 全手动安装教程(TL;DR)
- Part1: Specification of Required Functions
- 在Macbook M1上安装Parallels Desktop 17虚拟机和Originlab 2021
- EF Code First Migrations数据库迁移 (转帖)
- pageoffice动态模板填充JAVA,使用卓正PageOffice--动态填充Word文档
- 2020校招途家名宿开发笔试
- The page at ‘xxx‘ was loaded over HTTPS, but requested an insecure resource ‘xxx‘.
- 路由器运行python脚本_在路由器中运行Python
- Linux入门级操作实例--vi和vim编辑器的使用
- word2016设置整行背景颜色
- 调用企业微信API,ios端异常
- 多线程爬取NCBI数据库文献
- 搭建运营工单数据分析系统