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
Joined
2 Jul 2006
Posts
21
Normal C strings are already array like using a null terminator to indicate their end.

Standard string functions http://www.cplusplus.com/reference/cstring/

Use [index] to access an element.

e.g.
Code:
char myString[]="hello world";

char a=myString[3]; //a=fourth char i.e. the first 'l' note : it is fourth because index is zero based

another example going through each element in turn turning it upper case
Code:
char str[] = "hello world";
for(int i = 0; str[i] != '\0'; ++i)  //loop until null reached, you could also use strlen() here
{
    str[i] = toupper(str[i]);
}



Stl strings are another route that is sometimes more flexible to use ....

Standard Template Library Strings Standard string functions http://www.cplusplus.com/reference/string/string/

Use the at operator to get access to a specific location in an STL string

Code:
str.at(10);


There are other alternatives but those will likely do what you need and are the most portable.
 
Soldato
Joined
14 Oct 2003
Posts
7,831
To me that looks like an array of type chars and you'd reference the elements in the array with mystring[0] (1), mystring[1] (2).
 
Last edited:
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?
 
Soldato
Joined
13 Feb 2003
Posts
6,157
you must use single quotes for chars. Also you have 'total<=0' in your for expression, which doesn't make sense given the following code.

Generally, you should copy/paste code you want help with - what you have wont even compile, yet you say you get results.
 
Last edited:
Soldato
Joined
13 Feb 2003
Posts
6,157
great, let me get my crystal ball and then I will know what you changed, and how you changed it.


I'd also not recommend using printf (et al). There is just no need for it in c++ when std::cout is available and more friendly.
 
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
 
Soldato
Joined
14 Oct 2003
Posts
7,831
Put the brace after the for declaration:

for (i = 0; total<=0; i++) {
foo..
}

This style

for (i = 0; total<=0; i++)
{
foo..
}

Is nasty and makes it a pig to read.
 
Associate
Joined
14 May 2010
Posts
1,136
Location
Somerset
Put the brace after the for declaration:

for (i = 0; total<=0; i++) {
foo..
}

This style

for (i = 0; total<=0; i++)
{
foo..
}

Is nasty and makes it a pig to read.

Personally, I prefer the second method ;) I find it easier to identify where a block of code begins and ends with braces on seperate lines.

It's personal preference at the end of the day. As long as you are consistent thoughout the project then it shouldn't matter which style you use.
 
Soldato
Joined
13 Feb 2003
Posts
6,157
Personally, I prefer the second method ;) I find it easier to identify where a block of code begins and ends with braces on seperate lines.

It's personal preference at the end of the day. As long as you are consistent thoughout the project then it shouldn't matter which style you use.

Agree on both counts.
 
Soldato
Joined
13 Feb 2003
Posts
6,157
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

oh jesus, you even have gotos. :mad::mad::(:(

Things to fix:
unnecessary globals
gotos
using unintialised variables
using raw arrays
large methods (inp)
 
Last edited:
Soldato
Joined
19 Dec 2009
Posts
2,669
Location
Lancashire
In addition to the other things mentioned, read up on loops. You can use them instead of goto and labels, as you have here:

Code:
start:
cout << "Please input your first number and press enter" << endl; //Prints a request for the input, and then starts a new line
start2:
cout << "Input: "; cin >> input; //Takes the input
int num1 = inp(input, num, 0);
if (num1 == -1) {goto start;}

All of this could be contained within a small loop, which would be a much cleaner solution all-round and improve readability of your whole program. As well as that, your style is somewhat inconsistent when it comes to control flow structures that only execute a single statement. You have all of these throughout your program:

Code:
for (i = 0; i <= 99; i++)
{
	tot = tot + num[i];
}

Code:
if (num1 == -1) {goto start;}

Code:
for(i = 1; i <=20, i++)
{cout << out[i];}

You really need to pick a single style and stick with it. The first one is fine, but the second and third generally cause people frustration. It's common practice to either go with the first one (or the K&R-style alternative with the opening brace on the same line as the condition) or do the following, which is syntactically legal in C and C++ and generally accepted:

Code:
for (i = 1; i <= 20, i++)
	cout << out[i];

Be aware, however, that this is only applicable for a single statement. If you need to execute more than one statement when the condition is met, then you must include braces. Most importantly, remember to be consistent throughout your whole program (and if you're contributing to someone else's program, make sure to follow the style that they use to maintain consistency).
 
Caporegime
Joined
18 Oct 2002
Posts
32,623
Put the brace after the for declaration:

for (i = 0; total<=0; i++) {
foo..
}

This style

for (i = 0; total<=0; i++)
{
foo..
}

Is nasty and makes it a pig to read.

Not sure what you mean but e second style is easier and cleaner, and more standard. Opening and closes braces are at the same indention level and soar every easy to pair up.
 
Back
Top Bottom