Command not found

When I try to run my code and I input "B" or "b4" it always just tells me "command not found" in the terminal. Its supposed to have a "0" or "1" output. Does anybody know what is wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  
#include <iostream>
#include <cassert>
using namespace std;

int isValidNote(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 0;
}

int main ()
{
    assert(isValidNote("A7") == true);
    assert(isValidNote("B") == false);
    
    return 0;
}
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.
Last edited on
Thank you! That makes a lot of sense. :D
static_assert might be a better fit, it shows at compile-time if the assertion fails. Debug or release mode.

Requires C++11 or later to work, no library needed.
https://en.cppreference.com/w/cpp/language/static_assert
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>  // use a C++ string, need this

bool constexpr 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()
{
   static_assert(isValidNote("A7") != true);
   static_assert(isValidNote("B") != false);
}

Both assertions fail. In VS the failures show up as errors without needing to compile.

Do note the function has to be declared as constexpr or static_assert will not work.

And declaring the function as constexpr bool isValidNote(std::string note) works as well.
Last edited on
Topic archived. No new replies allowed.