You are misunderstanding how assert works. It doesn't display a message, it simply aborts program execution when the tested condition fails.
https://en.cppreference.com/w/cpp/header/cassert
Plus the assert macro relies on another macro NDEBUG to work. A non-standard macro.
With Visual Studio assert can be triggered when the code is compiled in debug mode. Release mode and assert is ignored.
Now, you can have the macro display a message, you have to write it. It is non-standard to do so.
https://en.cppreference.com/w/cpp/error/assert
Plus returning an int value when a Boolean is expected is potentially unsafe.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <iostream>
#include <cassert>
#include <string> // use a C++ string, need this
bool isValidNote(std::string note)
{
if (note.length() != 2)
{
return false;
}
else if ((note[0] >= 'A' && note[0] <= 'G') && (note[1] >= '0' && note[1] <= '9'))
{
return true;
}
return false;
}
int main()
{
assert(isValidNote("A7") == true);
assert(isValidNote("B") == false);
}
|
FYI, both assertions pass.
A7 because two characters consisting a letter and a number is true. true == true.
B because it is a single letter. false == false.
No output.
Change either test condition, A7 == false or B == true, and their respective assert should trigger. It happens with VS in debug mode, as expected.
If both are false, then the first one will trigger. The 2nd is never reached.