微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C++ cin a float before a string 不起作用,但 cin a string before 起作用为什么?

如何解决C++ cin a float before a string 不起作用,但 cin a string before 起作用为什么?

不起作用:

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

int main() {
  
  string planet;
  double weight;

  cout << "What is your earth weight?\n";
  cin >> weight;
  cout << "What planet do you want to fight on? ";
  getline (cin,planet);
  cout << "your planet is " << planet << "\n";
}

作品:

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

int main() {
  
  string planet;
  double weight;

  cout << "What planet do you want to fight on? ";
  getline (cin,planet);
  cout << "your planet is " << planet << "\n";
  cout << "What is your earth weight?\n";
  cin >> weight;
}

以第一种方式运行它会在打印“你想在哪个星球上战斗?”后终止程序。我的问题是:为什么它是第二种方式而不是第一种方式?和c++的编译方式有关系吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。