C++ 为什么我的质量计算器不能正确处理空间? #包括 #包括 使用名称空间std; /* 函数名称:weightConv 目的:取重量并将以下数字转换为堆芯重量单位 返回:0 */ 双重量转换(双w,字符串重量单位) { 如果(weightUnit==“g”| | weightUnit==“g”) cout

C++ 为什么我的质量计算器不能正确处理空间? #包括 #包括 使用名称空间std; /* 函数名称:weightConv 目的:取重量并将以下数字转换为堆芯重量单位 返回:0 */ 双重量转换(双w,字符串重量单位) { 如果(weightUnit==“g”| | weightUnit==“g”) cout,c++,string,spaces,C++,String,Spaces,istream::operator>在空白字符上标记。当您输入sh tn时,只有sh将保存在unitType中,tn将保留在流中 当你在流中得到一个sh或long时,检查另一个令牌,看看它是否是tn。一个健壮的解决方案可能是一个Unit类,它有一个自由函数std::istream&operator>(std::istream,Unit&)执行流提取的。std::istream的操作符>>(std::string&),您在此处使用: #include <iostream> #inclu

istream::operator>
在空白字符上标记。当您输入
sh tn
时,只有
sh
将保存在
unitType
中,
tn
将保留在流中


当你在流中得到一个
sh
long
时,检查另一个令牌,看看它是否是
tn
。一个健壮的解决方案可能是一个
Unit
类,它有一个自由函数
std::istream&operator>(std::istream,Unit&)执行流提取的

std::istream
操作符>>(std::string&)
,您在此处使用:

#include <iostream>
#include <string>



using namespace std;

/*
Function Name: weightConv
Purpose: To take the weight and convert the following number to the coressponding weight unit
Return : 0
*/
  double weightConv(double w, string weightUnit)
{

      if (weightUnit == "g" || weightUnit == "G" )
        cout << " Mass = " <<  w * 0.035274 << "oz";
    else if (weightUnit == "oz"||weightUnit == "OZ"||weightUnit == "oZ" ||weightUnit == "Oz")
        cout << " Mass = " <<  w / 28.3495 << "g";
    else if (weightUnit == "kg"||weightUnit == "KG"||weightUnit == "Kg" ||weightUnit == "kG")
        cout << " Mass = " <<  w * 2.20462 << "lb";
    else if (weightUnit == "lb" ||weightUnit == "LB" ||weightUnit== "Lb" ||weightUnit == "lB")
        cout << " Mass = " <<  w * 0.453592 << "kg";
    else if (weightUnit == "Long tn" ||weightUnit == "LONG TN"|| weightUnit == "long tn" || weightUnit == "long ton")
        cout << " Mass = " <<  w * 1.12 << "sh tn";
    else if (weightUnit == "sh tn" || weightUnit == "SH TN")
        cout << " Mass = " << w / 0.892857 << " Long tons";
    else if (weightUnit == "s" || weightUnit == "S")
        cout << " Mass = " << w * 6.35029 << "stones";
    else
        cout << "Is an unknown unit and cannot be converted";

    return 0;   
}// end of weightCov function


int main()
{
    for (;;)
    {

        double mass;
        string unitType;
        cout << "Enter a mass and its unit type indicator(g,kg,lb,oz,long tn,or sh tn)" << endl;
        cin >> mass >> unitType;

            // Output Results
            cout << weightConv(mass, unitType) << endl;

    }// end of for loop
}// end of main 
读取以空格分隔的标记。这意味着当您在输入流中输入
“12 long tn”
时,
mass
将为
12.0
,而
unitType
将为
“long”

您的问题的解决方案可能涉及
std::getline
,如中所示

cin >> mass >> unitType;
这将一直读到下一个换行符。但是,这不会像
操作符>
那样去掉前导空格,因此您将只剩下
“long tn”
,而不是
“long tn”
。您需要显式忽略这些空格,如下所示:

std::cin >> mass;
std::getline(std::cin, unitType);
这最终会给你留下

std::cin >> std::ws;

请注意,这不会删除尾随空格,因此如果用户键入
“12 long tn”
,它将无法识别装置。如果这是一个问题,您必须手动将其从
unitType

的末尾剥离。请在比较之前将用户输入转换为大写或小写。请参阅函数
std::tolower
std::toupper
。这将减少比较次数。此外,请考虑r使用
switch
而不是
if-then-else-if
梯形图。@ThomasMatthews:
switch
仅适用于整数类型,而不是字符串。我的缺点是,我认为OP只识别单个字母。getline函数似乎是一个更合适的解决方案。问题是我以前尝试过使用它,但我尝试过使用它像这样.getline(cin,mass,UnitType)。不幸的是,这会引发一个错误,因为它不喜欢mass有一个double作为数据类型。是否接受它?
getline
进行无格式输入,所以它总是给你一个字符串;你不能要求它有一个
double
,否。
std::cin >> mass >> std::ws;      // read mass, ignore whitespaces
std::getline(std::cin, unitType); // the rest of the line is the unit