Comma Delimited Number from Array

Need some help trying to write a way to properly format numbers by adding "," to numbers. Ex:
1000000 -> 1,000,000
30000000000000 -> 30,000,000,000,000

In the usual case, its an easy problem. But it gets tricky in my case, since I am working with numbers up to 30 digits, unable to store them in any int, long, long long. Due to this obstacle, the user inputs a number and each digit is stored in my array individually.
I need these commas to print as I am cout the array. This means I only have array length to work with.

I want to use modulus, but I'm not sure how this would work.
I have:
1
2
3
4
5
6
7
8
9
10
  for(int i=0; i <= arrSize; i++){
        remainingArr--;
        cout << Array[i];
        
        if (counter%3 == 0) {
            cout << ",";
        }counter++;
    }
    cout << endl;
}


This works, but only in the millions scenario.
This means 10000000 -> 1,000,000,0.

So my question: What conditions can I add that will detect if I need to skip the comma in the case of a 10 Million/Billion case, as well as skip the comma at the end (1,000,000,)?

Thanks
- Tom
Why not just take input as a std::string and then output right away?
1
2
3
4
5
6
std::string num;
if(std::getline(std::cin, num))
{
    //you know num.size()
    //I recommend a for loop
}
I have the input and print a separate function. I just sort of made this exercise to practice working between functions. I want to try and keep it like that, therefore I have to figure out how to do it while printing the array.
Last edited on
Made a little progress, I have :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void prArray(int Array[], const int arrSize) {
    int mod = arrSize % 3;
    int remainingSize = arraySize;
    int counter = 0;
    for(int i=0; i <= arrSize; i++){
        remainingSize--;
        mod = (remainingSize % 3);
        cout << Array[i];
        
        if((i != 0) && (remainingSize > 0) && (remainingSize%3 == 0))
            cout << ",";
        
            }
    cout << endl;
}


Which now outputs the first digite correctly, but leaves 4 digits at the end:
235423452345 -> 23,542,345,2345
Likewise
2354234523450 -> 235,423,452,3450

Any Ideas?
You could at least go with my suggestion to use std::string to contain the digits for you.
Topic archived. No new replies allowed.