DEV Community

Ted Larsson
Ted Larsson

Posted on

need help with c++ calculator

So I am taking a c++ course and I am on my third assignment. The first was a simple calculator, get 2 numbers and then Display the results for added, subtracted, divided and multiplied. The second one was a simple game that measured the time it took to press a key after the game said go. Now they've turned it up several notches and I'm doing a full blown calculator without the GUI, and I'm stuck. I don't even know where to start. The user must be able to add, subtract, multiply and divide as many times as he/she/it wants then get the result of be able to clear and start over. Now as far as pseudo code goes I've gotten so far as : Get user input number 1, operator, number 2, sum, put sum into vector, show sum and get next input, sum, put into vector loop this until user either decides to clear or wants the sum. An I totally wrong here? Now I don't want all the code written for me, I mean I want to learn this I just want a nudge in the right way of thinking 😊

Top comments (5)

Collapse
 
larssonted profile image
Ted Larsson • Edited

so i had an idea how to create this using while and if loops and switch and so and started working on that, got that working and was pretty happy with the result. then i reached out to the techer and asked if i was on the right track. well i was not, i need to make this calculator using string. they should be able to write the formula and then get the calculated answer. ex: 10+10-5(7*8).

now i havent used string and vectors in the other assignmenst and iv'e spent countless hours reading and watching youtube lessons on string. the problem is most of the tutorials i find arent advanced enough, they are only using at most 2 numbers and 1 operator and no (). Now this is programming 1, a introduction course, and this seems to be way more advanced than i thought so i think i need more than a little help here :P does anybody know where i can find a tutorial or something that explains this so that a beginner understands this? i want to learn this really badly and it feels like i almost do i just need something that makes my brain go aha thats how it works!

Collapse
 
larssonted profile image
Ted Larsson • Edited

Thanks for your inputs! I tried sitting down and making a flowchart. I kind of see the things I need to do. Now I'm just gonna figure out how to do them, and I don't know if I over complicate the project.

thepracticaldev.s3.amazonaws.com/i...

Collapse
 
codemouse92 profile image
Jason C. McDonald • Edited

It sounds like you're making the jump from having all the steps happen the same way in order, to working with functions.

Get out a literal piece of paper and figure out the following:

  • What information do you need to store as the program runs?

  • Break the problem down into pieces. What do you need to be able to do? e.g. you should be able to add, subtract, etc. Each piece should probably be a separate function.

  • What information do you need from the user? How do you want to ask them for it? How will you store it?

You'll do well to review and make use of the following programming structures:

  • Loops

  • Conditional statements

  • Functions

Collapse
 
lsponzoni profile image
Leon

Complementing Jason, you start here:
"The user must be able to add, subtract, multiply and divide as many times as he/she/it wants then get the result of be able to clear and start over."

Verbs are hints to subtasks: add, sub, mul, div, clear, get the result.
(Nouns are hints to variables and later on objects).: result

You thought a nice loop.
User goes: number, op, number, op, number,..., clear or result.

Or maybe: number, op, number, number, op, number,..., clear or result.
Should you reuse the result as operand for the next op?
Should the user be able to ask for "a sub formula"?
How do you track if user wants to use result as operand?
For example i_1 + i_2 - (i_3 + i_4)
Can the user go: "i_1 + clear" instead? What happens then?

A nice way to think and reason is a flow chart
Make a square for each action or calculation, put a arrow without anything when one thing leads to another.
Write an arrow for each different outcome of the box.
For example:
commons.wikimedia.org/wiki/File:Wr...

Hint: For now, you can ask for a number input and give options 1,2,... to interact with the user. Later, you can use c++ string, char and regex classes.
Try the simplest thing first, and then think about how you can make it better/closer to the expected.
You can always evolve from the simple later.

Best wishes.

Collapse
 
cairocafe profile image
bleskop stevens

Look up the Shunting Yard algorithm. At some point you're going to need it.