Silly question; maybe

Is it possible to have a variable type with several possible values. For example
1
2
int = 8 || 5;
string token_1; = '+' || '-';


Not silly, just very confusing.
 
int = 8 || 5;


An integer can hold the 8 or the 5, so I am not sure what you're trying to possibly do here. Perhaps an explanation and another example?
Try it.

1
2
int a = 8 || 5;
std::cout << "a = " << a;



It will print that a = 1.

It's using truth logic

http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap03/logical.html

the result of logical expression a .OR. b is .FALSE. if and only if both operands a and b are .FALSE.. In all other cases, the result is always .TRUE. In other words, if one of the two operands of the .OR. operator is .TRUE., the result is .TRUE.


Maybe somebody can correct me.


So no, you can't have a variable assigned 2 values. Maybe you can reassign them down the line but you can't really say an integer is 8 or 5, because then it'll just use truth tables to say that a is equal to 1 or 0. Try it with different numbers like int a = 8 || 5, int a = 8 && 5, and other numbers.
Last edited on
I'm trying to separate strings using delimiters. So it's just easier to have a delimiter for multiplication set to more than one value. For example string '*' || 'x' ||; This is much easier than making two strings.

I'm still fairly new and you might know more than me, I'm just going off of what I've learned so far..

Maybe you can do something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char a;
std::cout << "enter an operation: ";
std::cin >> a;

switch (a)
{
	case 'X':
	case 'x':
        case '*':
		std::cout << "you've chosen multiplication.\n";
		break;
	default:
		std::cout << "invalid operation!\n";
                break;
}


All this does is if you enter x or X itll do multiplication, the functions are up to you but this is just an example.

You can add more cases like case '+': or case '/' etc..
Last edited on
I know you marked this as solved, but did you see strtok()?
http://www.cplusplus.com/reference/cstring/strtok/
Last edited on
Topic archived. No new replies allowed.