Hello, I am trying to make a calculator that does multiplication, division, addition and subtraction. My problem is that when I have the two numbers that have decimals it goes wonky. Anyway, that program runs like this:
1. User enters equation
2. Unwanted characters get thrown out
3. If operation detected, send the equation string and the operation character to a function that does this:
1. Finds how many characters to the left the number is (how long)
2. Does the same for the right side
3. Does the operation accordingly by converting the numbers to double, adding (or whatever it is doing) the numbers, and converting back. The problem is converting back to string.


here is the bad code (this part is the same for every operation except what happens to the numbers):

double add1;
            double add2;
            double ans;
            add1 = atof (num1);
            add2 = atof (num2);
            ans = add1 + add2;
            itoa(ans, bufadd, 10);

num1 is the number on the left of the operator. It is a string
num2 is for the left side
bufadd is where the answer is converted back to a string. later, it gets injected back into the actual equation, all spaces get filled up, and the search for another operator repeats.

Is there any easy fix?

Thanks,
Mitch

Recommended Answers

All 5 Replies

You have a wrong concept IMO.
When you input a number, convert it to double. Keep everything as a double. When you output the answer, just output the double. You want to stay away from strings as much as possible after the initial input.

Also, any idea what the i means in itoa()? Which, by the way, is not a standard function.

You have a wrong concept IMO.
When you input a number, convert it to double. Keep everything as a double. When you output the answer, just output the double. You want to stay away from strings as much as possible after the initial input.

Also, any idea what the i means in itoa()? Which, by the way, is not a standard function.

the i in itoa means integer I think...

also, what if the equation has more than one operation? like:
7+5*9

Mitch

the i in itoa means integer I think...

And what type of values are you using?

also, what if the equation has more than one operation? like:
7+5*9

If your math teacher gave you the exact same equation, what would you do? That's what you teach the computer to do, also.

Hello, I am trying to make a calculator that does multiplication, division, addition and subtraction. My problem is that when I have the two numbers that have decimals it goes wonky. Anyway, that program runs like this:
1. User enters equation
2. Unwanted characters get thrown out
3. If operation detected, send the equation string and the operation character to a function that does this:
1. Finds how many characters to the left the number is (how long)
2. Does the same for the right side
3. Does the operation accordingly by converting the numbers to double, adding (or whatever it is doing) the numbers, and converting back. The problem is converting back to string.


here is the bad code (this part is the same for every operation except what happens to the numbers):

double add1;
            double add2;
            double ans;
            add1 = atof (num1);
            add2 = atof (num2);
            ans = add1 + add2;
            itoa(ans, bufadd, 10);

num1 is the number on the left of the operator. It is a string
num2 is for the left side
bufadd is where the answer is converted back to a string. later, it gets injected back into the actual equation, all spaces get filled up, and the search for another operator repeats.

Is there any easy fix?

Thanks,
Mitch

the easiest way to convert from a double to a string is using stringstreams...example below:

#include <iostream>
#include <sstream>

using namespace std;

int main(void) {
  double someDouble = 1.23456;
  stringstream output;
  output << someDouble;
  string finalString = output.str();
  cout << finalString << endl;
}

The above would yield 1.23456 as a string, converted from a double

above layer is right ! study

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.