counting with char arrays

Here is what I am trying to do

I have a one dimensional char array to which the size is set at runtime eg.

 
  char array[size]


I have another one dimensional char array of a fixed size which holds a series of values, 96 of them to be exact. eg.

 
  char characterarray[96] = {...}  


I am looking for the cleanest way to iterate through each possible value and print to output.

For example if the value of "size" was set to "3" making "array[]" 0 - 2,
I want to be able to adjust these values in a counting fashion with a number base of 96 (0-95) and output the result so I end up with 96*96*96 values

The output isn't a problem, I'm having trouble with the iteration.
I've tried to use nested for loops but since I need to account for the value of "size" being specified at runtime, I'm having trouble with this.

also. I would like to avoid using any specialized libraries if possible. Whats the best way to do this?

thanks,

Dan
what is the problem with size? why can't you use nested loops like:

1
2
for (int i=0; i<size; i++)
  for (int j=0; j<96; j++) {}

If the loops were nested like that when the first column finished, it would keep it's final value and the second column would start to iterate. like this (base 10, left to right)

800
900
010
020
030

when it should be like this
800
900
010
110
210
310
I think it will be simpler if you use string instead array[] of characters.
one way is to write your own function:

1
2
3
void increment_string ( std::string& s1 ) {
  // increment the value of string as per your logic
}
Last edited on
1
2
3
4
For( const auto &it : array )
{
    std::cout << it << std::endl;  //what ever you want to do here
}
I guess I didn't explain myself very well.
I know how to write a for loop and make a function. The problem is I'm having trouble coming up with logic thats scalable. I tried switching to vectors. but after reading about them, I still haven't found a solution.
something like...

1
2
3
4
5
6
7
8
9
void increment_string ( std::string& s ) {
  for (int i=0; i<s1.length(); i++) {
    if (s[i] == '9') s[i] = '0';
    else {
      ++s[i];
       break;
    }
  }
}


EDIT: I don't know whether it will work exactly as it should but you can form your own logic on similar lines.
Last edited on
Topic archived. No new replies allowed.