mar1980
mar1980

Reputation: 31

how to convert an integer value to a specific ascii character in c++

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!

Upvotes: 2

Views: 2408

Answers (2)

Mark B
Mark B

Reputation: 96233

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);
}

Upvotes: 3

Nordic Mainframe
Nordic Mainframe

Reputation: 28737

That would be

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

for C++

Upvotes: 1

Related Questions