-2

I have to details about a property's lenght, width, whether its approved by government or not then its cost and estimated cost after adding 10% tax. The output doesn't give me the option to select a property type.

 #include <stdio.h>
    #include <stdbool.h>
    int main()
    { 
        char l, f, pte;
        float length ,breadth,property_cost;
        float cost_per_squarefeet;
        bool approved_by_govt_OR_NOT;
        printf("enter the length\n");
        scanf("%f" ,&length);
        printf("enter the breadth\n");
        scanf("%f" ,&breadth);
        printf("enter the cost per squarefeet\n");
        scanf("%f" ,&cost_per_squarefeet);
        printf("enter property type\n");
        if(pte=='1')
    {
            printf("property type is land\n");
        }
        else if(pte=='f')
        {
            printf("property type is flat\n");
        }
        else
        {
            printf("property type is house\n");
        }
        property_cost =(length*breadth*cost_per_squarefeet) * (10/100.0);
        printf("property cost is %f" ,property_cost);
        return(0);
    }

The output

cigien
  • 57,834
  • 11
  • 73
  • 112
tdave
  • 1
  • This program exhibits undefined behavior, by way of accessing an uninitialized variable `pte`. Where and how was that variable meant to get its value? Under what circumstances do you expect it to have the value `'1'`, or `'f'`? – Igor Tandetnik Sep 13 '20 at 17:02
  • You have missed a line out: after `printf("enter property type\n");` Don't forget to place a space before `%c` when you add it. – Weather Vane Sep 13 '20 at 17:07

2 Answers2

0

You forgot to scanf() the users input. The value of pte is never set, therefore it neither equals "l" or "f", and the else statement will always be executed.

Krekel
  • 11
  • 4
  • i get it but how do i set a value for pte? – tdave Sep 13 '20 at 18:04
  • You want to ask the user for input. The input is a char, so specify `"%c"`, and store it at the address of pte using `&pte`. Put the following line just above your if statement. `scanf("%c" ,&pte);` – Krekel Sep 13 '20 at 19:50
-1

pte is uninitialized since it is a local variable, so it will be most likely a house, given you are reading whatever the memory happens to have there at the time.

You will need to use scanf() or similar to get the user's input.

Acorn
  • 24,970
  • 5
  • 40
  • 69
  • 1
    Incorrect. `pte` is uninitialized, accessing it exhibits undefined behavior. – Igor Tandetnik Sep 13 '20 at 17:01
  • @IgorTandetnik Yep, mixed it up with globals. – Acorn Sep 13 '20 at 17:02
  • @IgorTandetnik i used scanf after printf(property type) line but it still automatically chooses the type as a house. its supposed to give Land when the user types "l" and flat when typed "f" – tdave Sep 13 '20 at 17:35
  • You compare with `1` (digit one), not with `l` (lowercase letter Ell). Anyway, show your `scanf` call, it might be incorrect. – Igor Tandetnik Sep 13 '20 at 17:36
  • @tdave The `scanf()` lines you have are not for `pte`. – Acorn Sep 13 '20 at 18:03
  • @IgorTandetnik scanf("%f" ,&property_type); – tdave Sep 13 '20 at 18:06
  • There's no variable named `property_type` in your program as shown. – Igor Tandetnik Sep 13 '20 at 18:07
  • @IgorTandetnik i added the 'property_type' variable but its still showing the same thing – tdave Sep 13 '20 at 18:11
  • What type is that variable? Are you still checking `pte`? Edit your question, update it with the code you are running now; don't describe it in prose, don't play 20 Questions. – Igor Tandetnik Sep 13 '20 at 18:12
  • @Acorn "pte is uninitialized" correct. "so it will be most likely a house" incorrect. "you are reading whatever the memory happens to have there at the time" incorrect again. It's a rather common misunderstanding that reading an uninitialized variables results in reading some garbage value. This in not true. It is Undefined Behavior and it can manifest as apparently reading a value, a crash, or any kind of weird things like apparently a variable having multiple values, or no value at all. – bolov Sep 14 '20 at 22:12
  • See my answer for real example of weird things https://stackoverflow.com/a/60342706/2805305 and read this posts about UB: https://blog.llvm.org/posts/2011-05-13-what-every-c-programmer-should-know/ – bolov Sep 14 '20 at 22:13
  • @bolov I know what UB is. While it is useful to think about the semantics given by the C standard, it is not useful disregarding what happens in practice and explaining why OP sees what OP sees. Without optimizations, most compilers will output code that reads from the stack and therefore it is *very* likely that it is a house (eg 254/256 chance assuming uniformity which is a big ask but shows the point). *With* optimizations, most compilers will end up assuming `pte` is unconditionally zero (a house) since there is no way to infer any particular value. – Acorn Sep 15 '20 at 08:54