-1

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';
}
Aurum0114
  • 1
  • 1
  • 3
    FYI: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Scheff's Cat Aug 05 '21 at 06:33
  • 1
    @Scheff'sCat: You need to consider that newbies will most certainly not know what a debugger is and not at all know how to operate it. I see many such comments here on SO. And I think they are strange. Not everybody is born with 30 years of C++ experience . . . – A M Aug 05 '21 at 06:39
  • 2
    @ArminMontigny Yeah, but the linked article explains _What is a debugger and how can it help me diagnose problems?_ (Not that I believe I was born with 30 years of C++ experience... -- Actually, I'm still working on this.) ;-) – Scheff's Cat Aug 05 '21 at 06:40
  • the stringstream part only needs to be executed when `a` is not in `numbers`. same for `b`. Any yes, I recommend you to learn how to use a debugger too. It's extremely useful for bugs like this one. – bb1950328 Aug 05 '21 at 07:04
  • 1
    please show a [mre]. What is the input to this program? What is the output? What is the expected output? – Alan Birtles Aug 05 '21 at 07:25

1 Answers1

1

The issue is that the code does not stop looking for a matching entry in numbers after it has found a matching entry.

With this, if a number has been entered as a string, the second if block (the one that tries to convert the string into a number using the sstream) is always executed in the next iteration after a matching entry has been found and so val1 is set back to zero.

Adding a break statement after val1 = i; and val2 = j; to exit the for loop would solve that issue.

Fomas
  • 114
  • 4