Characters with Integers tabbed to Right

I took a month's break from programming, and got rusty. Here's the problem:

The character 'b' is char('a'+1), 'c' is char('a'+2), etc. Use a loop to write out a table of characters with their corresponding integer values:
a (tab) 97
b (tab) 98
...
z (tab) 122


Thanks for the help, guys! This is my first post here. :)
Last edited on
Character literals are actually the same as the numerical value they represent:

1
2
3
4
if( 'a' == 97 )
{
    // this code will execute because the above statement is true
}


Both are represented the same way in binary. The key difference between the two is how they are converted to text for the human to read.

By default, if you give cout a char, it will interpret the value you give it as a single character and print whatever character that value represents. Whereas if you give cout an int, it will interpret that as a numerical value and will convert the value into a sequence of numerical characters and print those instead.

Example:

1
2
3
4
5
6
7
8
9
10
11
cout << 97;   // <- giving cout an 'int'... so it will print "97"
cout << 'a';  // <- giving cout a 'char'... so it will print "a"

// note again that 'a' == 97.  The only difference here is the type...
// 'a' is a char
// 97 is an int
//
//  so all it takes to 'fool' cout is to change the type:

cout << char(97);  // <- giving cout a 'char'... so it will print "a"
cout << int('a');  // <- giving cout an 'int'... so it will print "97" 
Last edited on
Another hint for tab you can use '\t'
Topic archived. No new replies allowed.