-7

Working on a basic computer science project for class and am stuck on this issue. I am experienced in Java but am just beginning in C language. It seems to simply skip over my if/else statement as if it were not there, could someone help me understand this?

#include <stdio.h>
int main()
{
        //Systems Programming: Project3 - Justus Milhon
        //Requests User Input
        printf("Enter temparature in Farenheit (int up to 3 digits): ");

        //Names and scans in Farenheit value
        float fare;
        scanf("%f", &fare);

        //Declares and calculates Celcius value
        float celc;
        celc = 5.0 / 9.0 * ( fare - 32 );

        //Determines approptiate description (if/else system)
        char desc[50] =  "if statement is not running :( "; 
        if(fare == -40){
            char desc[50] = "Ouch! Cold either way!!";
        }
        else if(fare == 32){
            char desc[50] = "Freezing point of water";
        }
        else if(fare == 70){
            char desc[50] = "Room temperature";
        }
        else if(fare == 99){
            char desc[50] = "Average body temperature";
        }
        else if(fare == 212){
            char desc[50] = "Boiling point of water";
        }
        else{
            char desc[50] = "final else stetement is being used :(";
        }

        //Prints output
        printf("Farenheit          Celsius          Description\n----------        
        ----------         ----------\n%.0f                 %.3f        
        %s\n", fare, celc, desc);
        return 0;
}
  • 3
    That's *not* how you compare floats. – babon Sep 29 '17 at 08:11
  • 2
    Hint: when you are beginner in a language: the language is never broken. The broken thing is most most most likely the implicit assumptions that you put into your code. In your case: assuming that you can deal with floating point numbers the same way as with int numbers. – GhostCat Sep 29 '17 at 08:12
  • 1
    `{ char desc[50] = "Ouch! Cold either way!!"; }` : This `desc` is local scope variable in this block. – BLUEPIXY Sep 29 '17 at 08:13
  • Maybe reading that question you got a link to? – GhostCat Sep 29 '17 at 08:13
  • man float. You're doing C, go check the man of everything. – Quentin Laillé Sep 29 '17 at 08:13
  • I understand that the code is not broken, that's not what I'm saying. I'm asking what I'm doing wrong. thanks. – Justus Milhon Sep 29 '17 at 08:14
  • 1
    You have an answer already [here](https://stackoverflow.com/questions/1839422/strange-output-in-comparison-of-float-with-float-literal) – Nerdy Sep 29 '17 at 08:15
  • 1
    Just a tip: Don't title *"[some basic language feature..] not working"*. It **is** working, it's your code that isn't. With a title like this, you will only attract downvotes. –  Sep 29 '17 at 08:18
  • @Sathiya Main reason for seeming non-execution is local char pointers, not the float issue. – Yunnosch Sep 29 '17 at 08:18
  • You are getting "if statement is not running :(", don't you? Everybody talking about float guesses that you get "final else stetement is being used :(". – Yunnosch Sep 29 '17 at 08:20
  • @Yunnosch **both** issues are present here, so we'll need a dupe for both to correctly close this... –  Sep 29 '17 at 08:20
  • @FelixPalmen True. How do you treat a question which is answered by reading **two** others? Closing as duplicate gives OP only half of the answer... – Yunnosch Sep 29 '17 at 08:22
  • @Yunnosch close it with TWO dupes. Still looking for a good one about the scope issue .... –  Sep 29 '17 at 08:23
  • @Yunnosch yes, im getting the issue "if statement if not running :(" – Justus Milhon Sep 29 '17 at 08:28
  • @JustusMilhon please don't add complaints to your question, rolled back. I reopened it because the dupe indeed didn't address your **first** problem and I can't find one about that. See unwind's answer which explains the issue. –  Sep 29 '17 at 08:42

2 Answers2

3

This:

if(fare == -40){
  char desc[50] = "Ouch! Cold either way!!";
}

opens up a new scope, with a new local variable called desc, that "shadows" the one in the surrounding scope. This variable is initialized to the string, then thrown away as the scope exits. The variable of the same name in the parent scope is never touched.

You need:

#include <string.h>

up top, and then change all the ifs to look like this:

if(fare == -40){
  strcpy(desc, "Ouch! Cold either way!!");
}

This uses the standard strcpy() function to write the new string into the single desc variable in the parent scope.

Also you never look at celc, which seems confusing after computing it, and exact comparisons of floats is a bad idea.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Can you also cover the (secondary in my opinion) issue of floats and `==`? I.e. change `==`-> `<=`. – Yunnosch Sep 29 '17 at 08:23
  • Celc is part of the final output. – Yunnosch Sep 29 '17 at 08:26
  • I changed them to match your suggestion and now im getting the error "incompatible implicit declaration of built-in function ‘strcpy’" :( – Justus Milhon Sep 29 '17 at 08:28
  • @Yunnosch part of the issue with the == vs <= is that the code is only supposed to display a message if it is exactly equal to that temperature (numeric value) – Justus Milhon Sep 29 '17 at 08:31
  • @JustusMilhon you have to `#include ` for that function. –  Sep 29 '17 at 08:39
  • @JustusMilhon yw, but to make this program correct, either compare your `float`s to a sensible *range* or change the type of `fare` to `int` (that's the second problem in your code, the one that dupe was about). –  Sep 29 '17 at 08:47
  • @JustusMilhon Ok. It falsely seemed obvious to me that it would make sense for ranges of temperatures. I did not notice my wrong assumption. It is of course your question and your rules. Sorry for assuming a mistake on your part. – Yunnosch Sep 29 '17 at 17:56
0

Your code just defines different arrays with the same name and a scope restricted to the block in which they are defined. You should use a pointer and assign it to the proper string instead:

#include <stdio.h>

int main(void) {
    //Systems Programming: Project3 - Justus Milhon
    //Requests User Input
    printf("Enter temperature in Farenheit (int up to 3 digits): ");

    //Names and scans in Farenheit value
    float fare;
    scanf("%f", &fare);

    //Declares and calculates Celcius value
    float celc;
    celc = 5.0 * (fare - 32) / 9.0;

    //Determines appropriate description (if/else system)
    const char *char =  "if statement is not running :( "; 
    if (fare == -40) {
        desc = "Ouch! Cold either way!!";
    } else
    if (fare == 32) {
        desc = "Freezing point of water";
    } else
    if (fare == 70) {
        desc = "Room temperature";
    } else
    if (fare == 99) {
        desc = "Average body temperature";
    } else
    if (fare == 212) {
        desc = "Boiling point of water";
    } else {
        desc = "final else statement is being used :(";
    }

    //Prints output
    printf("Farenheit          Celsius          Description\n"
           "---------          -------          -----------\n"
           "%9.0f         %7.3f         %s\n",
           fare, celc, desc);
    return 0;
}

Note however that the floating point operations are not performed with absolute precision: 5.0 / 9.0 is an approximate value, so the value computed into celc may be just a tiny bit different from the integral result. Changing the order of operands as I did may improve the accuracy of the result for some computations.

chqrlie
  • 131,814
  • 10
  • 121
  • 189