Domanda C If / Else Statements are not functioning.

OliWan

Utente Bronze
15 Luglio 2022
20
15
0
23
I'm stuck on this issue while working on a fundamental computer science assignment for class here. I have prior expertise with Java but am just getting started with C. Could someone please help me understand why my if / else statement appears to be skipped over?
Codice:
 #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 do;
        scanf ("% f", & do);

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

        // Determines approptiate description (if / else system)
        char desc [50] = "if statement is not running :(";
        if (do == -40) {
            char desc [50] = "Ouch! Cold either way !!";
        }
        else if (do == 32) {
            char desc [50] = "Freezing point of water";
        }
        else if (do == 70) {
            char desc [50] = "Room temperature";
        }
        else if (do == 99) {
            char desc [50] = "Average body temperature";
        }
        else if (do == 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 ", do, celc, desc);
        return 0;
} [/ CODE]
 
The program it's entering the if... but it won't change the string content. There are many errors:
  • do is a reserved keyword (do...while loop) and even if it compiles you shouldn't name a variable or a function as a keyword.
  • I don't know if it was formatted wrongly by a paste but in the format strings there are spaces after the % sign, better remove them.
  • You're redeclaring desc many times in a inner scope
  • You have to study better how strings are handled in C, in high level languages a simple assignment is enough, in C it may be not.

After the celsius conversion correct the code with one of the following variants:
C:
const char* desc = "if statement is not running :(";
if (renamed_do == -40) {
    desc = "Ouch! Cold either way !!";
}
// ...

Otherwise:
C:
char desc [50] = "if statement is not running :(";
if (renamed_do == -40) {
    strcpy(desc, "Ouch! Cold either way !!");
}
// ...

In the first example you have all the strings as constants and desc is just a pointer to a constant string, you can move the pointer using the = operator and change which string is referenced.

In the second desc is an array that can hold 50 chars, the first assignment is optimized by the compiler which does the strcpy for you under the hood, strcpy copies each char until NUL is found, into the memory location provided as the first argument.