### Basic Calculator program help!!

Hi guys.

I am new to C++ and am trying to learn the language.
I have had a go at designing and creating a basic calculator program however a bit stuck.
My program kind of works but when the user hits the = sign the calculation is always 0 for some reason.

The programs purpose is to perform a simple calculation ie. (5)*(5)=25.

note the brackets for each number.

also I can't seem to figure out how to add a store previous value operation and make the calculator loop itself after each calculation.

i cant understand where i am going wrong. maybe a second pair of eyes could help. lol

any help would be much appreciated.

here is my code so far:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130`` ``````// manishCalculator.cpp : Defines the entry point for the console application. #include "stdafx.h" #include #include #include #include using namespace std; const int MAX_LINE_LENGTH = 100; const int MAX_NUMBERS = 10; const int MAX_OPERATORS = 10; const float PI = 3.1415; int numIndex = 0; // index into numbers array int opIndex = 0; // index into operators array float processCalculation(float a, float b, char op){ float result = 0; switch(op){ case '-': result = a - b; break; case '+': result = a + b; break; case '/': result = a / b; break; case '*': result = a * b; break; case '^': result = a; for(int c = 1; c
`(input[x] == "pi" || "Pi" || "PI")` will always evaluate to true.
You want `(input[x] == "pi" || input[x] == "Pi" || input[x] == "PI")` here.
Also it looks like `(5)` string will be passed to input array as "(5)" and not as "(", "5", ")" which probably what you want. That means `if(input[x] == "(" )` will never be true

remember at compile-time the function "processEquation" has no idea about the size of the array that is going to be passed to it (well it could check what Max-Array-size "input" has but it doesn't ;) (wouldn't even really be benefiting us))
`sizeof(input)`
will probably always evaluate to 4, no matter what you really put into input

Edit: In fancy words: "sizeof" is a "compile-time" operation.
Last edited on
Oh, yes, but even if it did know about size of array it still won't work, sizeof return memory size in bytes, not amount of elements in container.
Ya of course it doesn't make sense to use sizeof there at all. But that should be something he has to figure out himself! ;)
Just giving him a little hint, there is something not quite right :P

Btw. @manish1991: The key to be good at those kind of "algorithms" is to bang your head as long as it takes against a wall until it finally works. ^.^
The learning-process how to build the algo and fix all those little misstakes it the actual stuff to learn here.
Believe me once it works. You are happy for a day and then forget about it and go on to something cooler with the help of the experience solving the previous problem =)

Something general I'd like to drop here: Whyy always make a big mix between C and C++ stuff especially strings ^.^
Why reading it in an inconvenient char-Array and not directly into a great easy-to-use and dynamic string ;x
You can check afterwards if it's a valid term (it just feels right instead of typing in every character itself ;>)
Last edited on
First, your code is a good effort for a beginner :+) , so well done!

It is always `int main()` not `void main()`

Prefer to declare functions before main, then define them after main.

Use `double` rather than `float`. `float` has only 7 or 8 significant figures of precision - this is easily exceeded. `double` has 15 or 16.

You had a `const` value for PI , but didn't use it on line 40.

Every beginner seems to forget to check for divide by zero. Be careful with that - check if the absolute value of the number is less than some arbitrary precision 0.0001 say.

Always put a default case in a switch - use it to catch bad input or error conditions.

I am not a fan of multiple statements on one line.

Always initialise variables - this is one of the biggest source of errors. Do this at declaration, one per line plus a comment, especially if it is only 1 char long - say what it means and it's use and the expected valid range or values. Try to use meaningful names wherever you can.

There is an `*=` operator for multiplication. There are a lot of these assignment operators - look them up in the tutorial.

You don't need line 8 if you are going to qualify std namespace things as in `std::cout`. Line 8 is bad practice, qualifying things with `std::` is good practice.

Hope this helps and good luck - look forward to seeing your revised code.

Topic archived. No new replies allowed.