7 Replies - 10869 Views - Last Post: 08 May 2010 - 05:33 PM Rate Topic: -----

#1 college_trained  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 10-January 10

convert string to int array

Posted 06 May 2010 - 06:45 PM

Does anyone have any links where I can go to find out how to convert a string to an int array?

I thought I could assign the substring function of the string to the int array in a for loop but it seems not to be working.


Thanks for all the help in advance!
Is This A Good Question/Topic? 0
  • +

Replies To: convert string to int array

#2 Guest_Oler1s*


Reputation:

Re: convert string to int array

Posted 06 May 2010 - 06:46 PM

What have you Googled for? What have you found? How was it not suitable to answer your question?
Was This Post Helpful? 0

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: convert string to int array

Posted 06 May 2010 - 06:50 PM

post what you have tried
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3102
  • View blog
  • Posts: 19,142
  • Joined: 14-September 07

Re: convert string to int array

Posted 06 May 2010 - 06:56 PM

C++ string? What type of numbers? Single digits?

#include <iostream>
#include <string>
using namespace std;

int main(){
	const int SIZE = 10;
	string test = "1234567890";
	int buffer[SIZE];

	for(int i = 0; i < SIZE; i++){
		buffer[i] = test[i]-48;
	}

	for(int i = 0; i < SIZE; i++){
		cout << buffer[i] << endl;
	}
	return 0;
}

Was This Post Helpful? 1
  • +
  • -

#5 college_trained  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 10-January 10

Re: convert string to int array

Posted 06 May 2010 - 07:40 PM

View PostKYA, on 06 May 2010 - 07:56 PM, said:

C++ string? What type of numbers? Single digits?

#include <iostream>
#include <string>
using namespace std;

int main(){
	const int SIZE = 10;
	string test = "1234567890";
	int buffer[SIZE];

	for(int i = 0; i < SIZE; i++){
		buffer[i] = test[i]-48;
	}

	for(int i = 0; i < SIZE; i++){
		cout << buffer[i] << endl;
	}
	return 0;
}


KYA I do appreciate this code and I understand most of it except for the "...-48" part. Why subtract 48?

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3102
  • View blog
  • Posts: 19,142
  • Joined: 14-September 07

Re: convert string to int array

Posted 06 May 2010 - 07:42 PM

chars have an ASCII value. Assuming single digits, all we need to do is subtract 48 from the ASCII value of the char when casted into an integer (the cast is done implicitly).
Was This Post Helpful? 1
  • +
  • -

#7 college_trained  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 10-January 10

Re: convert string to int array

Posted 08 May 2010 - 04:06 PM

View PostKYA, on 06 May 2010 - 08:42 PM, said:

chars have an ASCII value. Assuming single digits, all we need to do is subtract 48 from the ASCII value of the char when casted into an integer (the cast is done implicitly).


Thanks for all the help KYA. I decided it would be easier to use a vector instead of an int array; it's much easier but thank you for the help.
I've been trying to create a program that converts between the 4 main number systems and I needed to convert the string that took in the input from the user to an int so that I could do numerical manipulation.
Now I have to work out this issue VS is having with the pow function. haha
Little by little it gets done.

Thanks again all.
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: convert string to int array

Posted 08 May 2010 - 05:33 PM

Quote

issue VS is having with the pow function. haha


is it yelling at you because your not passing in doubles?
you can just cast you integer input to double
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1