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

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

问题描述

我刚开始使用 C++ 中的数组,但在获取数组的第一个字符时遇到问题.

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

这是我的代码,

1- 我输入一个名字,例如Jim"

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);

因此,如果您在不带任何参数的情况下调用 ignore 函数,默认情况下它会忽略输入中的 '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天全站免登陆