找到你要的答案

Q:If statement running even when not true

Q:如果语句运行时不正确

I am not sure what I'm doing wrong here, every time I run it, it goes through the if part even when it's not true? So the 'else' never runs.

#include <iostream>
using namespace std;

string choice;
float numb;

float convert(float numb,string choice)
{
    float newNo;
    if (choice == "F" or "f"){
        newNo = numb * 0.2 * 9 + 32;
    }else{
        newNo = (numb - 32) / 1.8;
    }
    return newNo;
}

int main()
{
    cout << "welcome to Temperature converter v0.1" << endl;
    cout << endl;
    cout << "Which conversion would you like to use?" << endl;
    cout << "type C to convert to Celsius and F to convert to Fahrenheit - ";
    cin >> choice;
    cout << "what number would you like to convert? - ";
    cin >> numb;

    cout << convert(numb,choice);


    return 0;
}

我不知道我在这里做错了什么,每次我运行它,它通过中频部分,即使它不是真的吗?所以'其他'从来没有运行。

#include <iostream>
using namespace std;

string choice;
float numb;

float convert(float numb,string choice)
{
    float newNo;
    if (choice == "F" or "f"){
        newNo = numb * 0.2 * 9 + 32;
    }else{
        newNo = (numb - 32) / 1.8;
    }
    return newNo;
}

int main()
{
    cout << "welcome to Temperature converter v0.1" << endl;
    cout << endl;
    cout << "Which conversion would you like to use?" << endl;
    cout << "type C to convert to Celsius and F to convert to Fahrenheit - ";
    cin >> choice;
    cout << "what number would you like to convert? - ";
    cin >> numb;

    cout << convert(numb,choice);


    return 0;
}
answer1: 回答1:

The problem is your if Statement:

if (choice == "F" or "f"){

basically, what you say here is: If choise is "F" or if "f". You need to understand: True is everything besides zero (0). "f" is NOT zero, so it is true. You could also wirte (or = ||):

if (coice == "F" || true)

which is the same like:

if (true)

So in order for your code to work, you need:

if (choice == "f" || choice == "F")

That would do what you expect.

问题是你的if语句:

if (choice == "F" or "f"){

basically, what you say here is: If choise is "F" or if "f". You need to understand: True is everything besides zero (0). "f" is NOT zero, so it is true. You could also wirte (or = ||):

if (coice == "F" || true)

这是一样的:

if (true)

所以为了你的代码工作,你需要:

if (choice == "f" || choice == "F")

那将是你所期望的。

answer2: 回答2:

There is a problem in the syntax of 'if' statement. Following is the corrected code, where '||' stands for 'or':

if ((choice == "F") || (choice=="f")){
    newNo = numb * 0.2 * 9 + 32;
}else{
    newNo = (numb - 32) / 1.8;
}

在“如果”语句的语法中有一个问题。以下是修正后的代码,其中的| | '代表'或':

if ((choice == "F") || (choice=="f")){
    newNo = numb * 0.2 * 9 + 32;
}else{
    newNo = (numb - 32) / 1.8;
}
answer3: 回答3:

Change this if statement

if (choice == "F" or "f"){

to the following

if (choice == "F" or choice == "f"){

Otherwise the right operand of the operator or

"f"

is always equal to true because it is converted to the address of the first character of the string literal that evidently is not equal to 0.

That is your original condition in the if statement looks like

if (choice == "F" or "f" != nullptr){

and indeed "f" != nullptr

更改if语句

if (choice == "F" or "f"){

以下

if (choice == "F" or choice == "f"){

否则运算符的右操作数或

"f"

总是与true相等,因为它被转换为字符串字符的第一个字符的地址,这显然不等于0。

这是你的原始条件在if语句看起来像

if (choice == "F" or "f" != nullptr){

确实是“F”!= nullptr

c++