@kinjal2209
This is the stringstream version of fromString(), with comments, without templating. All the templates do is allow you to use the same routine for other types T (like float, double, etc.)
For stringstream, see
http://www.cplusplus.com/reference/sstream/stringstream/
and look at the constructor or member functions for examples.
Using a stringstream as a repository - getting the data from a string, sending it back as an integer - is probably the easiest and safest way for you. It acts rather like a disk file, but instead is an area of internal computer memory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int fromString( string s ) // takes a string s and returns an integer equivalent
{
int value; // this will hold the final integer value
stringstream ss( s ); // create a stringstream from s ... this is like writing s to a "file" or cout,
// but instead it is in computer memory
ss >> value; // now read back from the stringstream, but this time into the integer value
return value; // return this integer from the program
}
int main() // just to test it
{
string s = "13045";
int i = fromString( s ); cout << i << endl;
}
|
My second example worked a bit like yours, in that it picked off individual characters one by one.
Note that a char has an equivalent integer representation, and they are in alphanumeric order. Thus, successive characters (NOT digits) '0', '1', '2', ... will have successive equivalent integers (although these numbers may not be the same on all computers) and when you operations + and - on them you will be operating on those integers. Thus, for example,
'3' - '1' produces the integer 2, irrespective of the actual number attached to char '1'. Then
s[i] - '0' will produce the integer digit associated with s[i] - which is what is needed.
Numbers are then built up as follows. Note that we read strings from left to right.
Suppose the string is "12345".
result -> 1
Then multiply by 10 and add the next digit:
result -> 10 + 2 = 12
Then multiply by 10 and add the next digit:
result -> 120 + 3 = 123
Then multiply by 10 and add the next digit:
result -> 1230 + 4 = 1234
Then multiply by 10 and add the next digit:
result -> 12340 + 5 = 12345
This is a common way of building up numbers like this incrementally.
In your own code you use pow(10,i) because you think that this will work the same way as 10^(i) EXACTLY. One problem with this (apart from inefficiency, given the way pow() computes) is that the answer is a double and, unlike an int, this is stored only to a certain accuracy. Try to assign it to an int and you round TOWARD ZERO (not the nearest integer). Thus, for example, you might get 99999.9 which actually rounds down to 99999 and this is clearly not what you want.
The other major problems with your code is that you are using C-style headers and C-strings. The latter has its uses, but a string is more convenient here (and, probably, in most other places).
Hope this rambling explanation helps.