std::cin 输入有空格? [英] std::cin input with spaces?

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

问题描述

#include <string>

std::string input;
std::cin >> input;

用户想要输入Hello World".但是 cin 在两个单词之间的空格处失败.如何让 cin 接受整个 Hello World?

The user wants to enter "Hello World". But cin fails at the space between the two words. How can I make cin take in the whole of Hello World?

我实际上是在用结构体做这件事,而 cin.getline 似乎不起作用.这是我的代码:

I'm actually doing this with structs and cin.getline doesn't seem to work. Here's my code:

struct cd
{
    std::string CDTitle[50];
    std::string Artist[50];
    int number_of_songs[50];
};

std::cin.getline(library.number_of_songs[libNumber], 250);

这会产生错误.有什么想法吗?

This yields an error. Any ideas?

推荐答案

你必须使用 cin.getline():

You have to use cin.getline():

char input[100];
cin.getline(input,sizeof(input));

这篇关于std::cin 输入有空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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