Simple noob question

Hello folks,

I'm coming from the classic VB 6 and I'm facing some dificulties to perform simple tasks with C++. For example, how would be the correct way to convert and format an int integer to a two digit string?

In VB would be something like this:

1
2
3
4
5
6
7
8
9
10
11
12
Dim i As Long

For i = 1 to 10
    Print Format$(i,"00")
Next

In C++ would be...

for (int i = 1; i <= 10; i++) {
    cout << (?) i (?) << endl;
}


I know this is quite easy to do, I just need to know the right way to do it :)

Regards,

In C, strings and integers are worlds apart (well, not really...).

You could use sprintf to convert anything to string including integers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#define SIZE 3 /* We only have a 2 digit number, so this is fine. */
int main (int argc, char **argv) {
    int x = 56, y = 67;
    char a_string[SIZE], another_string[SIZE];

    snprintf (a_string, SIZE, "%d", x); /* The 2nd parameter ensures the resulted string never exceeds SIZE. */

    /* Another example.
     * It is only correct when using ASCII strings.
     */
    another_string[0] = y/10+0x30; /* Will fetch us 6 and we add 0x30 to it. */
    another_string[1] = y-y/10*10+0x30; /* Will fetch us 7 and we add 0x30 to it. */
    another_string[2] = '\0';  /* Terminate the string with a 0.
    There, we got our 6 and 7, 67, into a null terminated string. */

    printf ("a_string = %s;\nanother_string = %s;\n\n", a_string, another_string);
    return 0;
}

Will output:

a_string = 56;
another_string = 67;



The snprintf will convert x into a string and store it into the specified variable.
A non-standard function would be itoa() but don't rely on it.

The second trick is to extract one number at a time and add the ASCII value 0x30 which is actually a '0' character.
That will convert it, although a better solution would be to use a loop (not how I did).

Anything else you may wish to ask or are uncertain about?
Last edited on
unoriginal,

Thanks for your explanation.

Well, as a beginner, I'm uncertain about almost everything, but I think I got what you did.
Let me give you a real example to clarify what I really need to do:

I have a bitset with some bits turned on representing combinations of numbers.
Each element of the combination is represented by the position of each bit into the bitstring (right-to-left order)
ex.: 10010110 = 02 03 05 08

1
2
3
4
5
6
7
8
9
10
11
12
13
bitset<32> bitcombinations;

// Populating the bitset
bitcombinations.reset();
	
	for (int i = 0; i <= 10; i++) {

		bitcombinations.set(i,true);

	}

cout << bitcombinations << endl; // 00000000000000000000001111111111


My question is, how to covert this bitstring into a "string combination" concatenating the position's values formatted by 2 digits?

The code would be:
1
2
3
4
5
6
7
8
9
10
11
12

	for (int i = 0; i < 32; i++) {
		
		if (bitcombinations.at(i)) {
			
			// pseudo-code:
			//combstring = combstring & " " & i + 1;
		}

	}

cout << combstring << endl; // the output should be: "01 02 03 04 05 06 07 08 09 10" 



So unoriginal, how would be the proper way to construct such strings? Should I use string.h and stdlib.h?
I feel that I did not get how the things works in C/C++ yet because I'm still with the VB's concept in my mind. If you are also a VB programmer, I'm sure you know what I mean :)

But thank you, I think I already found some answers based on your explanations.

Regards,
equinoX
Last edited on
Look at the programme below and you might understand:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

int main (int argc, char **argv) {
	unsigned long bits, i = 1;
	int counter = 1;
	char string [87];
	string[0] = '\0';

	/* This is equal to 00000000000000000000001111111111
         * in binary. The first 10 bits are set.
         */
	bits = 0x3FF;

	while (counter <= 32) {
		if (bits & i) sprintf (string, "%s %02d", string, counter); 
		i += i;
		++counter;
	}
	puts (string);

    return 0;
}

Output:

 01 02 03 04 05 06 07 08 09 10


I hope the above code is readable and understandable.
It is actually quite simple.
We first check whether a certain bit is set in the variable or not with the if (variable & bit_to_check) (done so on line 15). If that bit is set we take the counter variable and append it to the string.

Simple as that!

Can you now implement this technique in your program?
Last edited on
100% understood :) you gave a different (and smarter) soluction to the problem.

Thanks for your help unoriginal, guys like you are an incentive to noobs like me.

Regards,
equinoX
Last edited on
Thanks for the support moorecm :) I will take a look at the links

Regards,
equinoX
The setfill one has an example just like your first question...
Topic archived. No new replies allowed.