从输入中获得一个数字和一个单元作为一个双和一个字符串——编程:C++的原理和实践 我一直在通过Bjarne Stroustrup的编程:原则和实践,用C++来为我自己的利益。这不是家庭作业,也不是学校用的

从输入中获得一个数字和一个单元作为一个双和一个字符串——编程:C++的原理和实践 我一直在通过Bjarne Stroustrup的编程:原则和实践,用C++来为我自己的利益。这不是家庭作业,也不是学校用的,c++,string,input,double,C++,String,Input,Double,我对第四章中的这个练习已经束手无策了。我应该从输入中获取一个数字和一个单位,在while循环中将数字存储为double,将单位存储为字符串,并跟踪到目前为止看到的最大和最小数字。它适用于一个字符(如m或g)的单位,但当我输入两个字符的单位(如cm或ft)时,循环结束,程序终止。下面是我的代码: #include <iostream> using namespace std; int main() { double temp = 0; string unit = "

我对第四章中的这个练习已经束手无策了。我应该从输入中获取一个数字和一个单位,在while循环中将数字存储为double,将单位存储为字符串,并跟踪到目前为止看到的最大和最小数字。它适用于一个字符(如m或g)的单位,但当我输入两个字符的单位(如cm或ft)时,循环结束,程序终止。下面是我的代码:

#include <iostream>

using namespace std;

int main()
{
    double temp = 0;
    string unit = " ";
    double largest = 0;
    double smallest = 0;

    while (cin >> temp >> unit)
    {
        if (largest == 0 && smallest == 0)
        {
            largest = temp;
            smallest = temp;
            cout << "That's the largest number seen so far.\n";
            cout << "That's the smallest number seen so far.\n";
        }
        else if (temp >= largest)
        {
            largest = temp;
            cout << "That's the largest number seen so far.\n";
        }
        else if (temp <= smallest)
        {
            smallest = temp;
            cout << "That's the smallest number seen so far.\n";
        }
        else
        {
            cout << temp << '\n';
        }

    }
    return 0;
}
我真的很感激能帮我解决这个问题。它快把我逼疯了。

我需要在编译之前包含它,并且我添加了额外的printf stmt用于调试

但是您的代码应该可以工作:一个字符,或者多个字符

这是我修改过的代码:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    double temp = 0;
    string unit = " ";
    double largest = 0;
    double smallest = 0;

    while (cin >> temp >> unit)
    {
        cout << "NEXT: temp=" << temp << ", unit=" << unit << "\n";
        if (largest == 0 && smallest == 0)
        {
            largest = temp;
            smallest = temp;
            cout << "That's the largest number seen so far.\n";
            cout << "That's the smallest number seen so far.\n";
        }
        else if (temp >= largest)
        {
            largest = temp;
            cout << "That's the largest number seen so far.\n";
        }
        else if (temp <= smallest)
        {
            smallest = temp;
            cout << "That's the smallest number seen so far.\n";
        }
        else
        {
            cout << temp << '\n';
        }

    }
    cout << "DONE: temp=" << temp << ", unit=" << unit << ", smallest=" << smallest << ", largest=" << largest << "\n";
    return 0;
}
我需要在编译之前包含它,并且我添加了额外的printf stmts用于调试

但是您的代码应该可以工作:一个字符,或者多个字符

这是我修改过的代码:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    double temp = 0;
    string unit = " ";
    double largest = 0;
    double smallest = 0;

    while (cin >> temp >> unit)
    {
        cout << "NEXT: temp=" << temp << ", unit=" << unit << "\n";
        if (largest == 0 && smallest == 0)
        {
            largest = temp;
            smallest = temp;
            cout << "That's the largest number seen so far.\n";
            cout << "That's the smallest number seen so far.\n";
        }
        else if (temp >= largest)
        {
            largest = temp;
            cout << "That's the largest number seen so far.\n";
        }
        else if (temp <= smallest)
        {
            smallest = temp;
            cout << "That's the smallest number seen so far.\n";
        }
        else
        {
            cout << temp << '\n';
        }

    }
    cout << "DONE: temp=" << temp << ", unit=" << unit << ", smallest=" << smallest << ", largest=" << largest << "\n";
    return 0;
}

好的,谢谢大家的帮助


问题似乎与编译器/系统有关。我的代码与OSX的默认编译器有问题,但在Windows 10上使用MinGW作为编译器运行相同的代码,效果完美。多奇怪的虫子啊。如果有人对如何使用默认编译器在OSX上运行此程序有任何建议,我们将不胜感激。

好的,谢谢大家的帮助


问题似乎与编译器/系统有关。我的代码与OSX的默认编译器有问题,但在Windows 10上使用MinGW作为编译器运行相同的代码,效果完美。多奇怪的虫子啊。如果有人对如何使用默认编译器使该程序在OSX上运行有任何建议,我们将不胜感激。

您确定您正在使用您向我们展示的程序,与我们展示的完全一样吗。看起来你的编译器假设一个字符串是一个没有字符串头的字符…@JoachimPileborg如果是这样的话,那么我能想到的唯一问题就是我运行它的环境。我正在使用Xcode并在Xcode的控制台模拟器中测试它。@SergeBallesta include仍然没有解决问题。您确定您正在使用您向我们展示的程序,与您向我们展示的完全一样吗。看起来你的编译器假设一个字符串是一个没有字符串头的字符…@JoachimPileborg如果是这样的话,那么我能想到的唯一问题就是我运行它的环境。我正在使用Xcode并在Xcode的控制台模拟器中测试它。@SergeBallesta include仍然没有解决问题。谢谢您的评论。对我来说还是不行。输入12cm会得到以下结果:完成:温度=0,单位=m,最小值=7,最大值=7。您需要一个介于12和cm之间的空间。因为你需要两个变量,所以你需要一个分隔符来分隔它们。@AnthonyRossello这是因为cin失败了,因为它试图读取一个12厘米的明显字符串,而不是它需要的数字。纠正方法是在12和厘米之间添加空格或回车符,然后将t 12作为数字读入temp,cm作为字符串读入Unit谢谢您的评论。对我来说还是不行。输入12cm会得到以下结果:完成:温度=0,单位=m,最小值=7,最大值=7。您需要一个介于12和cm之间的空间。因为你需要两个变量,所以你需要一个分隔符来分隔它们。@AnthonyRossello这是因为cin失败了,因为它试图读取一个12厘米的明显字符串,而不是它需要的数字。纠正方法是在12和cm之间添加一个空格或回车符,然后将把t12作为数字读入temp,把cm作为字符串读入unitIt's not a compiler bug。请参阅下面我的回答。这不是编译器错误。见下面我的回答。