【问题标题】:Can't find the bug/probleem in my program [duplicate]在我的程序中找不到错误/问题[重复]
【发布时间】:2016-08-02 19:58:03
【问题描述】:

我正在制作我的第一个 C++ 项目。这是一个简单的温度转换器。

我用 if 语句制作了一个测试部分 [代码 1]。 if 语句将比较用户输入。例如,如果您用户键入 c,然后键入 k(Celsius-Kelvin)。它应该运行函数[code 2] CtoK();但我没有运行所有功能为什么要这样做?

它尝试使用 return 但我没有(它也没有给出错误,所以我保留了它)

如果你们看到别的东西请说Code on pastebin

也想牢记: 刚说要学C++ 不是英语母语,所以如果有拼写和语法错误,请说出来,以便我学习它

[代码 1]

void whatToWhat(char firstDegrees, char secondDegrees) {
    if (firstDegrees == 'C' || 'c') {// tests if the user want form c to f
        if (secondDegrees == 'F' || 'f') {
            CtoF();
        }
    }if (firstDegrees == 'C' || 'c') {// tests if the user want form c to k
        if (secondDegrees == 'K' || 'k') {
            CtoK();
        }
    }if (firstDegrees == 'F' || 'f') {// tests if the user want form f to c
        if (secondDegrees == 'C' || 'c') {
            FtoC();
        } 
    }if (firstDegrees == 'F' || 'f') {// tests if the user want form f to k
        if (secondDegrees == 'K' || 'k') {
            FtoK();
        }
    }if (firstDegrees == 'K' || 'k') {// tests if the user want form k to f
        if (secondDegrees == 'F' || 'f') {
            KtoF();
        }
    }if (firstDegrees == 'K' || 'k') {// tests if the user want form k to c
        if (secondDegrees == 'C' || 'c') {
            KtoC();
        }
    }
}

[代码 2]

 void CtoF() {// c to f furmula
    double input;
    cout << "Enter a number[Celsius-Fahrenheit]" << endl;
    cin >> input;
    cout << "it's " << input * 1.8 + 32 << " Fahrenheit " << endl;
    return;
}
void CtoK() {// c to k  furmula
    double input;
    cout << "Enter a number[Celsius-Kelvin]" << endl;
    cin >> input;
    cout << "it's " << input + 273.15   << " Kelvin " << endl;
    return;
}
void FtoC() {//f to c furmula
    double input;
    cout << "Enter a number[Fahrenheit-Celsius]" << endl;
    cin >> input;
    cout << "it's " << input / 1.8 - 32 << " Celsius " << endl;

}
void FtoK() {//f to k furmula
    double input;
    cout << "Enter a number[Fahrenheit-Kelvin]" << endl;
    cin >> input;
    cout << "it's " << input / 1.8 - 32 + 273.15 << " Kelvin " << endl;
    return;
}
void KtoF() {// k to f furmula
    double input;
    cout << "Enter a number[Kelvin-Fahrenheit]" << endl;
    cin >> input;
    cout << "it's " << (input - 273.15) * 1.8 + 32 << " Fahrenheit " << endl;

}
void KtoC() {// k to c furmula
    double input;
    cout << "Enter a number[Kelvin-Celsius]" << endl;
    cin >> input;
    cout << "it's " <<273.15 - input << " Celsius " << endl;
    return;
}

【问题讨论】:

  • firstDegrees == 'C' || 'c' - 比较不是这样工作的......可能你想要firstDegrees == 'C' || firstDegrees == 'c'
  • 1) 如果你有超过 1 个相关的if,你应该有else ifelse。 2)if (firstDegrees == 'C' || 'c')我相信总是评估为真
  • 我在 Python 中多次看到这个错误,但对于 C++ 来说这是第一次。
  • @MarkRansom 信不信由你,这也是 c++ 中的常见错误。
  • 唉,在可能的 数百个 重复项中找到一个是近乎徒劳的效率练习,因为问题标题和文本通常如此模糊,几乎没有共同点锁定到。我想,对语言入门的引用是一个不错的后备方案。

标签: c++ visual-studio


【解决方案1】:

if(firstDegrees == 'K' || 'k') 将始终评估为 true,因为 k 原样Not Null,表示 有效,表示 是的

您需要以与此类似的方式编写所有表达式:(firstDegrees == 'K' || firstDegrees == 'k')

此外,您可能希望在每个 if 之后添加 elses,以获得更好、更清晰的逻辑控制。

【讨论】:

  • 'k' 不能是 null,因为它不是指针。为了更好更清晰的逻辑控制swith 恕我直言。
  • if(null) 将评估为假。这就是我对Not Null 的意思。为了澄清更多,我添加了其他术语,例如 ValidTrue
  • 没有nullif(null) 这样的东西是语法错误。并且if() 无法评估为false。 0 将评估为 false,'\0' 也将评估为 false,nullptr 将评估为 false。你应该更清楚Null的意思。
  • 是的,你是对的。 if(NULL) 不是语法错误,但 if(null)
猜你喜欢
  • 2021-07-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2019-12-28
  • 1970-01-01
  • 1970-01-01
  • 2013-09-28
相关资源
最近更新 更多