How will Calculate the string


char str[20]="15+20+18-10"

here how will convert the string as integer and negelate the Symbols. And calculate these numbers.
Please help me.

Output : 43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <sstream>

using namespace std;


int main()
{
	istringstream strin("15+20+18-10");

	int result = 0;
	strin >> result;
	
	char sign; int next;
	while(strin >> sign >> next)
		result += next * (sign == '-' ? (-1) : 1);
	cout << result;

	return 0;
}
Topic archived. No new replies allowed.