Quote:
quote:Originally posted by nokomis
OK! Just so! 'a' etc fine in that sense!
Why bother with the complexities of cin, cout, getline and that stuff when gets(), and all the printf() and scanf() procedures are there to help?
Georges
|
When learning C++, I think it is entirely appropriate to deal with cin, cout, getline, etc. The "old-fashioned" C-style ways still work, but lots of powerful tools become available if you learn about C++ functions (use of C++ strings, for example).
By the way, use fgets(), not gets(), to make sure you don't overflow whatever input buffer you are using.
Also, note that for example 'a' evaluates to an integer value of 97 (decimal) and can be used any place that an int variable is legal.
try this:
Code:
#include <stdio.h>
int main()
{
int x;
char y;
for (x = 'a'; x <= 'd'; x++) {
switch (x) {
case 'a': printf("x = 0x%02x (%d decimal)\n", x, x);
break;
case 'b': printf("x = '%c' (%d decimal, 0x%02x hex)\n", x, x, x);
break;
default : printf("x = %3d (0x%02x hex)\n", x, x);
}
}
y = x;
printf("y = '%c' (%d decimal)\n", y, y);
return 0;
}
Note that this is a valid C program and a valid C++ program.
Dave
Dave