C ++ cout cin字符串操作 [英] C++ cout cin string manipulation

查看:143
本文介绍了C ++ cout cin字符串操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从命令行输入一行。我的问题是,我没有得到整条线,但它是由空间来表示。

I'm trying to get a line as input from the command line. My problem is that I'm not getting the whole line, but it's being tokenized by space.

因此,如果我输入的东西,如我喜欢数学很多获得

So if I entered something such as "I like Math a lot" instead of getting

"you enterend: I like Math a lot"

我得到以下结果:

EDITING MODE: Enter a command
i like Math a lot
you entered i

EDITING MODE: Enter a command
you entered like

EDITING MODE: Enter a command
you entered Math

EDITING MODE: Enter a command
you entered a

EDITING MODE: Enter a command
you entered lot


void enterEditingMode(){
    editingMode = TRUE;
    static string CMD = "\nEDITING MODE: Enter a command\n";
    string input;
    while(editingMode == TRUE){
    	cout << CMD;
    	cin >> input;
    	//we assume input is always correct
    	// here we need to parse the instruction
    	cout << "you entered " << input <<endl;


推荐答案

std :: getline 是每次读取一行输入的标准方式。

std::getline is the standard way to read a line of input at a time.

您可以这样使用:

std::getline(std::cin, string);

它返回对输入流的引用,该引用隐式转换为 void * ,以便您可以像这样检查成功:

It returns a reference to the input stream which has an implicit conversion to void* so you can check for success easily like this:

if (std::getline(std::cin, string))
{
    // successfully read a line...
}

这篇关于C ++ cout cin字符串操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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