Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

string - Why is my if block not getting executed in my C program?

Why is the if block not executed in the code below?

#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf("----------------------------------< Welcome >----------------------------------
");
    printf("-------------------------------------- to -------------------------------------
");
    printf("-------------------- Temperature Convertor and Vice-Versa ---------------------
");
    printf("-------------------( Press enter button at prompt to exit )--------------------
");
    int i = 0;
    
    const char inp[4];
    printf("Do you want to Convert From Fahrenheit to Celsius:
");
    scanf("%s",inp);
    printf("%s",inp);
    if(inp == "Yes" || inp == "yes" || inp == "y")
    {
        const char *temp1;
        printf("Enter the Temperature in Fahrenheit:
");
        scanf("%s",temp1);
    }
    return 0;
}

Output of the Code:

----------------------------------< Welcome >----------------------------------
-------------------------------------- to -------------------------------------
-------------------- Temperature Convertor and Vice-Versa ---------------------
-------------------( Press enter button at prompt to exit )--------------------
Do you want to Convert From Fahrenheit to Celsius:

yes
yes
--------------------------------
Process exited after 19.01 seconds with return value 0
Press any key to continue . . .

I want to recreate this code in C:-

print("""----------------------------------< Welcome >----------------------------------
-------------------------------------- to -------------------------------------
----------------Celsius to Fahrenheit Convertor and Vice-Versa-----------------
""")
print("--------------------(Press enter button at prompt to exit)---------------------
")
i = 0
while i < 1:
    inp = input("Do you want to Convert From Fahrenheit to Celsius:
")
    if inp == "Yes" or inp == "yes" or inp == "y":
        fah = input("Enter the Temperature in Fahrenheit:
")
        if fah == "":
            break
        else:
            fah = float(fah)
            cel = float((fah-32)*(5/9))
            print("{}°F = {}°C".format(fah,cel))
            in2 = input("Do you want to continue?(Yes/No)
")
            if in2 != "yes" or in2 != "Yes":
                break
    elif inp == "":
        break
    else:
        cel = input("Enter the Temperature in Celsius:
")
        if cel == "":
            break
        else:
            cel = float(cel)
            fah = float((cel*(9/5)+32))
            print("{}°C = {}°F".format(cel,fah))
            in2 = input("Do you want to continue?(Yes/No)
")
            if in2 != "yes" or in2 != "Yes":
                break
    print()
print("----------------------------< Thanks for using >-------------------------------
")```

It is in Python
question from:https://stackoverflow.com/questions/65845379/why-is-my-if-block-not-getting-executed-in-my-c-program

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Other than the strcmp problem which has been addressed in an other answer there are three problems in this piece of code:

const char* temp1;
printf("Enter the Temperature in Fahrenheit:
");
scanf("%s",temp1);
  • you don't want const because the value you want to get from the user is not ... constant
  • temp1 is an uninitialized pointer that points nowhere
  • you don't need a string here but you want the user enter a number

So you want actually something like this:

float tempFahrenheit;
printf("Enter the Temperature in Fahrenheit:
");
scanf("%f", &tempFahrenheit);

float tempCelsius = ...   // I let you write the rest of the code yourself

If you want get a string from the user and convert that to a number you might want something like this:

float tempFahrenheit;
char inputstring[20];   // max length of string is 20
printf("Enter the Temperature in Fahrenheit:
");
scanf("%s", inputstring);
sscanf(temp, "%f", &tempFahrenheit); 
float tempCelsius = ...   // I let you write the rest of the code yourself

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...