计划跳过cin.getline() [英] Program skips cin.getline()

查看:251
本文介绍了计划跳过cin.getline()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了这个程序,它让用户ADRESS,姓名和工作。然后,它把所有成一个字符串,并输出字符串。 (我知道有更好的方法来做到这一点)

I have made this program, It get the users adress, name and work. Then It puts it all into one string and outputs that string. (I know there are better ways to do this)

char str[600];
char adrs[200];
char name[10];
char wrk[200];
cout<<"Enter your name and press ENTER: ";
cin.getline(name,10);
cout<<"\nEnter your adress and press ENTER:";
cin.getline(adrs,200);
cout<<"\nEnter your workplace and press ENTER:";
cin.getline(wrk,200);
strcpy(str,"My name is ");
strcat(str,name);
strcat(str,"\nand i live at ");
strcat(str,adrs);
strcat(str, "\nI also work at ");
strcat(str, wrk); strcat(str, "\n\n");
cout<<str<<endl;

下面当我写超过10个字符的程序的确需要第一个9字符的用户输入如我所料,但之后,它跳过所有接下来的 cin.getline()的名称这是在程序并进入输出 STR 键,结束程序。

Here when I write a name that exceeds 10 chars The program does take the first 9 chars the user entered as I expected but after that It skips all the next cin.getline() That are in the program and goes to output str and ends the program.

为什么会出现这种情况,如何解决?

Why does this happen and how to fix it?

推荐答案

每次使用CIN时,它在内存中存储进入的每一个字符,直到遇到一个换行符。该内存块被称为输入缓冲区。您对 cin.getline第一次调用()请求字符串10个字符,包括终止NULL字符。然而,直到他打回车键CIN愉快地读取尽可能多的字符的用户类型。如果用户键入超过9个字符,然后霉素存储在输入缓冲器中的剩余的字符,并在以后的输入操作将使用它们。例如,如果用户键入15个字符,您的来电 cin.getline()存储你的c-字符串数组中的第9。调用 cin.getline()将再次然后继续读一个已经进入了输入的其余部分。

Every time you use cin, it stores every character entered in memory until it encounters a newline character. This block of memory is called the input buffer. Your first call to cin.getline() requests a string with 10 characters, including the terminating NULL character. However, cin happily reads as many characters as the user types until he hits the Enter key. If the user types more than 9 characters, then cin stores the remaining characters in the input buffer and will use them during later input operations. For example, if the user types 15 characters, your call to cin.getline() store the first nine in your c-string array. Calling cin.getline() again will then continue reading the remainder of the input that was already entered.

要解决这个问题,你应该使用 cin.ignore () 以跳过此换行符。我强烈建议你熟悉的C ++库的在线参考。我最喜欢的两个是 http://www.cplusplus.com 并的 HTTP://www.cp$p$pference.com

To fix this problem, you should use cin.ignore() to skip past this newline character. I strongly suggest that you familiarize yourself with online references to the C++ libraries. Two of my favorites are http://www.cplusplus.com and http://www.cppreference.com.

编辑:为了让我的答案是完整的,我还需要添加 cin.getline()将设置故障位如果有在输入缓冲区中的字符数大于请求。使用 CIN 任何额外的输入操作,则必须调用 cin.clear()之前来清除失败位。

In order for my answer to be complete, I also need to add that cin.getline() will set the fail bit if there are more characters in the input buffer than requested. Before using cin for any additional input operations, you must call cin.clear() to clear the fail bit.

这篇关于计划跳过cin.getline()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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