简体   繁体   English

使用C ++的编程原理和实践第4章钻取,第7步

[英]Programming Principles and Practice Using C++ Chapter 4 Drill, Step 7

I'm working my way through Programming Principles and Practice Using C++ and I'm stuck on step 7 of the chapter 4 drill. 我正在使用C ++进行编程原理和实践,目前仍处于第4章练习的第7步。 I've found similar questions here, but something isn't working right with converting the units/values and seeing which is larger/smaller. 我在这里发现了类似的问题,但是在转换单位/值并查看更大/更小的值时,某些方法无法正常工作。 The program runs fine, but for some reason certain conversions aren't coming back correct such as if I enter 2 m, and then 2 ft. 2 ft comes back as the larger value. 该程序运行正常,但由于某些原因,某些转换无法正确返回,例如,如果我输入2 m,然后2 ft。2 ft作为较大的值返回。

I know the code probably looks a bit ugly, I'll put the conversion in a function if I can get this to work. 我知道代码看起来可能很难看,如果可以使转换生效,我会将其转换为函数。 Thanks in advance. 提前致谢。

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

Try this 尝试这个

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

Input 输入项

2 m
3 ft
6 in

Output 输出量

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.

There are few problems with your code : 您的代码有几个问题:
1. double smallestNum = ' ' should be replaced with double smallestNum = DBL_MAX (or a really large value). 1.应该用double smallestNum = DBL_MAX (或非常大的值)替换double smallestNum = DBL_MAX double smallestNum = ' '
2.Just as you have used largestNum and smallestNum to keep track of largest and smallest values, you also need to use unitOfLargestDistance and unitOfSmallestDistance to keep track of their corresponding units. 2.就像您使用了largestNumsmallestNum来跟踪最大值和最小值一样,您还需要使用unitOfLargestDistanceunitOfSmallestDistance来跟踪其对应的单位。
3. 3。

if (testNum < smallestNum) {
    smallestNum = doubNum;
    cout << smallestNum << " " << unitOfDistance << " is the smallest distance entered so far.\n";
}

Here you need to update smallestNum with testNum not doubNum . 在这里,你需要更新smallestNumtestNumdoubNum So it should be like: 所以应该像这样:

if (testNum < smallestNum) {
    smallestNum = testNum;
    cout << doubNum << " " << unitOfDistance << " is the smallest distance entered so far.\n";
}

Similarly for the other 2 conditions. 对于其他2个条件,情况类似。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编程:原理与实践使用C ++第4章钻取第6步:关于数值范围的一般问题 - Programming: Principles and Practice Using C++ chapter 4 drill step 6 : General question about numeric range 使用C ++的编程原理和实践第4章练习1 - Programming Principles and Practice using c++ chapter 4 drill 1 在“编程:使用C ++的原理和实践”(第4章)的第7章中使用“ cin”获得7号钻的不同结果 - Getting different results using “cin” for Drill #7 in Chapter 4 of Programming: Principles and Practice using C++ (Stroustrup) 第6章使用c ++的实践和原理[钻]使用令牌(计算器) - chapter 6 practice and principles using c++ [drill] using tokens (calculator) 编程原理与实践:第4章演练第1部分 - Programming Principles and Practice: chapter 4 drill part 1 不理解 Bjarne Stroustrup 的第 6 章“编程:使用 C++ 的原理和实践”中的这部分代码 - Don't understand this part of code from "Programming: principles and practice using C++", chapter 6 by Bjarne Stroustrup 编程-使用C ++的原理和实践-第3章尝试本练习-重复单词检测 - Programming - Principles and Practice using C++ - Chapter 3 Try this exercise - Repeated word detection “使用C ++的原理和实践”章节6.3.1代码错误? - “Principles and Practice Using C++” chapter 6.3.1 code error? 使用C ++循环钻取的编程原理和实践。 找不到退出循环的方法 - Programming Principles and Practice using C++ while-loop drill. Can't find a way to exit the loop 使用C++编程原理与实践报错:constexpr - Programming principles and practice using C++ error: constexpr
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM