Concretamente como funciona en algunos puntos, su función principal la entiendo, pero hay cosas que no. La función:
int get_number()
{
const int not_a_symbol = numbers.size(); // not_a_symbol is a value that does not correspond
// to a string in the numbers vector
int val = not_a_symbol;
if (cin >> val) return val; // try to read an integer composed of digits
cin.clear(); // clear string after failed attempt to read an integer
string s;
cin >> s;
for (int i = 0; i<numbers.size(); ++i) // see if the string is in numbers
if (numbers[i] == s) val = i;
if (val == not_a_symbol) error("unexpected number string: ", s);
return val;
}
¿Por que al principio tiene que realizar lo de asignarle a la variable constante not_a_symbol el valor del tamaño del vector? Dice que es un valor que no corresponde a una cadena en el vector de numeros, que por cierto, son numeros escritos en forma de cadena, "uno", "dos", ... , hasta "diez".
Entiendo que luego comprueba si lo introducido es true, o sea, es un entero, devuelve lo introducido, ¿no?
if (cin >> val) return val;
Limpia el cin. Crea una cadena llamada s, y pide al usuario introducir un valor, luego comprueba recorriendo el vector de cadenas si el valor introducido corresponde con algun valor cadena almacenado. Si es asi, coge la posicion, que corresponde al valor entero del valor cadena almacenado y lo asigna a val, al final devuelve val, como valor entero.
Todo bien, la funcion se entiende, pero, en esa sencencia donde hace un error handle de esos, ¿como funciona? Vuelve a entrar en juego el not_a_symbol, que no entiendo. ¿Es como una referencia de lo que es un entero y lo que no lo es?
Edito y añado el código completo, por si fuera necesario. Es una sencilla calculadora que solo admite operaciones entre numeros de un digito, pero estos pueden ser tanto int como strings.
#include "std_lib_facilities.h"
vector<string> numbers; // representation of numbers as strings
// numbers[i] is the string representation for i
// for numbers[0] to numbers[numbers.size()-1]
void initialize_numbers()
{
numbers.push_back("zero");
numbers.push_back("one");
numbers.push_back("two");
numbers.push_back("three");
numbers.push_back("four");
numbers.push_back("five");
numbers.push_back("six");
numbers.push_back("seven");
numbers.push_back("eight");
numbers.push_back("nine");
numbers.push_back("ten"); // why not? :-)
}
int get_number()
{
const int not_a_symbol = numbers.size(); // not_a_symbol is a value that does not correspond
// to a string in the numbers vector
int val = not_a_symbol;
if (cin >> val) return val; // try to read an integer composed of digits
cin.clear(); // clear string after failed attempt to read an integer
string s;
cin >> s;
for (int i = 0; i<numbers.size(); ++i) // see if the string is in numbers
if (numbers[i] == s) val = i;
if (val == not_a_symbol) error("unexpected number string: ", s);
return val;
}
int main()
try
{
initialize_numbers();
cout << "please enter two floating-point values separated by an operator\n The operator can be + - * / % : ";
while (true) { // "forever"; that is until we give an unacceptable input or make a computations error
int val1 = get_number();
char op = 0;
cin >> op; // get the operator
int val2 = get_number();
string oper; // text appropriate for an operator
double result;
switch (op) {
case '+':
oper = "sum of ";
result = val1 + val2;
break;
case '-':
oper = "difference between ";
result = val1 - val2;
break;
case '*':
oper = "product of ";
result = val1 * val2;
break;
case '/':
oper = "ratio of ";
if (val2 == 0) error("trying to divide by zero");
result = val1 / val2;
break;
case '%':
oper = "remainder of ";
if (val2 == 0) error("trying to divide by zero (%)");
result = val1 % val2;
break;
default:
error("bad operator");
}
cout << oper << val1 << " and " << val2 << " is " << result << '\n';
cout << "Try again: ";
}
}
catch (runtime_error e) { // this code is to produce error messages; it will be described in Chapter 5
cout << e.what() << '\n';
keep_window_open("~"); // For some Windows(tm) setups
}
catch (...) { // this code is to produce error messages; it will be described in Chapter 5
cout << "exiting\n";
keep_window_open("~"); // For some Windows(tm) setups
}
numbers
? ¿Dónde está declarado o definido? ¿Qué contiene? En tu código no aparece más que para ser usado