C++ Help

Associate
Joined
8 May 2011
Posts
1,068
Location
London
This shouldn't be too hard, I'm just beginning with C++:

In VBA, you can use the split function to split a string in to an array of individual characters. Alternatively, you can use 'mid' to get an individual character from a string.

So basically, a way of taking an input and putting each individual character in to array.

Is there a way to do this in C++? I've been looking but I can't find anything

Thanks
 
Associate
OP
Joined
8 May 2011
Posts
1,068
Location
London
I have another one. I'm trying to convert an arabic number to Roman numerals - only working additively since the task I'm working on completely ignores things like IX meaning 9 - 9 would be VIIII.

This code should, I believe, take off 1000 from the number if it is greater than 1000, and make the first value of the array 'M', and so on for 500 down to 1, in a loop until the number is 0 ('total' is the number)

However, it converts '10' to 00F9F840, and 22 to 0085F81C etc.

Code:
	char out[20];
	for (i = 0; total<=0; i++)
	{
		if(total>=1000) {total = total-1000; out[i] = "M";}
		else if(total>=500) {total = total-500; out[i] = "D";}
		else if(total>=100) {total = total-100; out[i] = "C";}
		else if(total>=50) {total = total-50; out[i] = "L";}
		else if(total>=10) {total = total-10; out[i] = "X";}
		else if(total>=5) {total = total-5; out[i] = "V";}
		else if(total>=1) {total = total-1; out[i] = "I";}
	}

Can anyone see where I'm going wrong?
 
Associate
OP
Joined
8 May 2011
Posts
1,068
Location
London
Exactly as you suggested -
Code:
	char out[20];
	for (i = 0; total<=0; i++)
	{
		if(total>=1000) {total = total-1000; out[i] = 'M';}
		else if(total>=500) {total = total-500; out[i] = 'D';}
		else if(total>=100) {total = total-100; out[i] = 'C';}
		else if(total>=50) {total = total-50; out[i] = 'L';}
		else if(total>=10) {total = total-10; out[i] = 'X';}
		else if(total>=5) {total = total-5; out[i] = 'V';}
		else if(total>=1) {total = total-1; out[i] = 'I';}
	}
 
Associate
OP
Joined
8 May 2011
Posts
1,068
Location
London
Sorry, I've just realised I've been a terrible idiot. Anyway, the code is just the bit relevant to this. The full code is here . The code takes two Roman numerals, adds or subtracts them, and outputs the result as both an Arabic and Roman numeral.

The console looks like this
 
Back
Top Bottom