0

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.
}   
3
Contributors
4
Replies
5
Views
4 Years
Discussion Span
Last Post by WaltP
0

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.

0

Thank you for answer.

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

0

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.

Edited by neithan

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.