2

I'm new to C++ and I'm trying to do something that should be pretty basic.

I have a small loop in C++ that just displays a sequence of numbers and I would like to convert these numbers into specific ASCII characters. Something like this:

    for (int k = 0; k < 16; k++) {
        display(65+k);
    }

And the result should look like this:

ABCDEFGH... etc

Any ideas?

Thanks!

mar1980
  • 31
  • 2

2 Answers2

3

EDIT based on clarification: Judging from the error message display takes a C-style string. You can build one like this:

for (int k = 0; k < 16; k++) {
    char str[2] = { 65 + k };  // Implicitly add the terminating null.
    display(str);
}
Mark B
  • 95,107
  • 10
  • 109
  • 188
  • This is an incorrect solution. The OP wanted to output *ASCII* characters, but the value of 'A' depends on the host encoding. If you compile this on a AS/400 then it would send 0xc2 0xc3 0xc4 .. to the output stream. – Nordic Mainframe Jul 28 '10 at 22:05
  • @Luther The OP specifically requested that the result be characters. I don't understand how `std::cout` on such a system wouldn't translate `'A'+1` for example into `'B'` on the display, regardless of what numeric representation the host actually uses. – Mark B Jul 28 '10 at 23:08
  • cout does not know how to make an 'A' on your display. It only knows how to send character codes to your display (or a file) and its up to your display to interpret the codes and show an 'A' for a specific code. If the program and the display disagree on the code for 'A' you have a problem. – Nordic Mainframe Jul 28 '10 at 23:20
  • The OP made the requirement that the program should produce characters in the ASCII encoding, which means that a 65 should be produced for an 'A' and so on. But if the program is compiled under a system where an 'A' in the source means a differnet code (EBCDIC), then we have the disagree situation. Therefore, we can not use 'A' to produce ASCII, but have to use the character code directly. – Nordic Mainframe Jul 28 '10 at 23:24
  • Actually the OP has impossibly conflicting requirements as I read it now: First that it produce ASCII and second that it produce characters such as "ABCDEFGH". My approach (I believe) solves that latter question, and I agree it won't always produce ASCII output. – Mark B Jul 28 '10 at 23:41
  • Sorry, I didn't clarify the question. The display() method above requires that a string be passed to it. So really what I need is to go from an integer say 65 to a string (in this case the letter A). With the code suggested I get the error "Invalid conversion from 'char' to 'const char*'. Thanks again for your help! – mar1980 Jul 29 '10 at 04:43
1

That would be

#include <iostream>  
int main() {  
for (int k = 0; k < 16; k++) {
        std::cout.put(65+k);
    }
}

for C++

Nordic Mainframe
  • 28,058
  • 10
  • 66
  • 83
  • 2
    an improvement would be to replace 65 with 'A' – Clark Gaebel Jul 28 '10 at 21:48
  • Not really. Actually we have not guarantee that the codes following 'A' are letters at all. If this ran on a AS/400 the program would produce a file of gibberish for anyone reading it as ASCII. 65 is *correcter* than 'A', but 'A' is nicer to the loose^H^H^H^H^H people who haven't memorized the ASCII table. – Nordic Mainframe Jul 28 '10 at 22:14
  • Luther is correct that this solution will portably produce ASCII characters in any environment with at least 7-bit wide streams (while C `char` type must be at least 8 bits, that doesn't prevent a narrowing conversion when writing to disk or stdout), regardless of whether the environment uses ASCII as its native format. – Ben Voigt Jul 29 '10 at 13:19