Hey guys. I have the following string array, say,

string array[2];
array[0] = "hello7925";
array[1] = "bye344539";

I want to read the last two characters from each string in the array, which in this case are 25 and 39 and then put them in a separate array like this..

int array_integers[2];
array_integers[0] = 25;
array_integers[1] = 39;
the end of every string will have a integer thats why the second array is of type int.
I am kind of stuck on this part. Any suggestions would be greatly appreciated. Thanks in advance.

Recommended Answers

All 7 Replies

Isolate (using whatever method you like) the last two char of each string as a substring and then convert the substring to an int (using whichever method you prefer). There are at least two ways to do each the isolation and the conversion.

well thats the entire thing....I cant seem to figure that out....is there a STL method to do that?

you should be able to use the substr() method to isolate the last two char of the string and you should be able to use a stringstream to convert the string isolated by substr() to an int. Both substr() and stringstream are standard C++ items.

Some information on both is available at cppreference.com, but having your own reliable hard copy reference book would be strongly recommended, too.

hey thanks. Substring worked quite well. I have another question. rather silly to ask but is it possible to print a integer as a string?
like int val = 1;
if we cout<<val its gonna put out 1...but is there a way that it represents 1 yet prints out it self? so cout<<val should print val insted of 1 lol..

There is no routine way to print a variables name. You could create a user defined type and include the name of the type as one of the data members. However, the name of the variable itself, not that I can think of.

It is possible to use numerical values as either numerical variables or string variables and it is possible to convert the value of numerical variables into strings or strings representing numerical values into numerical values.

int i = 1;
string strI = "1";
cout << i << " or " << strI;

will print out: 1 or 1. You can't tell which 1 is an int and which 1 is a string to look at them on the screen or in a file, etc.

yea...thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.