Hi

I would like to save integer values from 0 to n ( n = 112 e.g.) to a char array. How can I do this?

char  numbers[n];

for (int i = 0; i<= n; i++)
{
    numbers[i] = i // or numbers[i] = 'i'; when I write code like this, the value that is 
                   // recorded in numbers will be ASCII equivalent of i. But I would like to
                   // record i not ASCII equivalent. How can I do this.
}   

Recommended Answers

All 4 Replies

Well right now you are just storing from 0 to n (0, 1, 2, 3...) in numbers[] array. So numbers[4] will be 4 and so on.

Is that what you want to do? I don't know if you have another source of int's (besides this for) that you want to copy from but didn't mention here.

If you want to do what it seems, i think you're doing it fine. The problem is probably the way you are reading (printing) numbers. Since it's a char, numbers will be printed with their ASCII translations. If you don't want that, just declare numbers[] as int, or print char numbers[] like int's.

Thank you for answer.

I want to print char numbers[] like int's. How can I do this?

I would encourage to use int numbers[] and avoid type casting, but if you still want to do this, using cout would be something like cout << int << numbers[i]; or cout << int(numbers[i]);

I don't have a compiler right now so i'm not 100% sure.

I would encourge a complete description of your qestion, with understandable examples. I for one am confused by your question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.