Int from char array

How to extract integer value from a character array in simple way .
Example : My age is 20
Output : 20
http://www.cplusplus.com/reference/cstdlib/atoi/?kw=atoi
or just find the number element the '2' and '0' has in the array.
In this case it would be 11 and 12 in the array.
(if this is how you want to solve the problem)
Last edited on
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
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>   
#include <string>   
#include <sstream>
using namespace std;

int main ()
{
	string str = "my age is 20 and I am 200testing. Is this 2015 already?";
	string::size_type sz;
	stringstream ss;

	ss<<str;
	string temp;
	int integer;

	while(ss>>temp)
	{
		bool allInt = true; //all int
		//check wether the string is entirley numbers
		for(int i = 0; i < temp.size(); i++)
		{
			if(!isdigit(temp.at(i)))
			{
				allInt = false;
				break;
			}
		}
		if(allInt)  //if so..
		{
			integer = stoi(temp,&sz);
			cout<<integer<<endl;
		}
	}

	system("pause");
	return 0;
}
#shadow code what if the inputus given by the user not heartcoded
Last edited on
Is there any difference?
Assume that the use inputs the same string in the example above.
Topic archived. No new replies allowed.