well I was just playing around with what I've learned in C++ so far, and I wanted to make a very very basic text calculator. So I want to have a char and then I want to check its value: Add, Subtract, Multiply, etc.
So how can I check the value of a char...I thought it would be like this:
"Human beings didn't evolve brains in order to lie around on lakes. Killing's the first thing we learned. And a good thing we did, or we'd be dead, and the tigers would own the earth."
- Valentine to Ender ("Ender's Game")
well it worked for me in my text rpg I made awhile ago and it was in c++.
I will live forever or die trying.
"Human beings didn't evolve brains in order to lie around on lakes. Killing's the first thing we learned. And a good thing we did, or we'd be dead, and the tigers would own the earth."
- Valentine to Ender ("Ender's Game")
XianForce wrote:well I was just playing around with what I've learned in C++ so far, and I wanted to make a very very basic text calculator. So I want to have a char and then I want to check its value: Add, Subtract, Multiply, etc.
So how can I check the value of a char...I thought it would be like this:
And I think you get the idea. I keep getting an error that Add is an unknown variable. I changed a to a short for now and made 1=add, 2=subtract,etc.
So is there something I missed in my code? ^That's the Gist of it^ Or is there another way to do this?
Keep in mind that char's only hold one character at a time unless you do a char array, plus to check the value of a char it would have to be something like (a = 'A'), note the single quotes.
MarauderIIC wrote:You know those people that are like "CHECK IT OUT I just made Linux run on this piece of celery [or other random object]!!"? Yeah, that's Falco, but with ES.
Dear god, they actually ported ES to a piece of celery!
Martin Golding wrote:
"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
please use a switch! if else if else if else is such an ugly way to code.
i would have had something like
#define ADD atoi('+') // dont know if that would work, but i just meant the ascii value of a plus sign.
and do the rest so on.
dont use strings for something a char will do just fine. single key strokes should always just be a char. and addition, subtraction, multiplication and division all have their own special buttons on the keyboard. use them, they feel left out.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
cin >> mychar;
switch (mychar) {
case '+':
Add(rest of input)
break;
case '-':
Subtract(rest of input);
break;
};
?
And avansc, you can directly cast from int to char, so #define ADD = '+' //// or in C++, const char ADD = '+' //// or const int ADD = '+' //// would work just fine
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
If he's going through a C++book, I do believe that everyone that I've read goes to if statements before the almighty switch statement... so maybe that's why he's using IF statements?
MarauderIIC wrote:You know those people that are like "CHECK IT OUT I just made Linux run on this piece of celery [or other random object]!!"? Yeah, that's Falco, but with ES.
Dear god, they actually ported ES to a piece of celery!
Martin Golding wrote:
"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
cin >> mychar;
switch (mychar) {
case '+':
Add(rest of input)
break;
case '-':
Subtract(rest of input);
break;
};
?
And avansc, you can directly cast from int to char, so #define ADD = '+' //// or in C++, const char ADD = '+' //// or const int ADD = '+' //// would work just fine
Haven't got there yet xD, I'm a novice, don't know much C++ xD
Well, I hope you don't plan on enforcing order of operations, because then it gets all recursive and hard =)
Also, read up on switch :). It can only handle types that are equivalent to a number or true/false (in other words, no case "this is my string!" allowed, but case 'a' or case 1 are fine), but it's still really handy.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
MarauderIIC wrote:Well, I hope you don't plan on enforcing order of operations, because then it gets all recursive and hard =)
Also, read up on switch . It can only handle types that are equivalent to a number or true/false (in other words, no case "this is my string!" allowed, but case 'a' or case 1 are fine), but it's still really handy.
Ya, I'm just going to go in the order the book lays it down for me though, I think they know better than I xD.