help reading from a string

i have a function that is getting passed a string that will have an integer followed by a plus sign followed by an integer, how do i get the program to read what the integers are.
Is it an actual C++ string or a pointer to an array of char?
it is an actual string
the code in question is
1
2
3
4
5
int computational ( string i )
{
	//will handle the addition
	return 0;
}


this passes the string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int reader ()
{
	string input;
	while (true)
	{
		cin >> input;
		if (input == "help"){
			help ();
		}
		else if (input == "stop") {
			return 0;
		}
		else {
			computational (input);
		}
	}
	return 0;
}
Last edited on
is the string as string str = "123 + 1234" ;
Read each char in turn from the string using

char someChar = theString[i];

If the char is '+'. discard it. Otherwise, turn it into an int

int someInt = somechar - '0';

You will then have a set of individual integers you can use to create the complete integers; for example, if you have three integers, you know that one of them is hundreds, one is tens, and one is units.
Last edited on
Suppose
1
2
3
4
5
6
7
8
9
10
string str = "123+456 ";
string str1 , str2 ; 
 int  position = str.find('+');
str1 = str.substr( 0 , position ) ; 
str2 = strsubstr( position+ 1); 
istringstream ivalue ( str1 , istringstream::in);
int val1 = 0  , val2 = 0; 
str1 >> val1; 
ivalue.str( str2);
str2>>val2;
i am trying your code blue coder but i got error messages on ivalue and the extraction between str1 and val1 and str2 and val2
1
2
3
4
5
6
7
8
9
10
string str = "123+456 ";
string str1 , str2 ; 
 int  position = str.find('+');
str1 = str.substr( 0 , position ) ; 
str2 = strsubstr( position+ 1); 
istringstream ivalue ( str1 , istringstream::in);
int val1 = 0  , val2 = 0; 
ivalue >> val1; 
ivalue.str( str2);
ivalue>>val2;
Topic archived. No new replies allowed.