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

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

问题描述

#include <string>

string input;
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?

我实际上是用structs和 cin.getline 似乎不工作。这是我的代码:

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

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

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天全站免登陆