Individual Digit Management in intetegers

I was writing up a program for calling up data from a file when I realized the way I "coded" it didn't have a way to call it back effectively. That probably makes no sense, even to me. However, simple put, I was curious if there was a way to change a single digit of an integer to what you want.
ie:
22 to 12 without subtracting ten, just changing the digit.
I was trying something with arrays, but it didn't work (and honestly I didn't expect it to.) I was running a test to see if you have an integer array but print it without defining which part of the array to print.

1
2
3
4
5
6
7
8
9
#include <stdio.h>

main()
{
int test[2];
test[0] = 1;
test[1] = 2;
printf("%d", &test);
}

This is specifically what I tried, but it didn't work.


Bottom line: is there a way to change the digits in integers without having to do a mathematical calculation? Or would I have to write up a function to do it for me? Amd is that possible?

Thanks in advance.
Yes it's possible and can be done using arrays !
1
2
3
4
	char myarr[] = "678";
	myarr[2] = '1';
	
	cout << myarr;


1
2
3
4
	int myint[] = {6,7,8};
	myint[2] = 1;
	
	cout << myint[0] << myint[1]<< myint[2];
Last edited on
For a long string of numbers,

1
2
3
4
for (int i = 0; i < ARRAY_SIZE; i++)
{
   cout << array[i];
}
Topic archived. No new replies allowed.