How to check the value of a char?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

How to check the value of a char?

Post 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?
Last edited by XianForce on Fri Nov 07, 2008 1:06 am, edited 1 time in total.
User avatar
jesterguy
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Wed Oct 08, 2008 8:00 pm
Location: Illinois
Contact:

Re: How the check the value of a char?

Post by jesterguy »

I think this is what you need but not positive
http://www.cplusplus.com/reference/clib ... trcmp.html
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
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: How the check the value of a char?

Post by XianForce »

jesterguy wrote:I think this is what you need but not positive
http://www.cplusplus.com/reference/clib ... trcmp.html
That looks like its C.
User avatar
jesterguy
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Wed Oct 08, 2008 8:00 pm
Location: Illinois
Contact:

Re: How the check the value of a char?

Post by jesterguy »

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")
User avatar
mokkan
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 3
Joined: Thu Nov 06, 2008 6:10 pm
Location: Washougal, WA, USA
Contact:

Re: How the check the value of a char?

Post 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;
}
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: How the check the value of a char?

Post 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
User avatar
Trask
ES Beta Backer
ES Beta Backer
Posts: 738
Joined: Wed Oct 29, 2008 8:17 pm
Current Project: Building a 2D Engine
Favorite Gaming Platforms: Sega Genesis and Xbox 360
Programming Language of Choice: C/C++
Location: Pittsburgh, PA
Contact:

Re: How to check the value of a char?

Post 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.
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."
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: How to check the value of a char?

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: How to check the value of a char?

Post 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
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Trask
ES Beta Backer
ES Beta Backer
Posts: 738
Joined: Wed Oct 29, 2008 8:17 pm
Current Project: Building a 2D Engine
Favorite Gaming Platforms: Sega Genesis and Xbox 360
Programming Language of Choice: C/C++
Location: Pittsburgh, PA
Contact:

Re: How to check the value of a char?

Post 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?
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."
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: How to check the value of a char?

Post 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
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: How to check the value of a char?

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: How to check the value of a char?

Post 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.
Post Reply