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.
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?
#include <iostream>
#include <string>
usingnamespace 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.