如何正确评估 C++ 中的用户输入?
How to correctly evaluate user input in C++?
在我的 cpp 文件中,我包含以下内容:
#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
它适用于没有空格的字符串(但我仍然遇到错误)。现在使用 getline
它根本不起作用。
你必须正确使用getline:
cin.getline(celestial);
编辑:我为完全错误道歉。
getline(cin, celestial);
您以正确的方式使用了 getline。 但是在第一次使用 "cin" 之后您没有清理它的缓冲区。因此,当你使用getline时,程序会读取之前存储在cin缓冲区中的内容,然后程序结束。
要解决此问题,您必须在用户输入重量后包含 cin.ignore() 函数。那将是:
cin >> weight;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
第一个参数表示如果none个字符是第二个参数,则要忽略的最大字符数。如果 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;
}
在我的 cpp 文件中,我包含以下内容:
#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
它适用于没有空格的字符串(但我仍然遇到错误)。现在使用 getline
它根本不起作用。
你必须正确使用getline:
cin.getline(celestial);
编辑:我为完全错误道歉。
getline(cin, celestial);
您以正确的方式使用了 getline。 但是在第一次使用 "cin" 之后您没有清理它的缓冲区。因此,当你使用getline时,程序会读取之前存储在cin缓冲区中的内容,然后程序结束。
要解决此问题,您必须在用户输入重量后包含 cin.ignore() 函数。那将是:
cin >> weight;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
第一个参数表示如果none个字符是第二个参数,则要忽略的最大字符数。如果 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;
}