Page 1 of 1
How to check the value of a char?
Posted: Fri Nov 07, 2008 12:00 am
by XianForce
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:
Code: Select all
char a;
std::cin >> a;
std::cout << std::endl;
if(a==Add)
Add();
if(a==Subtract)
Subtract();
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?
Re: How the check the value of a char?
Posted: Fri Nov 07, 2008 12:18 am
by jesterguy
Re: How the check the value of a char?
Posted: Fri Nov 07, 2008 12:19 am
by XianForce
Re: How the check the value of a char?
Posted: Fri Nov 07, 2008 12:26 am
by jesterguy
well it worked for me in my text rpg I made awhile ago and it was in c++.
Re: How the check the value of a char?
Posted: Fri Nov 07, 2008 12:33 am
by mokkan
Why not just use std::string if you're using C++? Here's some code:
Code: Select all
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
cout << "What operation would you like to perform? ";
cin >> input;
if(input == "add") {
cout << "You chose the 'add' operation." << endl;
} else if (input == "subtract") {
cout << "You chose the 'subtract' operation." << endl;
} else if (input == "divide") {
cout << "You chose the 'divide' operation." << endl;
} else if (input == "multiply") {
cout << "You chose the 'multiply' operation." << endl;
} else {
cout << "Invalid operation." << endl;
}
return 0;
}
Re: How the check the value of a char?
Posted: Fri Nov 07, 2008 1:07 am
by XianForce
mokkan wrote:Why not just use std::string if you're using C++? Here's some code:
Code: Select all
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
cout << "What operation would you like to perform? ";
cin >> input;
if(input == "add") {
cout << "You chose the 'add' operation." << endl;
} else if (input == "subtract") {
cout << "You chose the 'subtract' operation." << endl;
} else if (input == "divide") {
cout << "You chose the 'divide' operation." << endl;
} else if (input == "multiply") {
cout << "You chose the 'multiply' operation." << endl;
} else {
cout << "Invalid operation." << endl;
}
return 0;
}
lol, duh should've used a string, thanks lol
Re: How to check the value of a char?
Posted: Fri Nov 07, 2008 7:24 am
by Trask
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:
Code: Select all
char a;
std::cin >> a;
std::cout << std::endl;
if(a==Add)
Add();
if(a==Subtract)
Subtract();
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.
Re: How to check the value of a char?
Posted: Fri Nov 07, 2008 8:18 am
by avansc
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.
Re: How to check the value of a char?
Posted: Fri Nov 07, 2008 10:30 am
by MarauderIIC
In line with avansc, why are you not doing
Code: Select all
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
Re: How to check the value of a char?
Posted: Fri Nov 07, 2008 10:37 am
by Trask
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?
Re: How to check the value of a char?
Posted: Fri Nov 07, 2008 11:43 am
by XianForce
MarauderIIC wrote:In line with avansc, why are you not doing
Code: Select all
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
Re: How to check the value of a char?
Posted: Fri Nov 07, 2008 1:08 pm
by MarauderIIC
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.
Re: How to check the value of a char?
Posted: Fri Nov 07, 2008 2:30 pm
by XianForce
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.