Problem 14 Modular Calculator

Back to Problem Solutions forum

SlapstickMojo     2017-08-31 18:31:47

So I get that I need to calculate the modulo as I go so the number doesn't end up huge. My problem is how I'm supposed to get the modulo before doing the calculations since it's at the end of the input list. I could read in values until I get to the % symbol and then read in the modulo, but then I've gone through all the other numbers and can't get them back. And I can't easily store the other values in an array since I don't know how many entries there will be.

What's the right direction to go? I assume I'm not supposed to hard code the modulo number, nor am I supposed to count the number of inputs and hard code that. How do I get the value at the end of the inputs, then go back and use it on all the inputs that came before it? Is it a trick of the iostream, or arrays, or what?

Quandray     2017-08-31 19:50:00
User avatar

Hi Jason,

Read the data and store it "somewhere" until you get to the modulo, then go back through the stored data doing the calculation.

Ideally, you'd store the data in a "container", maybe one container for the operator and another for the number. The advantage of using containers, is that you don't need to know up front, how many things you are going to put in them.

If you don't know what I mean by "container", arrays with 100 elements should be big enough.

Please login and solve 5 problems to be able to post at forum