C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why does this work? (Newbie Question)

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
usenetaccount@mailsent.ne
Guest





PostPosted: Sat Jan 29, 2005 5:38 am    Post subject: Why does this work? (Newbie Question) Reply with quote



Hi all,

I'm a self-learning programmer, making my way through a book and this
popped up (originally to access an array) with no explantion. Prolly
missed it but... in the following, why does (c - 'a') return an int?

#include <iostream>
using namespace std;

int main() {
while(1) {
char c, cr;
cout << "pick a letter or 'q' to quit: ";
cin.get(c); cin.get(cr); // second one for CR
if (c == 'q') break;
cout << "Number " << ((c - 'a')+ 1) << " in the alphabet." << endl;
}
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jack Klein
Guest





PostPosted: Sat Jan 29, 2005 9:36 am    Post subject: Re: Why does this work? (Newbie Question) Reply with quote



On 29 Jan 2005 00:38:12 -0500, [email]usenetaccount (AT) mailsent (DOT) net[/email] wrote in
comp.lang.c++.moderated:

Quote:
Hi all,

I'm a self-learning programmer, making my way through a book and this
popped up (originally to access an array) with no explantion. Prolly
missed it but... in the following, why does (c - 'a') return an int?

(c - 'a') is an expression, not a function. It does not return
anything. It yields a value.

It yields a value of type int, because while you write code to
subtract the values of two chars, both of them undergo default
promotion to int and actually one int value is subtracted from another
int value.

Quote:
#include using namespace std;

int main() {
while(1) {
char c, cr;
cout << "pick a letter or 'q' to quit: ";
cin.get(c); cin.get(cr); // second one for CR
if (c == 'q') break;
cout << "Number " << ((c - 'a')+ 1) << " in the alphabet." << endl;

You know, I hope, that this will generate incorrect output on
implementations that do not use ASCII as the execution character set.
Even on ASCII platforms, the output is meaningless if the input is not
a lower case letter of the alphabet.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Maeder
Guest





PostPosted: Sat Jan 29, 2005 3:28 pm    Post subject: Re: Why does this work? (Newbie Question) Reply with quote



[email]usenetaccount (AT) mailsent (DOT) net[/email] writes:

Quote:
I'm a self-learning programmer, making my way through a book and this
popped up (originally to access an array) with no explantion. Prolly
missed it but... in the following, why does (c - 'a') return an int?

#include <iostream

Side note 1: The functionality that the main() function uses requires


Quote:
using namespace std;

int main() {
while(1) {
char c, cr;
cout << "pick a letter or 'q' to quit: ";
cin.get(c); cin.get(cr); // second one for CR

Side note 2: Please note that these I/O operations can fail. Your code
should check for successful input before attempting to interpret its
results. E.g.:

if (std::cin.get(c) && std::cin.get(cr))
; // interpret c and/or cr
else
break;


A more robust solution would be to not read one single character after
c, but everything up to the end of the line. E.g.:

std::string line;
if (getline(std::cin,line) && !line.empty())

and use line[0] instead of c.


Quote:
if (c == 'q') break;
cout << "Number " << ((c - 'a')+ 1) << " in the alphabet." << endl;

c and 'a' are of type char, which is an integer type. Operations such
as addition and subtraction are thus available. 'a' is the char value
that your program uses to encode the character a.

If your program uses a character encoding that encodes the Latin
characters in the "natural" order (a first, z last, no holes, ...),
the expression c-'a' will thus return the rank of the character
encoded by c in the Latin alphabet.

Note that C++ does not guarantee such a character encoding. On some
C++ implementations, your program might have "interesting" results.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
user105
Guest





PostPosted: Sun Jan 30, 2005 4:00 am    Post subject: Re: Why does this work? (Newbie Question) Reply with quote

Thanks,

I realized that 'return' was not the right word to use soon after I
posted!

OK, I thought 'well, why not simply cast the char to an int instead of
subtracting an 'a' char'? But it doesn't work quite the same way. Is
this just an idiosyncrasy of the language, where a char 'default
promotion' to an int works slightly differently than casting?


James


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tim Rowe
Guest





PostPosted: Tue Feb 01, 2005 12:42 am    Post subject: Re: Why does this work? (Newbie Question) Reply with quote

On 29 Jan 2005 23:00:07 -0500, "user105" <usenetaccount (AT) mailsent (DOT) net>
wrote:

Quote:
Thanks,

I realized that 'return' was not the right word to use soon after I
posted!

OK, I thought 'well, why not simply cast the char to an int instead of
subtracting an 'a' char'? But it doesn't work quite the same way. Is
this just an idiosyncrasy of the language, where a char 'default
promotion' to an int works slightly differently than casting?

If you just cast the character to an int you get it's position in
whatever character encoding scheme is used, which is not the same as
the characters position in the alphabet. That's not an idiosyncrasy
of the language, but of the character coding. You'll find the same
thing in most languages.
Replies to tim at digitig dot co dot uk

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.