Welcome, Guest. Please login or register.

Author Topic: C++ - convert string to array of ints  (Read 3882 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
C++ - convert string to array of ints
« on: September 01, 2007, 12:01:06 PM »
I need to convert a string, which is guaranteed to contain only numbers, to an array of ints. Or at least convert each digit from the string to an int so I can add it to an array. Here's what I was thinking of doing:

str="12345";
int nums = str.length();
for ( int i=0; i{
    n = atoi( str );
}


The effect should be that the for loops runs once for each digit in str (in this case five times) and sets n to the numerical value of str. However, this gives me an error "invalid conversion from `char' to `const char*' ". Is there any way to do this?

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
Re: C++ - convert string to array of ints
« Reply #1 on: September 01, 2007, 12:21:52 PM »
Thanks Karlos! I had actually already tried "int num=scorestr-'0';" but I missed the quotes around the 0 which is subtracted from the character. I thought you were subtracting zero to make it think it was an integer (because it was being used like one), when further reading reveals that you are subtracting the ASCII code '0' to get the value of the digit.


Here is the solution, where "scorestr" is a string containing the player's score:

CODE
for ( int i=0; i{
    int num=scorestr-'0';
    apply_surface( scorestartpos+(i*12), 10, font, screen, &fontClips[ num ] );
}

Thanks again

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
Re: C++ - convert string to array of ints
« Reply #2 on: September 01, 2007, 12:44:22 PM »
Would that be significantly more efficient than converting it to a string, extracting each character and then converting the character back to an int?

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
Re: C++ - convert string to array of ints
« Reply #3 on: September 01, 2007, 12:58:12 PM »
The target platform is the GP2X, so the code needs to be as efficient as possible. The GP2X doesn't handle floating point maths well (apparently) so a solution which avoids this might end up more efficient.

In any case, the routine to convert the integer score to a string and then to an array of ints only needs to be run when the score changes. Then all the main loop has to do is iterate through the array of ints and draw the corresponding numbers on the screen.

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
Re: C++ - convert string to array of ints
« Reply #4 on: September 01, 2007, 03:23:51 PM »
Quote
Karlos wrote:
Damn you Mark, you're making me think of C/C++ and distracting me from so called "Web 2.0" development...

You know it's got bad when C++ is a break from what you were doing before :lol:

Quote
Karlos wrote:
Pretty soon I'll be in danger of booting up a miggy.

God forbid! It's been a while since I did that too :-( Mind you, my new game (a port of GridWars to the GP2X) is written in C++/SDL, so I suppose I could port it to the Amiga.....

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
Re: C++ - convert string to array of ints
« Reply #5 on: September 01, 2007, 05:27:16 PM »
Quote
Karlos wrote:
Financial considerations have crushed all forms of creativity outside the defined constraints of whatever it is I'm working on for someone else :-(

You know I understand that after my thread on [not] making music. I was hoping to use my current sabbatical to write some music, but I need the new version of Reason to finish my new track. I don't want to move on to something else until I finish it in case I forget it :-) Hopefully I'll still have time to write it when it has been released - by which time I will have started my degree!

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10