In my cpp file, I include the following:
#include <cstdlib>
#include <iostream>
#include <string>
#include <math.h>
I prompt the user for input
double weight;
cout << "What is your weight? \n";
cin >> weight;
string celestial;
cout << "Select a celestial body: \n";
getline(cin, celestial);
Then I have the following statements:
if (celestial == "Mercury")
{
g_ratio = g_mercury / g_earth;
wt_on_celestial = g_ratio * weight;
cout << "Your weight on Mercury would be " << wt_on_celestial << " kilograms.";
}
else if (celestial == "Venus")
{
g_ratio = g_venus / g_earth;
wt_on_celestial = g_ratio * weight;
cout << "Your weight on Venus would be " << wt_on_celestial << " kilograms.";
}
else if (celestial == "The moon")
{
g_ratio = g_moon / g_earth;
wt_on_celestial = g_ratio * weight;
cout << "Your weight on the moon would be " << wt_on_celestial << "kilograms.";
}
When I run the code, I get the following:
read from master failed
: Input/output error
What am I doing wrong in regards to getting the input? I initially used cin << celestial
which worked for the strings without spaces (but I still got an error). Now using getline
it doesn't work at all.