C++ cin 问题。不捕获用户的输入

发布于 2024-08-26 07:34:34 字数 684 浏览 4 评论 0原文

我有以下方法,它不会捕获用户的任何内容。如果我输入新乐队作为艺术家姓名,它只会捕获“新”而忽略“乐队”。如果我使用 cin.getline() 而不是捕获任何内容。有什么想法如何解决这个问题吗?

char* artist = new char [256];

char * getArtist()
{
    cout << "Enter Artist of CD: " << endl;
    cin >> artist;      
    cin.ignore(1000, '\n');
    cout << "artist is " << artist << endl;
    return artist;
}

这效果很好。谢谢罗杰

std::string getArtist()

{   

    cout << "Enter Artist of CD: " << endl;

    while(true){            

        if ( getline(cin, artist)){

        }

    cout << "artist is " << artist << '\n';

    }

    return artist;

}

I have the following method which is not capturing anything from the user.If I input New Band for the artist name, it only captures "New" and it lefts out "Band". If I use cin.getline() instead nothing is captured. Any ideas how to fix this?

char* artist = new char [256];

char * getArtist()
{
    cout << "Enter Artist of CD: " << endl;
    cin >> artist;      
    cin.ignore(1000, '\n');
    cout << "artist is " << artist << endl;
    return artist;
}

This worked just fine. Thank you Roger

std::string getArtist()

{   

    cout << "Enter Artist of CD: " << endl;

    while(true){            

        if ( getline(cin, artist)){

        }

    cout << "artist is " << artist << '\n';

    }

    return artist;

}

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

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

发布评论

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

评论(3

乱世争霸 2024-09-02 07:34:34
std::string getArtist() {
  using namespace std;
  while (true) {
    cout << "Enter Artist of CD: " << endl;
    string artist;
    if (getline(cin, artist)) {             // <-- pay attention to this line
      if (artist.empty()) { // if desired
        cout << "try again\n";
        continue;
      }
      cout << "artist is " << artist << '\n';
      return artist;
    }
    else if (cin.eof()) { // failed due to eof
      // notice this is checked only *after* the
      // stream is (in the above if condition)

      // handle error, probably throw exception
      throw runtime_error("unexpected input error");
    }
  }
}

整个事情是一个普遍的改进,但使用 getline 可能对你的问题来说是最重要的。

void example_use() {
  std::string artist = getArtist();
  //...

  // it's really that simple: no allocations to worry about, etc.
}
std::string getArtist() {
  using namespace std;
  while (true) {
    cout << "Enter Artist of CD: " << endl;
    string artist;
    if (getline(cin, artist)) {             // <-- pay attention to this line
      if (artist.empty()) { // if desired
        cout << "try again\n";
        continue;
      }
      cout << "artist is " << artist << '\n';
      return artist;
    }
    else if (cin.eof()) { // failed due to eof
      // notice this is checked only *after* the
      // stream is (in the above if condition)

      // handle error, probably throw exception
      throw runtime_error("unexpected input error");
    }
  }
}

The whole thing is a general improvement, but the use of getline is possibly the most significant for your question.

void example_use() {
  std::string artist = getArtist();
  //...

  // it's really that simple: no allocations to worry about, etc.
}
嘿哥们儿 2024-09-02 07:34:34

这是指定的行为; istream 只能读取到一个空格或换行符。如果您想要整行,您可以使用 getline 方法,正如您已经发现的那样。

另外,请在任何新的 C++ 代码中使用 std::string 而不是 char*,除非有充分的理由。在这种情况下,它将帮助您避免缓冲区溢出等各种问题,而无需您付出任何额外的努力。

This is the specified behaviour; istreams only read up to a space or a newline. If you want an entire line, you use the getline method, as you already discovered.

Also, please use std::string instead of char* in any new C++ code, unless there are very good reasons otherwise. In this case, it will save you from all kinds of problems like buffer overflows, without any extra effort on your part.

め可乐爱微笑 2024-09-02 07:34:34

如果您要在输入中包含空格分隔符,则需要使用 getline 供您输入。这会让你的忽略变得不必要。

If you're going to have white space separators in your input, you need to use getline for your input. That would make your ignore unnecessary.

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