lundi 29 octobre 2018

Char equals char undesired output [duplicate]

This question already has an answer here:

I am trying to create a simple c++ program which prints out if char is Y or y, N or n or neither.

After debugging I have found out that the if(chr == 'Y' || 'y') statement is true even though char variable is 'N'. Can anybody tell me why this if statement is true and not false?

#include "pch.h"
#include <iostream>
using namespace std;

void main()
{
char chr = 'N';

if (chr == 'Y' || 'y')
{
    cout << "chr is y" << endl;

}
else if (chr == 'N' || 'n')
{
    cout << "chr is n" << endl;
}
else
{
    cout << "chr is something else" << endl;
}
}

Aucun commentaire:

Enregistrer un commentaire