-2

So I'm trying to print a table of characters with their corresponding integer values. Below is my code:-

#include <iostream>

using namespace std;

int main()
{
    char i = 'a';
    while (i <= 'z')
    {
        cout << i << '\t' << 'a' + 1 << '\n';
        ++i;
    }
}

I tried to put char('a' + 1) in the code which gave me a whole lot of wrong answers.

Output(correct output) I get by using static_cast in code:- a 97 b 98 c 99 .... z 122

Output I'm getting using the above written code:- a 98 b 98 ..... z 98

So i would like to know what is the difference between char('a'+1) and 'a' + 1 and static_cast(i)?

r0gue
  • 115
  • 1
  • 1
  • 11
  • 1
    There are a few typos in the question and code. – drescherjm Feb 26 '18 at 16:14
  • _"the code to print char values in int."_ What does this mean please? – Lightness Races in Orbit Feb 26 '18 at 16:14
  • 2
    _"char i = a;"_ What is `a`? Did you mean `'a'`? Pay closer attention to the book. – Lightness Races in Orbit Feb 26 '18 at 16:15
  • `'a' + 1` => `98`, while `char ('a' + 1)` => `b`. Why? `1` is an integer, so normal rules of integer promotion apply in the `'a' + 1` addition resulting in integer `98`. In `char ('a' + 1)` the same promotion rules apply, but the result is cast to `char` when (presuming ASCII) results in `b` being displayed. – David C. Rankin Feb 26 '18 at 16:17
  • You are surely getting _error messages_ if you try to compile this code. Are you asking what those error messages mean? – Drew Dormann Feb 26 '18 at 16:18
  • 1
    @DavidC.Rankin: Assuming ASCII. – Lightness Races in Orbit Feb 26 '18 at 16:18
  • 1
    `char i = 'a'; while (i < 'z') { std::cout << i << '\t' << static_cast(i) << '\n'; ++i; }` is this what you want? – Killzone Kid Feb 26 '18 at 16:20
  • sorry for all the typos in the code.... formatted the code correctly and removed all errors. – r0gue Feb 26 '18 at 16:23
  • @KillzoneKid thanks.. that is exactly what i wanted. But can you explain what static_cast(i) does ? – r0gue Feb 26 '18 at 16:25
  • Okay now if there are no errors, what is the problem you are asking about? What is your question? – Lightness Races in Orbit Feb 26 '18 at 16:25
  • @LightnessRacesinOrbit since i got the solution to my code thanks to killzoneKid, now i just want the explanation of static_cast(i). – r0gue Feb 26 '18 at 16:28
  • @r0gue: Isn't that a completely different question? – Lightness Races in Orbit Feb 26 '18 at 16:29
  • @LightnessRacesinOrbit Im sorry for that but yes it is a different question. – r0gue Feb 26 '18 at 16:30
  • 2
    You have just accepted an answer within twenty minutes of asking. Also, you have accepted an answer that you yourself have stated as "lacking explanation". Finally, that answer was posted five minutes after another answer that provided a that same answer, and also added _more_ explanation. – Xetrov Feb 26 '18 at 16:31
  • @VortexYT Im sorry for all the ruckus.... – r0gue Feb 26 '18 at 16:32
  • @r0gue , it's fine, you're going through the learning phase most of us went through upon joining SE. You're just receiving more constructive but stubborn criticism to that I remember. – Xetrov Feb 26 '18 at 16:34
  • Additionally, you have acted upon our advice, which is fine ofc – Xetrov Feb 26 '18 at 16:34
  • 2
    @VortexYT: It's been a long time since I've encountered such a mess. There's a blatant typo in the _title_ for goodness's sake; no research effort shown, no attention to detail, flipping the accept mark back and forth after not even half an hour, the question's changed twice, typos in the original code that entirely change the meaning of the original question..... which _still_ doesn't contain a problem description. I don't think it's hard to see why this endeavour hasn't been very well received. Chalking it up to "being new" is a tough sell with 15.4m existing questions for inspiration. – Lightness Races in Orbit Feb 26 '18 at 16:36
  • @VortexYT Thank you for understanding. – r0gue Feb 26 '18 at 16:36
  • Ah, now I finally understand the question... – Tim Diekmann Feb 26 '18 at 16:44
  • @LightnessRacesinOrbit , well what can I say? You are completely right, and I am not saying that he is completely right ofc. All I'm saying is that OP might require a longer learning phase than many of us did. BTW, your username is so annoying to type out lol. – Xetrov Feb 26 '18 at 16:47
  • @VortexYT Tip: just type `@` then `L` then `` – Lightness Races in Orbit Feb 26 '18 at 17:50

3 Answers3

5

The type of expression 'a' + 1 is int.

Note that there is no char operator+(char, char), but there is int operator+(int, int). This is why a gets promoted to int and there result of the expression is int. See Integral promotion for more details:

In particular, arithmetic operators do not accept types smaller than int as arguments, and integral promotions are automatically applied after lvalue-to-rvalue conversion, if applicable. This conversion always preserves the value.

Whereas char('a' + 1) is char because it explicitly casts int 'a' + 1 to char.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
1

Please post the code, which does compile.

Let's go through it step by step:

char i = a

You are trying to initialize the variable i with the variable a. The same problem in while (i < z), where you try to compare the variable i with the variable z.

In addition you missed the header <iostream>.

So the correct code would be

#include <iostream>

using namespace std;

int main()
{
    char i = 'a';
    while (i < 'z')
    {
        cout << i << '\t' << 'a' + 1 << '\n';  //how can i use char('a'+1)
        ++i;
    }
}

if you want to display 'a' + 1 as a character, you have to explicitly convert it to char, since 'a' + 1 would evaluate to int: char('a' + 1). I think, you are looking for char(i + 1) though.

You may also convert it to a for-loop:

int main() {
    for (char i = 'a'; i < 'z'; ++i) {
        cout << i << '\t' << char(i + 1) << '\n';
    }
}
Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69
0

There are a few compilation errors in your code. You should understand the difference between char i = a and char i = 'a'.

However, coming to the actual question, I believe what you want to print can be achieved as:

#include <iostream>

using namespace std;

int main()
{
    char i = 'a';
    while (i < 'z')
    {
        cout << i << '\t' << static_cast<int>(i) << '\n';
        ++i;
    }
}

If I understand correctly, you wanted to print a table with the alphabet in the first column and the corresponding ASCII value in the second column.

Since you got most of it down, converting the alphabet to its int value is achieved using static_cast<int>(i), which is known as type casting.

A good reading to understand the difference types of casts in C++ and the difference between the C and C++ casts is here.

CK.
  • 312
  • 3
  • 13