使用C+的编程原理和实践+;第4章练习,第7步 我正在通过C++编程原理和实践来工作,我被困在第4章的第7步。我在这里也发现了类似的问题,但转换单位/值并查看哪个大/小有些不对劲。程序运行正常,但由于某些原因,某些转换不正确,例如,如果我输入2 m,然后2 ft.2 ft作为较大的值返回

使用C+的编程原理和实践+;第4章练习,第7步 我正在通过C++编程原理和实践来工作,我被困在第4章的第7步。我在这里也发现了类似的问题,但转换单位/值并查看哪个大/小有些不对劲。程序运行正常,但由于某些原因,某些转换不正确,例如,如果我输入2 m,然后2 ft.2 ft作为较大的值返回,c++,C++,我知道代码可能看起来有点难看,如果我能让它工作,我会把转换放在一个函数中。提前谢谢 int main() { double doubNum = 0; double smallestNum = ' '; double largestNum = 0; string unitOfDistance = " "; double testNum = 0; cout << "Enter a distance with a unit of measure (ft, in, cm, m): ";

我知道代码可能看起来有点难看,如果我能让它工作,我会把转换放在一个函数中。提前谢谢

int main() {
double doubNum = 0;
double smallestNum = ' ';
double largestNum = 0;
string unitOfDistance = " ";
double testNum = 0;

cout << "Enter a distance with a unit of measure (ft, in, cm, m): ";

while (cin >> doubNum >> unitOfDistance) { //while tests to see if the input is a double and unit is legal

    //check the unitOfDistance and convert all values to cm and hold in testNum for comparison
    if (unitOfDistance == "in") { //in to cm
        testNum = doubNum * 2.54;
    }
    else if (unitOfDistance == "ft") { //ft to cm
        testNum = (doubNum * 12) * 2.54;
    }
    else if (unitOfDistance == "cm") { //cm
        testNum = doubNum;
    }
    else if (unitOfDistance == "m") { //m to cm
        testNum = doubNum * 100;
    }
    else {
        cout << "I don't know that unit.\n";
        return 0;
    }

    //check to see if testNum (the converted version of doubNum) is the smallest/largest/same value entered so far
    if (testNum < smallestNum) {
        smallestNum = doubNum;
        cout << smallestNum << " " << unitOfDistance << " is the smallest distance entered so far.\n";

    }
    else if (testNum > largestNum) {
        largestNum = doubNum;
        cout << largestNum << " " << unitOfDistance << " is the largest distance entered so far.\n";

    }
    else {
        cout << smallestNum << " " << unitOfDistance << " is the smallest distance entered so far.\n";
        cout << largestNum << " " << unitOfDistance << " is the largest distance entered so far.\n";
    }

    cout << "Enter another distance with unit: \n";
}}
intmain(){
双双双对数=0;
双最小数=“”;
双最大数=0;
字符串距离单位=”;
双testNum=0;
cout>doubNum>>unitOfDistance){//,同时测试输入是否为双精度且单位是否合法
//检查距离单位,将所有值转换为cm,并保留在testNum中进行比较
如果(距离单位==“in”){//in到cm
testNum=doubNum*2.54;
}
否则,如果(距离单位==“英尺”){//英尺到厘米
testNum=(doubNum*12)*2.54;
}
else如果(距离单位==“cm”){//cm
testNum=doubNum;
}
else如果(距离单位==“m”){//m到cm
testNum=doubNum*100;
}
否则{

您的代码几乎没有问题:

1.
double smallestNum=''
应替换为double
smallestNum=DBL_MAX
(或非常大的值)。
2.正如您使用了
largestNum
smallestNum
来跟踪最大值和最小值一样,您还需要使用
unitoflargest
unitOfSmallestDistance
来跟踪它们对应的单位。
三,

对于其他两种情况也是如此。

试试这个

#include <iostream>
#include <limits>
using namespace std;

int main() {
    double num, result, smallest, largest;
    smallest = numeric_limits<double>::max();
    largest = numeric_limits<double>::min();
    string unit;
    cout << "Enter a distance with a unit of measure (ft, in, cm, m): ";
    while (cin >> num >> unit) {
        if (unit == "in")       // in to cm
            result = num * 2.54;
        else if (unit == "ft")  // ft to cm
            result = (num * 12) * 2.54;
        else if (unit == "cm")  // cm
            result = num;
        else if (unit == "m")   // m to cm
            result = num * 100;
        else {
            cout << "I don't know that unit.\n";
            break;
        }
        smallest = min(smallest, result);
        largest = max(largest, result);
        cout << smallest << " cm is the smallest distance entered so far.\n";
        cout << largest << " cm is the largest distance entered so far.\n";
    }
    return 0;
}
输出

Enter a distance with a unit of measure (ft, in, cm, m):
200 cm is the smallest distance entered so far.
200 cm is the largest distance entered so far.
91.44 cm is the smallest distance entered so far.
200 cm is the largest distance entered so far.
15.24 cm is the smallest distance entered so far.
200 cm is the largest distance entered so far.

double smallestNum='';
?我认为错误在于
double smallestNum='';
就像@Mat提到的那样。但是你试过调试或其他什么吗?这正是我需要的。谢谢!
#include <iostream>
#include <limits>
using namespace std;

int main() {
    double num, result, smallest, largest;
    smallest = numeric_limits<double>::max();
    largest = numeric_limits<double>::min();
    string unit;
    cout << "Enter a distance with a unit of measure (ft, in, cm, m): ";
    while (cin >> num >> unit) {
        if (unit == "in")       // in to cm
            result = num * 2.54;
        else if (unit == "ft")  // ft to cm
            result = (num * 12) * 2.54;
        else if (unit == "cm")  // cm
            result = num;
        else if (unit == "m")   // m to cm
            result = num * 100;
        else {
            cout << "I don't know that unit.\n";
            break;
        }
        smallest = min(smallest, result);
        largest = max(largest, result);
        cout << smallest << " cm is the smallest distance entered so far.\n";
        cout << largest << " cm is the largest distance entered so far.\n";
    }
    return 0;
}
2 m
3 ft
6 in
Enter a distance with a unit of measure (ft, in, cm, m):
200 cm is the smallest distance entered so far.
200 cm is the largest distance entered so far.
91.44 cm is the smallest distance entered so far.
200 cm is the largest distance entered so far.
15.24 cm is the smallest distance entered so far.
200 cm is the largest distance entered so far.