0

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.

Doug Foster
  • 17
  • 1
  • 7
  • 5
    [Why does std::getline() skip input after a formatted extraction?](http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – chris Sep 05 '15 at 21:19

1 Answers1

0

You have to make the correct use of getline:

cin.getline(celestial);

Edit: I apologize for being totally incorrect.

getline(cin, celestial);

You have used getline the right way. However after using "cin" the first time you don't clean its buffer. Therefore, when you use getline, the program reads what had been stored in the cin buffer before and program ends.

To solve this problem you have to include the cin.ignore() function after the user inputs the weight. That would be:

cin >> weight;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

The first parameter represents the maximum amount of characters to be ignored if none of this characters is the second parameter. If cin.ignore() finds the second parameter, all of the characters before it will be ignored until it's reached (including it).

So the final program could look like this:

#include <iostream>
#include <limits>

#define g_earth 9.81
#define g_mercury 3.7
#define g_venus 8.87
#define g_moon 1.63

using namespace std;

int main (void)
{
    float wt_on_celestial, g_ratio;

    double weight;
    cout << "What is your weight? ";
    cin >> weight;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    string celestial;
    cout << "Select a celestial body: ";
    getline(cin, celestial);
    cout << "\n";

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

    return 0;
}