0

I have been struggling to get my if statement in my function to evaluate correctly. I am attempting to get the if statement to evaluate as true only if the variable is equal to 'Y' or 'y'. I am new to messing with char variables, so my suspicion is I am either storing chars into the variable incorrectly, or evaluating the expression in a way that is always true.

The code I wrote is the following:

#include <stdio.h>

// fuctions
int Greeting(void);

// variables
int return_status;

int main(void)
{
    return_status = Greeting();
    printf("Return status is %d \n", return_status);
    return 0;
}

int Greeting(void)
{
    char status;
    printf("Welcome to the program. Would you like to continue?(Y/N)\n");
    scanf(" %c", &status);

    if (status == 'Y' || 'y') // Problem is probably here
    {
        printf("You have said %c.\n", status);
        return 0;
    }
    else
    {
        return 1;
    }
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
JArch
  • 27
  • 3
  • Your condition combining does not work how you expect it to. You need `if(status == 'Y' || status == 'y')` - the second condition does only evaluate if `'y'` is `true` which is always the case – Odysseus Jan 31 '22 at 10:34
  • 3
    There **must** be a suitable canonical duplicate for this ... looking ... – Adrian Mole Jan 31 '22 at 10:36
  • First of all indent your code properly, for example like the samples in your C text book. – Jabberwocky Jan 31 '22 at 10:40
  • Properly affixed with sufficient warning levels and treated as errors, your compiler should tell you what is wrong [see here](https://godbolt.org/z/oYhbfEe13). – WhozCraig Jan 31 '22 at 10:44
  • 1
    @AdrianMole I concur. the problem is this mistake is usually only made by extremely junior engineers/students very new to the C language. And their engineering youth spills over into their quality of question, and choice of vernacular. To be honest, the title of this question is probably one of the better ones I've seen when encountering this issue. They're usually of the ilk of "my code don't work", "what is wrong", etc., which are basically useless. At least this question title has a hint of problem specificity. – WhozCraig Jan 31 '22 at 10:47
  • @WhozCraig Well, I looked a lot but didn't find an obvious dupe. (There's one for `a < x < b`, but that's a bit different.) Maybe this could become the canonical? – Adrian Mole Jan 31 '22 at 10:50
  • @AdrianMole I looked for a while but could not find anything before posting. However, this terminology is pretty new to me and I'm self studying, so that doesn't mean much. Thank you for your time. – JArch Jan 31 '22 at 11:04

3 Answers3

3

The first comparison you wrote here is correct, but the expression you wrote to the right of the statement is not a comparison. You directly write the expression 'y' there, since it does not correspond to 0 in the ASCII table, it is considered true and always gives the true result when combined with the OR expression.

if(status == 'Y' || 'y')

You should change like this;

if((status == 'Y') || (status== 'y'))
Ogün Birinci
  • 598
  • 3
  • 11
0
if((status == 'Y') || (status == 'y')) //Problem is probably here
{
  printf("You have said %c.\n", status);
  return 0;
}

You can check char like that

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
0

Yes, the problem is the "if" statement. You have to write:

if (status == 'Y' || status == 'y')

Variable return_status should be declared into main() function.

Wolf
  • 1
  • 1