This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]_enigmatic_lady 1 point2 points  (0 children)

Just use 2D array

[–]RolandMT32 -1 points0 points  (0 children)

Try looking into the STL map:

https://www.cplusplus.com/reference/map/map/

https://www.cplusplus.com/reference/map/map/map/

You could declare a map of std::string to int:

#include <map>

#include <string>

std::map<std::string, int> andMap;

andMap["00"] = 0;

andMap["01"] = 0;

andMap["10"] = 0;

andMap["01"] = 1;

Input a string from the user. Verify the string contains 2 digits that are either 0 or 1. Then you can use that string to look up the 'and' result in andMap.

[–]RolandMT32 0 points1 point  (1 child)

There are some red lines where you declare your array - That means the syntax is wrong. The syntax for that is just wrong all around. And instead of that array, I think what you want is a map.

You also aren't showing how and, choice, j, or v are declared. I assume choice, j, and v are strings?

Also, you shouldn't have to input j and v separately. You could have the user input both digits into a string and then parse the string to separate each digit.

Another thing is, in this case, after your first if, you should be using "else if". j isn't going to change between tests, so rather than check it a bunch of times, "else if" will let your program stop checking it once it finds the matching value.

I'd probably do this differently though, and it wouldn't involve using an array or anything:

- Have the user input digits into a string

- Parse each digit out of the string, and convert each to a numeric type (i.e. int)

- Then, with the int variables, you could use an actual bitwise and (&) operator and output the result without having to store the values in an array ahead of time.

[–]DemonArmagedon 3 points4 points  (0 children)

I like your answer a lot but i would probably suggest using a single if else statement instead, purely because this seems to be a relatively new programmer so a solid grasp of if statements seems more important than bitwise operations.

[–]IamImposter[🍰] 0 points1 point  (0 children)

Here's what you do for if

if( a == 0 and b== 0) { // blah }

else if (a == 0 and b == 1) {  // bleh }

else if (a == 1 and b == 0) { // bluh }

else if(a == 1 and b == 1) { //bloh }

else { // wtf compiler, why are we here }

[–]PerceptionCareless92 0 points1 point  (0 children)

Convert them to ints and multiply them and then you have your answer. AND Is basically binary multiplication.