0

Doesn't Work:

#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";
}

Works:

#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;
}

Running it the first way terminates the program after printing "What planet do you want to fight on?". My question is: why does it work the second way but not the first way? Does it have something to do with how c++ is compiled?

Mar
  • 3
  • 2

0 Answers0