Simple Calculator Program

Hello!
I want to make a simple calculator program can work with float type numbers and arithmetic operators. I don't want the user to enter each term and operator again and again but to enter the whole expression in turn. Such as in the following Example...

if the user enters:
2.5 + 3.5 + 56.0 - 12.23 / 9 * 8

The results should yields:
44.24

It should not follow BODMAS Rule...
I use Dev-Cpp as a part of my programming

Note for you:
I am just a beginner and is not as good as you. I have learned a bit of array, function, string, structure(very very basic), matrix and loops as the part of class 11 syllabus(INDIA - CBSE BOARD)...Class and other derived data types are completely unknown to me. printf and all looks scary to me...I use cout<< and cin>>

Please help in as simple code as possible

A quick help will be highly acknowledged and respected...
Regards and thank you in anticipation...
Last edited on
I want to make a simple calculator program ...


So make it, then come back and ask for help when you run into problems. If you're having problems of where to even get started ask that, don't just ask people to do it for you. Put forth some effort and people will be much more willing to help out.
sorry for that but I cannot start off with it...at first, I thought for using an array...but then I am facing with separating the elements and perform arithmetic calculations...as I cannot convert a char array to int or float...
plz understan...and help...
regards...
kinjal
Your program needs to be able to distinguish between numbers, such as
2.5   3.5   56.0   12.23   9   8
and operators such as
+    -    /    * 


One way to do this is to read the input one character at a time
 
char ch = std::cin.get();

and then test it using either a series of if/else statements, or using a switch-case.

Follow a set of rules:
ch is a newline '\n'
That's the end of the input. Stop processing.

ch is a space or tab '\t'
ignore it and proceed

ch is one of '+', '-', '/', '*'
Store the operator in a character variable.
 
    op = ch;


ch is a digit from '0' to '9'
Put the character back into the stream, then read the number.
1
2
    std::cin.putback(ch);
    std::cin >> number;

and after reading the number, decide what to do with it.
If there is not any operator already stored, store the number in the total.
 
    total = number;
if there is an operator already stored, carry out the operation with the stored total and the current number, and save the result. example, operator is '+'
 
    total += number;



After end of input, print out the total.
as I cannot convert a char array to int or float...

http://www.cplusplus.com/reference/cstdlib/atof/
http://www.cplusplus.com/reference/cstdlib/atoi/

Sometimes the more advanced function strtod() can be useful. It is possibly more trouble than it's worth if you're a beginner. An advantage is that it not only converts the string to a number, it also returns a pointer to the next character in the array after the number.
http://www.cplusplus.com/reference/cstdlib/strtod/
can someone please explain me how to use and what actually is

char ch = std::cin.get();
and...
std::cin.putback(ch);

and how to take input more than one digit number and decimal numbers if we input one char at a time???

Thanx Chervil...but I cannot actually use the above thing...but I am trying...
Regards

You need to try some simple examples in a program.

1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
    std:: cout << "Please type a character: ";
    char ch = std::cin.get();
    std::cout << "you typed [" << ch << "] or ASCII code " << int(ch) << '\n';

}


Actually we are not interested in ASCII codes, but I put that just in case some non-displayable character was entered.

Try it, play with it see what it does if you just press [enter] or if you type a space and press enter.

The important thing to realise is that the input from the keyboard is buffered. When you press a key, it is stored in an area of memory until some command such as cin.get() retrieves it.

The putback() function is useful because it allows us to change our mind. We read a character, take a look at it, decide what to do with it. Then we can put the character back into the buffer as though we never read it in the first place. After we have put it back, then we can use some other command such as
1
2
int n;    // define an integer variable
std::cin >> n; // read an integer from the input buffer 


see the reference page:
http://www.cplusplus.com/reference/istream/istream/get/
http://www.cplusplus.com/reference/istream/istream/putback/

can someone please explain me
...
and how to take input more than one digit number and decimal numbers if we input one char at a time???

This is very much the reason why the putback() function is used.

Reading numbers one digit at a time is not much fun - it can be done, but there's no need. As soon as we identify that the current character is a digit '0' to '9', then we can stop (at least temporarily) reading one char at a time, and read the entire number in one go.

In fact there's an example of this in the sample code on the reference page:
http://www.cplusplus.com/reference/istream/istream/putback/

Look at the code, where it says,
 
    std::cout << "Please, enter a number or a word: ";

the program does what I've been trying to describe. You might test this example for yourself to see it in action.
Topic archived. No new replies allowed.