STD :: CIN输入带空间?

发布于 2025-01-31 05:27:31 字数 482 浏览 3 评论 0原文

#include <string>

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

用户想输入“ Hello World”。但是cin在两个单词之间的空间上失败。如何使cin纳入整个Hello World

我实际上是使用structs和cin.getline做到这一点的。这是我的代码:

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

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

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

#include <string>

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

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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

离旧人 2025-02-07 05:27:32

使用:

getline(cin, input);

该功能可以在

#include <string>

Use :

getline(cin, input);

the function can be found in

#include <string>
韵柒 2025-02-07 05:27:32

您想在CIN中使用.getLine函数。

#include <iostream>
using namespace std;

int main () {
  char name[256], title[256];

  cout << "Enter your name: ";
  cin.getline (name,256);

  cout << "Enter your favourite movie: ";
  cin.getline (title,256);

  cout << name << "'s favourite movie is " << title;

  return 0;
}

there 。检查一下,以获取更多信息和示例。

You want to use the .getline function in cin.

#include <iostream>
using namespace std;

int main () {
  char name[256], title[256];

  cout << "Enter your name: ";
  cin.getline (name,256);

  cout << "Enter your favourite movie: ";
  cin.getline (title,256);

  cout << name << "'s favourite movie is " << title;

  return 0;
}

Took the example from here. Check it out for more info and examples.

鹿! 2025-02-07 05:27:32

如何从输入中读取字符串?

您可以读取一个带有std :: cin的单个空格终止的单词,例如:

#include<iostream>
#include<string>
using namespace std;

int main()
{
    cout << "Please enter a word:\n";

    string s;
    cin>>s;

    cout << "You entered " << s << '\n';
}

请注意,没有明确的内存管理,没有您可能会溢出的固定尺寸缓冲区。
如果您确实需要一条线(而不仅仅是一个单词),则可以做到这一点:

#include<iostream>
#include<string>
using namespace std;

int main()
{
    cout << "Please enter a line:\n";

    string s;
    getline(cin,s);

    cout << "You entered " << s << '\n';
}

How do I read a string from input?

You can read a single, whitespace terminated word with std::cin like this:

#include<iostream>
#include<string>
using namespace std;

int main()
{
    cout << "Please enter a word:\n";

    string s;
    cin>>s;

    cout << "You entered " << s << '\n';
}

Note that there is no explicit memory management and no fixed-sized buffer that you could possibly overflow.
If you really need a whole line (and not just a single word) you can do this:

#include<iostream>
#include<string>
using namespace std;

int main()
{
    cout << "Please enter a line:\n";

    string s;
    getline(cin,s);

    cout << "You entered " << s << '\n';
}
与君绝 2025-02-07 05:27:32

C方式

您可以使用get 在cstdio(c)中找到的函数:

#include<cstdio>
int main(){

char name[256];
gets(name); // for input
puts(name);// for printing 
}

c ++方式

get在C ++ 11中删除。

[推荐]:您可以使用 getline(cin,name) String.h
cin.getline(name,256) iostream中本身。

#include<iostream>
#include<string>
using namespace std;
int main(){

char name1[256];
string name2;
cin.getline(name1,256); // for input
getline(cin,name2); // for input
cout<<name1<<"\n"<<name2;// for printing
}

THE C WAY

You can use gets function found in cstdio(stdio.h in c):

#include<cstdio>
int main(){

char name[256];
gets(name); // for input
puts(name);// for printing 
}

THE C++ WAY

gets is removed in c++11.

[Recommended]:You can use getline(cin,name) which is in string.h
or cin.getline(name,256) which is in iostream itself.

#include<iostream>
#include<string>
using namespace std;
int main(){

char name1[256];
string name2;
cin.getline(name1,256); // for input
getline(cin,name2); // for input
cout<<name1<<"\n"<<name2;// for printing
}
关于从前 2025-02-07 05:27:32

我宁愿使用以下方法获取输入:

#include <iostream>
#include <string>

using namespace std;

int main(void) {
    string name;

    cout << "Hello, Input your name please: ";
    getline(cin, name);

    return 0;
}

它实际上非常易于使用,而不是定义包含空格字符的字符串的总长度。

I rather use the following method to get the input:

#include <iostream>
#include <string>

using namespace std;

int main(void) {
    string name;

    cout << "Hello, Input your name please: ";
    getline(cin, name);

    return 0;
}

It's actually super easy to use rather than defining the total length of array for a string which contains a space character.

挖个坑埋了你 2025-02-07 05:27:31

它不会“失败”;它只是停止阅读。它将词汇令牌视为“字符串”。

使用 std :: getline getline

#include <string>
#include <iostream>

int main()
{
   std::string name, title;
   
   std::cout << "Enter your name: ";
   std::getline(std::cin, name);
   
   std::cout << "Enter your favourite movie: ";
   std::getline(std::cin, title);
   
   std::cout << name << "'s favourite movie is " << title;
}

请注意,请注意这与不是std :: istream :: getline相同,它与C-style char char buffer而不是std: :String s。

更新

您的编辑问题与原件几乎没有相似之处。

您正在尝试将getline纳入int,而不是字符串或字符缓冲区。流的格式操作仅适用于运算符&lt;操作员&gt;&gt;。要么使用其中一个(并因此使用getline,然后词法转换为int,要么使用getline,要么使用其中一个。

It doesn't "fail"; it just stops reading. It sees a lexical token as a "string".

Use std::getline:

#include <string>
#include <iostream>

int main()
{
   std::string name, title;
   
   std::cout << "Enter your name: ";
   std::getline(std::cin, name);
   
   std::cout << "Enter your favourite movie: ";
   std::getline(std::cin, title);
   
   std::cout << name << "'s favourite movie is " << title;
}

Note that this is not the same as std::istream::getline, which works with C-style char buffers rather than std::strings.

Update

Your edited question bears little resemblance to the original.

You were trying to getline into an int, not a string or character buffer. The formatting operations of streams only work with operator<< and operator>>. Either use one of them (and tweak accordingly for multi-word input), or use getline and lexically convert to int after-the-fact.

摘星┃星的人 2025-02-07 05:27:31

您必须使用 cin.getline() ::

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

You have to use cin.getline():

char input[100];
cin.getline(input,sizeof(input));
我的影子我的梦 2025-02-07 05:27:31

标准库提供了一个称为ws的输入函数,该功能从输入流中消耗了空格。您可以这样使用:

std::string s;
std::getline(std::cin >> std::ws, s);

The Standard Library provides an input function called ws, which consumes whitespace from an input stream. You can use it like this:

std::string s;
std::getline(std::cin >> std::ws, s);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文