#include <iostream>
using namespace std;
void funcOne()
{
cout << "one";
}
void funcTwo()
{
cout << "two";
}
int main()
{
int vienadojums;
cout << "Type [K] or [L]: ";
cin >> output;
if (output == 'K' || 'k')
{
funcOne();
}
else if (output == 'L' || 'l')
{
funcTwo();
};
return 0;
}
It doesn't matter what input I enter, it will always output funcOne()
. With this code, I want to make, so user can choose 2 different math problem formulas by inputting [K] or [L]. But for some reason no matter what you enter it will show funcOne()
CodePudding user response:
if (output == 'K' || 'k')
This is true if:
output
equals'K'
or
'k'
(is not equal zero)
The second condition always evaluates to true.
What you want is:
if ( ( output == 'K' ) || ( output == 'k' ) )
Same for the second condition.
I assume an edit artifact here:
int vienadojums;
cout << "Type [K] or [L]: ";
cin >> output;
You never defined output
, and you never used vienadojums
. If those were meant to be the same variable, you would be expecting an int
from input, not a char
(as you assume later on in your code). This error is covered up by your first condition always evaluating to true
regardless of what the user enters, but you will have to fix this as well.
The meta to take away here: Always put paranthesis around subexpressions, and enable compiler warnings at the strictest level possible; only by telling the compiler explicitly what you want do you give the compiler the opportunity to tell you where you might have erred. The compiler could, and should, have warned you of all of the above.
CodePudding user response:
In C , a character literal (for example, 'k'
) will contextually convert to a boolean expression, where true is any non-null value (i.e. the value is not the null character '\0'
†), and false is the null value. This happens in any context where a boolean expression is expected by the compiler like, naturally, the clause of an if-statement.
So when you write if (output == 'K' || 'k')
, the character literal on the right is contextually converted to a boolean value, and this expression is logically equivalent to if (output == 'K' || true)
, which will always evaluate to true
.
The correct way to write this is if (output == 'K' || output == 'k')
.
† This is the null character, '\0'
, and NOT the character literal '0'
CodePudding user response:
In your if statement the condition is always true because:
[x] or true => true
[x] => (output == 'K')
'k'
is a truthy value
no matter the result of output == 'K'
CodePudding user response:
Declare the variable vienadojums
as having the type char
char vienadojums;
and use it instead of the variable output
because in the if statement you are comparing it with characters.
Or instead of the declaration of the variable vienadojums
char vienadojums;
decalre
char output;
And change the conditions in the if statements like
if (output == 'K' || output == 'k')
{
funcOne();
}
else if (output == 'L' || output == 'l')
{
funcTwo();
};
Otherwise for example this if statement
if (output == 'K' || 'k')
always evaluates to true independent on the value stored in the variable output
because the character literal 'k'
is not equal to 0
.
That is the above if statement is equivalent to
if ( ( output == 'K' ) || ( 'k' != 0 ) )
Pay attention to that in general when you have many functions from which you need to select one function to call the approach with if statements is not flexible.
Instead you could store all functions and corresponding identifiers of the functions in a container for example in an array. And then select a required function from the container according to the user input.
For example
#include <iostream>
#include <utility>
#include <iterator>
#include <algorithm>
#include <cctype>
void funcOne()
{
std::cout << "one\n";
}
void funcTwo()
{
std::cout << "two\n";
}
int main()
{
std::pair<char, void( * )( void )> expressions[] =
{
{ 'l', funcOne }, { 'k', funcTwo }
};
std::cout << "Type any of ";
bool following = false;
for (auto &[selection, func] : expressions)
{
if (following)
{
std::cout << ", ";
}
std::cout << '\'' << selection << '\'';
following = true;
}
std::cout << " : ";
char selection = '\0';
std::cin >> selection;
auto it = std::find_if( std::begin( expressions ), std::end( expressions ),
[&]( const auto &item )
{
return item.first == ::tolower( ( unsigned char )selection );
} );
if (it != std::end( expressions ))
{
it->second();
}
else
{
std::cout << "Invalid input.\n";
}
}
The program output might look like for example
Type any of 'l', 'k' : L
one