函数getline忽略输入的第一个字符 [英] Getline ignoring first character of input

查看:210
本文介绍了函数getline忽略输入的第一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用C ++的数组和我有得到一个数组的第一个字符的一个问题。

I'm just starting with arrays in C++ and I'm having a problem getting the first character of an array.

这是我的code,

1 - 我输入一个名称,如吉姆

1- I enter a name, such as "Jim"

char name[30];
cin.ignore();
cin.getline(name, 30);

2 - 马上我试着来清点阵列

2- Right away I try to cout the array

    cout<<"NAME:"<<name; // THIS PRINTS 'im'

我确信这将打印J。我在做什么错了?

I was sure it would print 'J'. What am I doing wrong?

推荐答案

下面是cin.ignore的签名:

Here is signature of cin.ignore:

istream& ignore (streamsize n = 1, int delim = EOF);

所以,如果你忽略函数不带任何参数,它将忽略由输入默认的'1'字符。在这种情况下,忽略J。删除忽略打电话,你会得到吉姆。

So if you call ignore function without any parameters, it will ignore '1' char by default from input. In this case it ignored 'J'. Remove ignore call and you will get 'Jim'.

这篇关于函数getline忽略输入的第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆