I am working with Bjarne Stroustrup book for beginners and I've encountered some trouble with this exercise: "Modify the “mini calculator” from exercise 5 to accept (just) single-digit numbers written as either digits or spelled out". Firstly, only digits didnt work, so I cut out the part that converts strings in order to try if it doesn't interefere in some way. It indeed started working, but when I pasted that part of the code back it turned out digits still work on the contrary to spelled words now... Does anybody see why that happens?
string a, b; char op; int val1=0, val2=0;
cout << "Enter a calculation:\n";
vector<string> numbers = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
cin >> a >> op >> b;
for(int i=0; i<numbers.size(); i++)
{
if(a==numbers[i])
{
val1 = i;
}
if(a!=numbers[i])
{
stringstream ss;
ss << a;
ss >> val1;
}
}
for(int j=0; j<numbers.size(); j++)
{
if(b==numbers[j])
{
val2 = j;
}
if(b!=numbers[j])
{
stringstream ss;
ss << b;
ss >> val2;
}
}
if(op=='+')
{
cout << "The sum of " << a << " and " << b << " equals " << val1+val2 << '\n';
}