如何正确评估C ++中的用户输入?

时间:2015-09-05 21:16:30

标签: c++ input output

在我的cpp文件中,我包含以下内容:

unsigned int integer_div_round_nearest(unsigned int numerator, unsigned int denominator)
{
    unsigned int rem;
    unsigned int floor;
    unsigned int denom_div_2;

    // check error cases
    if(denominator == 0)
        return 0;

    if(denominator == 1)
        return numerator;

    // Compute integer division and remainder
    floor = numerator/denominator;
    rem = numerator%denominator;

    // Get the rounded value of the denominator divided by two
    denom_div_2 = denominator/2;
    if(denominator%2)
        denom_div_2++;

    // If the remainder is bigger than half of the denominator, adjust value
    if(rem >= denom_div_2)
        return floor+1;
    else
        return floor;
}

我提示用户输入

#include <cstdlib>

#include <iostream>
#include <string>
#include <math.h>

然后我有以下陈述:

double weight;
cout << "What is your weight? \n";
cin >> weight;

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

当我运行代码时,我得到以下内容:

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

在获取输入方面我做错了什么?我最初使用read from master failed : Input/output error 为没有空格的字符串工作(但我仍然有错误)。现在使用cin << celestial它根本不起作用。

1 个答案:

答案 0 :(得分:0)

你必须正确使用getline:

cin.getline(celestial);

编辑:我为完全不正确而道歉。

getline(cin, celestial);

你以正确的方式使用了getline。然而在使用&#34; cin&#34;你第一次不清理缓冲区。因此,当您使用getline时,程序会读取之前存储在cin缓冲区中的内容并且程序结束。

要解决此问题,您必须在用户输入权重后包含cin.ignore()函数。那将是:

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

第一个参数表示如果这些字符都不是第二个参数,则要忽略的最大字符数。如果cin.ignore()找到第二个参数,那么它之前的所有字符都将被忽略,直到它到达(包括它)。

所以最终的程序看起来像这样:

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