Join TSR
 
About Us | FAQs | Sign in
 
Advanced
Search

Join The Student Room Today

Be part of the UK's largest and fastest growing student community.

It's free to join and a lot of fun - Get inspired, express your ideas, interact and share

RSS  From C++ to PHP, debugging to webhosting; help and discussion about writing your latest program to running your website. NOT for help when your PC won't work.
Reply
 
Announcements   Posted By
 
Old 2 Weeks Ago: 4th November 2009 04:11 #1 
G A B R I E L G A B R I E L is offline
Junior Member
Thread Starter
G A B R I E L will become famous soon enough
Join Date: Oct 2009
Posts: 57
Default C++ char question
 
I have this code:

Code:
if (aChar == '0' || '1' || '2' || '3' || '4' || '5'|| '6' || '7' || '8' || '9') return DIGIT // (I've defined DIGIT as something else I need to use later

Basically my question is

(1) Is there an easier way to do that i.e. if I were to do it with say, the upper case alphabet would I have to write out every single letter (or is there is an easier way)

(2) If I just wanted to return DIGIT for digits, and say, return SOMETHING I(which i've defined as somethign) for anything thats not a digit (space, underscore, uppercase, lowercase, etc), how would I do that
Register to remove banners from posts.
Old 2 Weeks Ago: 4th November 2009 04:24 #2 
mwnciau mwnciau is offline
New Member
mwnciau will become famous soon enough
Join Date: Jul 2009
Posts: 5
 
(1) You could use regular expressions

(2) Expanding on (1), you could use regular expressions to check if the char is a digit in which case return DIGIT, otherwise return SOMETHING
Old 2 Weeks Ago: 4th November 2009 07:46 #3 
Psyk's Avatar
Psyk Psyk is offline Male
TSR Legend
Psyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sense
United Kingdom
Join Date: Sep 2005
Location: Leamington Spa
Posts: 10,295
My Societies
Default Re: C++ char question
 
You could do the check based on numeric ASCII values. So just check that aChar is between 48 and 57 (inclusive) as they are the ASCII values for the characters 0 and 9.
Old 2 Weeks Ago: 4th November 2009 12:29 #4 
G A B R I E L G A B R I E L is offline
Junior Member
Thread Starter
G A B R I E L will become famous soon enough
Join Date: Oct 2009
Posts: 57
Default Re: C++ char question
 
Alright, thanks I get that. But I've defined aChar as char, so wouldn't I need to do: int(aChar) to get the ASCII value?
Old 2 Weeks Ago: 4th November 2009 12:38 #5 
G A B R I E L G A B R I E L is offline
Junior Member
Thread Starter
G A B R I E L will become famous soon enough
Join Date: Oct 2009
Posts: 57
Default Re: C++ char question
 
Originally Posted by spikeymike
See code edit. You put a cast around the number.

My original code was wrong.

What does that do? (as opposed to what I suggested)?
Old 2 Weeks Ago: 4th November 2009 12:45 #6 
spikeymike's Avatar
spikeymike spikeymike is online now Male
TSR Legend
spikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own good
Scotland
Join Date: Feb 2008
Location: Scotland
My Societies
Default Re: C++ char question
 
Originally Posted by G A B R I E L
What does that do? (as opposed to what I suggested)?

It's the equivalent to yours but for some reason it doesn't work properly. Forget my code... this works however.

@Psyk: Why did you mention ASCII? More complicated than what it should have been...

Code:
#include <iostream> using namespace std; int main () { int aChar; cin >> aChar; if (aChar >= 0 && aChar <= 9) cout << "ok"; system("PAUSE"); }

Basically, if aChar is greater than or equal to 0 and less than/equal to 9 then print "ok".
 
Old 2 Weeks Ago: 4th November 2009 12:47 #7 
G A B R I E L G A B R I E L is offline
Junior Member
Thread Starter
G A B R I E L will become famous soon enough
Join Date: Oct 2009
Posts: 57
Default Re: C++ char question
 
I did think about that but later on in the program I need to consider the whole uppercase alphabet so I guess resorting to ASCII would be what needs to be done. But as I said I wasnt sure if int(aChar) is the right way
Old 2 Weeks Ago: 4th November 2009 12:51 #8 
spikeymike's Avatar
spikeymike spikeymike is online now Male
TSR Legend
spikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own good
Scotland
Join Date: Feb 2008
Location: Scotland
My Societies
Default Re: C++ char question
 
Originally Posted by G A B R I E L
I did think about that but later on in the program I need to consider the whole uppercase alphabet so I guess resorting to ASCII would be what needs to be done. But as I said I wasnt sure if int(aChar) is the right way

Ok, going back to the ASCII stuff. And you're right aChar cannot be int if you're using Psyk's suggestion.

Your aChar has to be a char, and the number that represents a particular letter/number etc has to be put into a cast..

Code:
char aChar; cin << aChar; if (aChar == char(101)) // checks for 'e' // do this
 
Old 2 Weeks Ago: 4th November 2009 13:12 #9 
G A B R I E L G A B R I E L is offline
Junior Member
Thread Starter
G A B R I E L will become famous soon enough
Join Date: Oct 2009
Posts: 57
Default Re: C++ char question
 
Originally Posted by spikeymike
Ok, going back to the ASCII stuff. And you're right aChar cannot be int if you're using Psyk's suggestion.

Your aChar has to be a char, and the number that represents a particular letter/number etc has to be put into a cast..

Code:
char aChar; cin << aChar; if (aChar == char(101)) // checks for 'e' // do this


Alright thanks, that works.
Old 2 Weeks Ago: 4th November 2009 13:15 #10 
DFranklin DFranklin is offline
TSR Legend
TSR Moderation Team: Moderator
DFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own good
United Kingdom
Join Date: Jan 2007
Location: London
Posts: 10,515
My Societies
Default Re: C++ char question
 
Originally Posted by spikeymike
Basically, if aChar is greater than or equal to 0 and less than/equal to 9 then print "ok".
Are you sure your code works? You want

if (aChar >= '0' && aChar <= '9') // note the '0' and '9'
...

surely?

And for the alphabet:

if (aChar >= 'A' && aChar <='Z') etc...

Edit: you might also want to google isalpha and isdigit.

Last edited by DFranklin : 2 Weeks Ago at 13:19.

Old 2 Weeks Ago: 4th November 2009 13:19 #11 
spikeymike's Avatar
spikeymike spikeymike is online now Male
TSR Legend
spikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own goodspikeymike  has too much reputation for their own good
Scotland
Join Date: Feb 2008
Location: Scotland
My Societies
Default Re: C++ char question
 
Originally Posted by DFranklin
Are you sure you're code works? You want

if (aChar >= '0' && aChar <= '9') // note the '0' and '9'
...

surely?

And for the alphabet:

if (aChar >= 'A' && aChar <='Z') etc...

Edit: you might also want to google isalpha and isdigit.

The bit in orange... code works, tried it before posting.

The bit in bold... I doubt very much that would work. I've always used ASCII if I'm validating a letter.
 
Old 2 Weeks Ago: 4th November 2009 13:29 #12 
DFranklin DFranklin is offline
TSR Legend
TSR Moderation Team: Moderator
DFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own good
United Kingdom
Join Date: Jan 2007
Location: London
Posts: 10,515
My Societies
Default Re: C++ char question
 
Originally Posted by spikeymike
The bit in orange... code works, tried it before posting.
Actually it doesn't, not really.

I hadn't noticed that aChar was an int (nice choice of variable name - this is why I hate naming schemes that include the variable type).

So you're not actually doing what was asked - the input is being interpreted as decimal - you're not actually doing anything based on the value of the characters entered. (To be clear, the OP has a variable aChar of type char. If you were to write "int aInt = aChar;" and then "if (aInt >=0 && aInt <=9) ..." you'd get the wrong result).

If you enter 10, it won't say OK, which I very much doubt is the desired outcome.

Also, if the user doesn't enter an digit, then aChar doesn't get written to, so you're left with what ever was in there before.

So it *can* still say OK even if you don't enter a digit. (Try initialising aChar to 5 before the call to cin to see this happen).

The bit in bold... I doubt very much that would work. I've always used ASCII if I'm validating a letter.
It works fine (aChar has to be a char, (or an int created by casting a char to an int)) of course. It's arguably more portable, although it still goes horribly wrong for something like EBCDIC where the alphabet isn't a single contiguous range.

Last edited by DFranklin : 2 Weeks Ago at 13:36.

Old 2 Weeks Ago: 4th November 2009 18:09 #13 
Psyk's Avatar
Psyk Psyk is offline Male
TSR Legend
Psyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sense
United Kingdom
Join Date: Sep 2005
Location: Leamington Spa
Posts: 10,295
My Societies
Default Re: C++ char question
 
I'm sure you can do a comparison between a char and an int without any casting. But I guess it's sensible to do the cast even if it's not strictly necessary.

Now I think about it, you don't really need to use the numeric ASCII values, just checking that it's between '0' and '9' should do (note the quotes, they mean the character '0' and not the number 0). The same should work for 'a'...'z' and 'A'...'Z', assuming the platform you're working on uses ASCII (as DFranklin says, this might not work for some character encoding schemes).
Old 2 Weeks Ago: 4th November 2009 18:20 #14 
DFranklin DFranklin is offline
TSR Legend
TSR Moderation Team: Moderator
DFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own good
United Kingdom
Join Date: Jan 2007
Location: London
Posts: 10,515
My Societies
Default Re: C++ char question
 
If the "cast" comment was directed at me, let me clarify:

If you have a variable aChar of type char, then the comparison "aChar >=0 && aChar <=9" does NOT correctly identify if aChar is a decimal digit (assuming the ASCII character set).
The correct comparison is "aChar >= '0' && aChar <= '9'".

The reason spikeymike's code works is because the code "cin >> aInt; " (where aInt is an integer) actually parses the input to create an integer. So the intent of me saying "int created by casting a char to an int" was to contrast casting the input with using cin to parse it. You don't need an explicit cast, but "int aInt = aChar;" still involves a cast operation, even if it's implicit.

[Not that the details hugely matter - it was the contrast with cin I was trying to emphasize].
Old 2 Weeks Ago: 4th November 2009 18:33 #15 
Psyk's Avatar
Psyk Psyk is offline Male
TSR Legend
Psyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sensePsyk has more reputation than sense
United Kingdom
Join Date: Sep 2005
Location: Leamington Spa
Posts: 10,295
My Societies
Default Re: C++ char question
 
Oh ok that makes sense. But am I right in thinking that something like this is valid?

Code:
char c = '0'; if(c == 48) { return true; }
Old 2 Weeks Ago: 4th November 2009 18:57 #16 
gregmantis gregmantis is offline Male
Respected Member
gregmantis has a brilliant futuregregmantis has a brilliant futuregregmantis has a brilliant futuregregmantis has a brilliant futuregregmantis has a brilliant futuregregmantis has a brilliant futuregregmantis has a brilliant futuregregmantis has a brilliant futuregregmantis has a brilliant future
Join Date: Aug 2007
Location: UK
Posts: 206
Default Re: C++ char question
 
Originally Posted by Psyk
Oh ok that makes sense. But am I right in thinking that something like this is valid?

Code:
char c = '0'; if(c == 48) { return true; }

Yes. A char is basically an integer type where sizeof(char) is guaranteed to be 1 (though whether it is signed or unsigned by default is implementation specific).

Here's an interesting little program:

Code:
extern "C" int printf( const char*, ... ); int main() { char c = 0; do { printf( "%d '%c'\n", c, c ); } while ( ++c ); }

Last edited by gregmantis : 2 Weeks Ago at 19:01.

Old 2 Weeks Ago: 4th November 2009 19:09 #17 
gregmantis gregmantis is offline Male
Respected Member
gregmantis has a brilliant futuregregmantis has a brilliant futuregregmantis has a brilliant futuregregmantis has a brilliant futuregregmantis has a brilliant futuregregmantis has a brilliant futuregregmantis has a brilliant futuregregmantis has a brilliant futuregregmantis has a brilliant future
Join Date: Aug 2007
Location: UK
Posts: 206
Default Re: C++ char question
 
Originally Posted by gregmantis
Code:
extern "C" int printf( const char*, ... ); int main() { char c = 0; do { printf( "%d '%c'\n", c, c ); } while ( ++c ); }

What's also quite interesting (subjective) is if you play with different types for c.

Code:
extern "C" int printf( const char*, ... ); int main() { unsigned short c = 0; do { printf( "%d %d '%c'\n", c, (char)c, c ); } while ( ++c ); }
Old 2 Weeks Ago: 4th November 2009 19:12 #18 
DFranklin DFranklin is offline
TSR Legend
TSR Moderation Team: Moderator
DFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own goodDFranklin  has too much reputation for their own good
United Kingdom
Join Date: Jan 2007
Location: London
Posts: 10,515
My Societies
Default Re: C++ char question
 
@gregmantis: "Most" interesting if c is a double, of course...
 
Thread Tools Search this Thread
Search this Thread
Advanced
Search