Why doesnt this work?

Everything works however the when I put a capital letter it gets an error in the first 3 letters aswell but i dont understand why because the statement that messes it up is about the last 3 digits ánd has nothing to do with the first 3.

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
  #include <iostream>
#include <exception>
using namespace std;
int main()
{
    string ID;
    int i;
    try
    {   
        cout << "enter your id (3 letters into 3 numbers: ";
        cin >> ID; 
            // if (ID.at(0&&1&&2) >= 'A' && ID.at(0&&1&&2) <= 'Z'); (not important but i tried to fix this way.
            if (isdigit (ID.at(0&&1&&2))) throw std::runtime_error ("error: letter expected");
            if (ID.at(3&&4&&5) >= 'A' && ID.at(3&&4&&5) <= 'Z' || ID.at(3&&4&&5) >= 'a' && ID.at(3&&4&&5) <= 'z') 
            throw std::runtime_error ("error: letter expected");
    
        cout << ID << endl;
    } 
    catch(std::runtime_error & e)
    {
        std::cerr << e.what() << endl;
    }
    return 0;
}


 
if (ID.at(3&&4&&5) >= 'A' && ID.at(3&&4&&5) <= 'Z'


thats the part that messes with the first 3 digits for some reason not allowing for a capital letter.
Last edited on
The expression 0&&1&&2 always evaluates to false. Similarly, the expression 3&&4&&5 always evaluates to true.

This isn't what you intended. To phrase it another way, do_something(a && b) does not mean do_something(a) && do_something(b).
thankyou mbozzi that made it work but i dont fully understand why the true and false.
in c++ zero is false, and everything else is not false.
so the first one is
false and true and true = false.
the second one is
true and true and true = true.

the collapsed values (false for the first, true for the second) are then passed forward.
Last edited on
mbozzi is correct as well as jonnin. I'll chime in my two cents as well:

You're assuming that AND statements can be used to collect elements.

Take a look at this code.
1
2
 string myString = "abc";
cout << myString.at(0&&1&&2));


You might assume that this is going to return "abc". But behind the scenes,
0&&1&&2 is actually a function which returns
true/false


This may seem unintuitive, but in C++,
true
and
1
are actually the same thing. Likewise,
false
is the same thing as
0


So what happens instead? 0&&1&&2 will be evaluated to false. You're code is going to get calculated down to the following statement.
1
2
 string myString = "abc";
cout << myString.at(0));


which means, only get the first letter in my string. So actually you're only going to print "A".

I hope that helps!
Last edited on
0&&1&&2 is actually a function which returns true/false
This (the built-in operator &&) is not a function (and neither is the expression 0&&1&&2). This is an important distinction because the built-in operator && evaluates its arguments in order such that short-circuiting evaluation (See: https://en.wikipedia.org/wiki/Short-circuit_evaluation ) is possible. Function arguments, however, are evaluated in unspecified order.

Also important is to note that && is left-associative, so 0&&1&&2 is equivalent to ((0 && 1) && 2).
Last edited on
Topic archived. No new replies allowed.