 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
usenetaccount@mailsent.ne Guest
|
Posted: Sat Jan 29, 2005 5:38 am Post subject: Why does this work? (Newbie Question) |
|
|
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
|
Posted: Sat Jan 29, 2005 9:36 am Post subject: Re: Why does this work? (Newbie Question) |
|
|
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
|
Posted: Sat Jan 29, 2005 3:28 pm Post subject: Re: Why does this work? (Newbie Question) |
|
|
[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
|
Posted: Sun Jan 30, 2005 4:00 am Post subject: Re: Why does this work? (Newbie Question) |
|
|
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
|
Posted: Tue Feb 01, 2005 12:42 am Post subject: Re: Why does this work? (Newbie Question) |
|
|
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 |
|
 |
|
|
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
|
|