变量可能未初始化/未初始化时使用 我正在编写C++中的英文度量转换器程序,我尝试使用“尝试”、“抛出”、“catch”来拒绝“主”函数中的负数/非数值。p>

变量可能未初始化/未初始化时使用 我正在编写C++中的英文度量转换器程序,我尝试使用“尝试”、“抛出”、“catch”来拒绝“主”函数中的负数/非数值。p>,c++,try-catch,C++,Try Catch,我的两个问题是: 1.) 每当我进入控制台,比如说“g”,我就会得到一个输出:0英寸等于0厘米,然后我就会得到我想要弹出的错误显示。我只需要输出错误显示 2.)当我输入一个负数,如-3,当我想让它告诉我输入无效时,我得到了作为负数的正确转换 这是我的密码 #include <iostream> #include <cctype> char menuSelect(); using namespace std; int main(int argc, const char *

我的两个问题是:

1.) 每当我进入控制台,比如说“g”,我就会得到一个输出:0英寸等于0厘米,然后我就会得到我想要弹出的错误显示。我只需要输出错误显示

2.)当我输入一个负数,如-3,当我想让它告诉我输入无效时,我得到了作为负数的正确转换

这是我的密码

#include <iostream>
#include <cctype>
char menuSelect();
using namespace std;

int main(int argc, const char * argv[])
{
    double inches;
    double centimeters;
    char select;
    try
    {
        do
        {

            if (centimeters < 0.0 || inches < 0.0)
                throw 0;
            if (!cin)
                throw 0;

            select = menuSelect();
            if (select == 'E')
            {
                cout << "Enter the number of inches: ";
                cin >> inches;
                centimeters = inches * 2.54;
                cout << inches << " inches is equal to " << centimeters
                        << " centimeters." << endl;
            }
            else if (select == 'M')
            {
                cout << "Enter the number of centimeters: ";
                cin >> centimeters;
                inches = centimeters / 2.54;
                cout << centimeters << " Centimeters is equal to " << inches
                        << " inches." << endl;
            }

        } while (select != 'Q');

    }
    catch (int errID)
    {
        cout << "Error: " << errID << endl;
        cout << "Please enter a positive number. ";
    }
    return 0;

}
#包括
#包括
char menuSelect();
使用名称空间std;
int main(int argc,const char*argv[]
{
双英寸;
双厘米;
字符选择;
尝试
{
做
{
如果(厘米<0.0 | |英寸<0.0)
掷0;
如果(!cin)
掷0;
选择=菜单选择();
如果(选择=='E')
{
不能>英寸;
厘米=英寸*2.54;

cout应该是这样的:(我在输入之后添加了一个标志
boolbneg=false
if(inches>=0)
if(cm>=0)
,如果“E”或“M”集的输入为负数
bNeg=true

bool bNeg=false;
如果(选择=='E')
{
不能>英寸;
如果(英寸>=0){//对负值没有意义
厘米=英寸*2.54;

无法复制:“对'menuSelect()'的未定义引用”请。英寸和厘米在您第一次使用时都是未初始化的。如果您想验证您的输入,您需要在您实际接受它的地方进行验证,而不是在循环的另一个迭代中。不要同时测试
厘米<0.0
英寸<0.0
。您可能只设置了其中一个。如果您想
厘米<代码>检查<代码>英寸不是必需的,你可以在代码<英寸英寸>代码>中使用旧的值负值出错。请不要在主程序路径上使用控制流的异常。这不利于C++。在读取结果之前和在输出结果后,检查输入是否无效。按照程序执行的顺序逐行检查程序,并向其解释所有内容。我建议标记并解释您更改的内容以及更改的原因。答案看起来不错,但除了代码之外,提问者可能什么也学不到,只会逐步提高剪切和粘贴技能。
    bool bNeg = false;

    if (select == 'E')
    {
        cout << "Enter the number of inches: ";
        cin >> inches;
        if (inches>=0) { // No meaning to negative values
            centimeters = inches * 2.54;
            cout << inches << " inches is equal to " << centimeters
                << " centimeters." << endl;
        }
        else bNeg = true;

    }
    else if (select == 'M')
    {
        cout << "Enter the number of centimeters: ";
        cin >> centimeters;
        if (centimeters>=0) { // No meaning to negative values
            inches = centimeters / 2.54;
            cout << centimeters << " Centimeters is equal to " << inches
                << " inches." << endl;
        }
        else bNeg = true;
    }

    if (bNeg) {
        // tbd: say what you want to say
    }