I'm new to C++ and, as many others here, I'm trying to learn it from Bjarne Stroustrup's Programming -- Principles and Practice Using C++.
I'm stuck on Exercise 7, Chap.4., where the idea is to write a calculator that, when the input is either an integer and/or a string followed by a character (+,-,* or /), the output should announce "the sum/diff/prod/ratio of" input 1 and input 2 is the result; so if ("two" 3 *) is the input, the output should be "the product of 2 * 3 = 6".
Here's Stroustrup's solution (I'm leaving Stroustrup's comments):
-- There's no copyright infringement, as this is all from his website --
/*The solution uses two functions (in addition to main():
initialize_numbers() to initialize the vector of number string
representations
get_number() to read a number that is either a string or a sequence of
digits
*/
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: ";
}
}
More specifically, my problem is with the following part:
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
etc etc etc... }
I just don't understand what's going on here, in the big context. I'm having trouble understanding this whole get_number() function, and how it relates to the rest of the code.
1 - Why assign the value of number.size() to not_a_symbol? What does this accomplish?
2 - if (cin >> val) - why is that the conditional? val is == size of vector numbers, which is 11, so is the conditional the number 11? How is that helpful? And what is it returning? Itself?
3 - // try to read an integer composed of digits - how is that accomplished, and why is that helpful?
Thank you, and sorry for the long format of the question.