#include <iostream>
#include <cctype> // isdigit
using namespace std;
// Global buffer
const int LINE_LENGTH = 128;
char line[LINE_LENGTH];
int lineIndex;
void getLine () {
// Get a line of characters.
// Install a newline character as sentinel.
cin.getline (line, LINE_LENGTH);
line[cin.gcount () - 1] = '\n';
lineIndex = 0;
}
enum State {eI, eF, eM, eSTOP};
void parseNum (bool& v, int& n) {
int sign;
State state;
char nextChar;
v = true;
state = eI;
do {
nextChar = line[lineIndex++];
switch (state) {
case eI:
if (nextChar == '+') {
sign = +1;
state = eF;
}
else if (nextChar == '-') {
sign = -1;
state = eF;
}
else if (isdigit (nextChar)) {
sign = +1;
n = nextChar - '0'; // line 41
state = eM;
}
else {
v = false;
}
break;
case eF:
if (isdigit (nextChar)) {
n = nextChar - '0';
state = eM;
}
else {
v = false;
}
break;
case eM:
if (isdigit (nextChar)) {
n = 10 * n + nextChar - '0';
}
else if (nextChar == '\n') {
n = sign * n;
state = eSTOP;
}
else {
v = false;
}
break;
}
}
while ((state != eSTOP) && v);
}
int main () {
bool valid;
int num;
cout << "Enter number: ";
getLine();
parseNum (valid, num);
if (valid) {
cout << "Number = " << num << endl;
}
else {
cout << "Invalid entry." << endl;
}
return 0;
}
What does the '0' in line 41 mean? Does this line assign n the next character minus the first character of nextChar?