0

I'm writing a C program that asks the user for a variety of inputs, one is a Yes or No question. If you put Y, or y, the If statement is supposed to execute. However, no matter what you input, it goes through with the If statement.

double phonePrice;
double phoneTax;
double phoneTotal;
double phoneApplePrice;
double phoneSubtotal;
double temp;
int yearsAppleCare;
char userAnswer;

//prompting for price
printf("Enter the price of the phone> ");
scanf("%f", &phonePrice);
fflush(stdin);

//prompting for iphone
printf("Is the phone an iPhone (Y/N) ?> ");
scanf("%c", &userAnswer);
fflush(stdin);

//nested if statements asking for apple care amount
if(userAnswer=="Y"||"y")
{
    printf("Enter the number of years of AppleCare> ");
    scanf("%d", &yearsAppleCare);
    
    if(yearsAppleCare<=0)
    {
        printf("You must choose at least 1 year of AppleCare");
        return 0;
    }
}

Any help with this would be appreciated.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 5
    `userAnswer=="Y"||"y"` should be `userAnswer=='Y' || userAnswer=='y'`. – Petr Skocik Oct 03 '21 at 18:05
  • Use `if(userAnswer=='Y'||userAnswer=='y')`. – Roman Hocke Oct 03 '21 at 18:05
  • 2
    Don't use `fflush(stdin)`: https://stackoverflow.com/questions/2979209/using-fflushstdin – Nate Eldredge Oct 03 '21 at 18:07
  • This is quite possibly the first time I've seen this question (or its equivalent) asked for a language other than Python. Strange that it doesn't happen more often, actually. – Karl Knechtel Oct 03 '21 at 18:14
  • 1
    @KarlKnechtel I've seen and closed probably tens or a hundred C and C++ questions asking about this, but I've never seen such a python question. Probably it's because you're more active in python questions – phuclv Oct 04 '21 at 03:28
  • duplicates: [Program not recognizing character inputs in if-else statements](https://stackoverflow.com/q/29896240/995714), [Comparing user-inputted characters in C](https://stackoverflow.com/q/3911653/995714), [`if (x==0||7||14||21||-7||-14) {y=THURSDAY;}`](https://stackoverflow.com/q/14082446/995714), [If always returns true](https://stackoverflow.com/q/32035762/995714) – phuclv Oct 04 '21 at 03:38
  • @phuclv I suppose it must be. Thanks for having duplicates handy. – Karl Knechtel Oct 04 '21 at 21:48

2 Answers2

1

For starters this call

fflush(stdin);

has undefined behavior. Remove it.

Instead of this call

scanf("%c", &userAnswer);

use

scanf(" %c", &userAnswer);
      ^^^^

to skip white spaces in the input buffer as for example the new line character '\n'.

Also for double variables use the conversion specifier %lf. For example

scanf("%lf", &phonePrice);

The condition in the if statement

if(userAnswer=="Y"||"y")

is equivalent to

if( ( userAnswer=="Y" ) || ( "y" ) )

As the string literal "y" that is implicitly converted to a pointer to its first element is not equal to a null pointer then the condition always evaluates to logical true.

You need to write

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

using integer character constants 'Y' and 'y' instead of the string literals.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thank you so much! I'm taking a class on C, and my teacher told me to use fflush to get rid of the space input, but this is a much better way. The previous language I was learning was very simple, and or statements didn't require that much specification. Thanks again! – inkystinky33 Oct 04 '21 at 03:20
-2

Error is here:

//nested if statements asking for apple care amount
if(userAnswer=="Y"||"y")
{

Should be:

//nested if statements asking for apple care amount
if(userAnswer == "Y" || userAnswer == "y")
{

Wait! No!

Should be:

//nested if statements asking for apple care amount
if( strcmp(userAnswer, "Y") == 0 || strcmp(userAnswer, "y") == 0)
{

Why?

What does == mean?

In C language, == means the two objects are equal. In the case of strings, the objects are char*, I.e. pointers to char. These will be equal if and only if the memory address is the same. This will not be true in this case.

Why? Because one string is compiled into the program and initialised as the program starts, and the other is provided by the user into temporary memory. These will be at different addresses so the pointers will not be the same.

What you probably want is to compare the contents of the memory locations pointed to by the two pointers.

For that purpose the strcmp function is provided. This function returns zero if the strings are the same. You may also want to consider stricmp.

Ben
  • 34,935
  • 6
  • 74
  • 113