getline的一个大问题 [英] A big problem with getline

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

问题描述

我正在尝试读取两个字符串,一个接一个,在我读第二个之前,我使用cout.flush()清理缓冲区,但我不知道为什么程序没有读取第二个字符串,甚至不是使用cin的第三个值,请我需要你的帮助。 。 。 。 。 。这是程序:


#include< stdio.h>

#include< cctype>

#include< iostream>

#include< string>

使用std :: string;

使用命名空间std;




int main(){

int x;

string a,b;


cout<< 输入您的名字: << endl;

cout.flush();

getline(cin,a);


cout<< 输入你的姓氏: << endl;

cout.flush();

getline(cin,b);


cout<< 输入您的年龄: << endl;

cout.flush();

cin>> x;

返回0;


}


感谢您的帮助。 。 。 。 。 。 。

解决方案

我无法重现你的问题。这段代码对我运行正常。


你可以尝试的东西是放一个cin.clear()。在每个getline之后。这将解决换行符留在cin缓冲区中的可能性。


嗨!!!!!!!代码不能正常工作,我做了你告诉我的修改,我用Microsoft Visual C ++编译它,它不能正常工作,这里是代码:


#包括< stdio.h>

#include< cctype>

#include< iostream>

#include< string>

使用std :: string;

使用命名空间std;



int main(){

int x;

string a,b;


cout<< 输入您的名字: << endl;

cout.flush();

getline(cin,a);

cin.clear();


cout<< 输入你的姓氏: << endl;

cout.flush();

getline(cin,b);

cin.clear();


cout<< 输入您的年龄: << endl;

cout.flush();

cin>> x;


cout<< 你的名字是: << a<< endl;

cout<< 你的姓氏是: << b<< endl;

cout<< 你的年龄是: << x<<结束;


返回0;


}


这是输出:


输入你的名字:

Edwin


输入你的姓:

Alvear

输入您的年龄:

您的姓名是:Edwin

您的姓氏是:

您的年龄是:-858993460

按任意键继续


感谢您的帮助。 。 。

Hi, I am trying to read two string, one after another, and before I read the second one I clean the buffer using cout.flush(), but I don''t know why the program does not read the second string, not even the the third value using cin, Please I need your help. . . . . . this is the program:

#include<stdio.h>
#include <cctype>
#include <iostream>
#include <string>
using std::string;
using namespace std;



int main(){
int x;
string a,b;

cout << "Enter your first name: " << endl;
cout.flush();
getline(cin,a);

cout << "Enter your surname: " << endl;
cout.flush();
getline(cin,b);

cout << "Enter your age: " << endl;
cout.flush();
cin >> x;
return 0;

}

Thanks for your help. . . . . . .

解决方案

I can''t reproduce your problem. This code runs fine for me.


Something you may try is putting a "cin.clear()" after each getline. This would address the possiblity that the newline character is being left in the cin buffer.


Hi!!!!!!! the code is not working properly, I did the modifications that you have told me and I Compile it using Microsoft Visual c++, and it does not work well, here is the code:

#include<stdio.h>
#include <cctype>
#include <iostream>
#include <string>
using std::string;
using namespace std;



int main(){
int x;
string a,b;

cout << "Enter your first name: " << endl;
cout.flush();
getline(cin,a);
cin.clear();

cout << "Enter your surname: " << endl;
cout.flush();
getline(cin,b);
cin.clear();

cout << "Enter your age: " << endl;
cout.flush();
cin >> x;

cout << "Your Name is: " << a << endl;
cout << "Your Surname is: " << b << endl;
cout << "Your age is: " << x << endl;


return 0;

}

And this is the output:

Enter your first name:
Edwin

Enter your surname:
Alvear
Enter your age:
Your Name is: Edwin
Your Surname is:
Your age is: -858993460
Press any key to continue

Thanks for your help. . . .


这篇关于getline的一个大问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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