-2

I'm practicing with a simple program for area calculating, but for some reason the program just skips the first 'if' condition and goes directly to the 'else' structure. Why though?

char t;
float a, b, h, area;

printf("Enter the type of your rectangle:\n R for right angled \n N for not right angled");
t = getchar();
if (t == 'R')
{
    printf("Enter the legs of your triangle");
    scanf("%f", &a);
    scanf("%f", &b);
    printf("The area of your triangle is:%f\n", area=(a*b)/2);
}
else 
{
    printf("Enter the height and the side of your triangle:");
    scanf("%f", &h);
    scanf("%f", &a);
    printf("The area of your triangle is: %f\n", area=a*h/2);
}       
paddy
  • 60,864
  • 6
  • 61
  • 103
Renegade
  • 1
  • 2
  • 2
    Edit the question to provide a [mre]. – Eric Postpischil Dec 29 '21 at 09:10
  • It did not skip the if condition. It must have had a remaining character in the input buffer. Like for example a newline character. You can check by replacing the `t=='R'` by a function call with `t` as a parameter, and have the function log some diagnostics. – Cheatah Dec 29 '21 at 09:14
  • @EricPostpischil Putting this into a main function and adding the `#include stdio.h` is not really alot (3 or 4 lines). This is easily reproducible for anyone that would have the expertise to answer the question in the first place i think. – Jakob Sachs Dec 29 '21 at 09:20
  • 2
    @JakobSachs There is no doubt in my mind that Eric Postpischil has the expertise to answer this. But the point is that if OP spends a tiny bit of time making a [mre], this would be more time efficient than expecting everyone else to do it, without having any knowledge about prior code, that might for example contain a `scanf` that leaves input on the buffer. I'd rather not have this depend on assumptions. – Cheatah Dec 29 '21 at 09:27
  • @JakobSachs, a lot of people (me included) don't enjoy answering basic homework questions when the questioner hasn't put in the basic work to make it minimal. It shows that the questioner hasn't attempted to begin the process of debugging for themself. – Elliott Dec 29 '21 at 09:27
  • 3
    @JakobSachs: Putting this into a `main` function is not the only part of a [mre]. It includes input that reproduces the problem. Also, if the actual program has some prior code OP has not shown, preparing a [mre] reveals issues such as prior code that has input, leaving a new-line in the buffer, so the later `getchar` shown in the question reads the new-line instead of the “R”. Regardless, Stack Overflow has well established guidance for debugging questions, and it includes a [mre]. – Eric Postpischil Dec 29 '21 at 09:28
  • @Elliott Finding the bug in this doesn't seem like a typical homework question to me really (partially cause debugging is rarely a part of uni/school classes sadly). – Jakob Sachs Dec 29 '21 at 09:30
  • @EricPostpischil Fair enough i guess. Just seems a bit over the top when you can find/replicate the issue in the same way as i described it. Edit: Nevermind, i am a fool. – Jakob Sachs Dec 29 '21 at 09:33
  • @Renegade Can you please tell us how are you executing the program and how are you providing the input to your program? Are you pressing ENTER immediately after executing your program? – Gaurav Pathak Dec 29 '21 at 09:47
  • @GauravPathak I think if the user enter 4 characters, the program immediately move on to the next phase – justANewb stands with Ukraine Dec 29 '21 at 09:49
  • 1
    It wouldn't surprise me if there's confusion about the fact that a capital R must be input. If the user inputs a lower-case letter, this will not "work" – paddy Dec 29 '21 at 09:51
  • As a pedant, can I point out that `getchar()` returns an `int` not a `char` - and while this common idiom usually works, there is an implicit type conversion going on... – Andrew Jan 06 '22 at 09:30

1 Answers1

0

The reason why it skipped was on this code

printf("Enter the type of your rectangle:\n R for right angled \n N for not right angled");
  1. You just tap enter when it ask for input, because it will read as zero value on your input. Which was mean not "R"
  2. You type "r" or other value than "R", it was case sensitive, so be careful on it, if you want it case insensitive, just try put some method to make it insensitive. Or if it was in your case, which was only has one char on it, you can simply change
if (t == 'R'){} // to
if (t == 'R' || t == 'r'){} // this