String Termination Using The getline Function

HI, I came across a strange problem while writing a program using the getline function. I understand that when using the getline function an input string will terminate when the "enter" key is pressed and in the following program it works as one would suspect.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
using namespace std;

int main() {
	
	string str;
	cout << "Enter: ";
	getline(cin, str);
	
	cout << str;
}


However when I use it in this next program (below) that I have been working on it will only terminate after pressing the "enter" key if the first character is a number, otherwise it will not terminate. So the question is: How do I get it to terminate the string regardless of the input order?
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
38
39
40
41
42
43
#include <iostream>
#include <string>
using namespace std;

int i = 0, j;
int numValue;

int PowerFunc(int x, int y);

int main() {
	
	string expression;
	cout << "Enter an expression: ";
	getline(cin, expression);
		
	while (expression.at(i) < 49 || expression.at(i) > 56) {
		i += i;
	}
		if (0 <= (expression.at(i)-48) <= 9) {
			
			for (j = 47; j < expression.at(i); j++) {
				
					numValue = (j - 47);
			}
			PowerFunc(numValue, (i + 1));
		}

	cout << numValue << endl;
	
	return 0;
}

int PowerFunc(int x, int y) {
	
	int z = 10, s;
	
	for (int k = 1; k <= y; ++k) {
		z *= z;
		s = z/10; 
		x *= s;
	}
	return x;
}


Additional Info: the purpose of this program is to change a character, which is extracted from a string, into an equivalent numerical value, if the character is an integer, and assign it to an int variable. I plan on eventually adapting it to return the correct value of a multi-character integer such as 123.
1
2
3
int i = 0
//...
i += i;
0 + 0 = 0. You never change your i, so it will never check another letter.
wow I cant believe i didn't catch that thanks. i is a counter variable that needs to start at zero it should have been i += 1.
Topic archived. No new replies allowed.