Not Yet Answered # C++ calculator problem

Discussion Starter thecoolman5 8 L7Sqr 227 Discussion Starter thecoolman5 8 L7Sqr 227 Discussion Starter thecoolman5 8 StuXYZ 731 Discussion Starter thecoolman5 8 Discussion Starter thecoolman5 8 Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...

Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...

0

hi, I am making a calculator and i need a function that can detect a char in a string. In other words, i need it to detect a NON-number.

```
string n1 = "35+66/7";
int pos = -1;
n1.find(char, pos+1);
cout << char.n1_find << " was found at "<< pos << endl;
```

I already know how to use the find function.

`n1.find("+", pos+1);`

but that wont work in my calculator. Any solutions?

*Edited 5 Years Ago by thecoolman5*: n/a

0

ok guys, i used find_first_not_of and the program works great. But there is only one problem. When i enter a digit larger than 9 and then add one, the answer doesnt come out right. I have reviewed it over and over and i cant see where im going wrong. Maybe you guys will have more luck than me.

```
#include <iostream>
#include <string>
#include<sstream>
using namespace std;
int main ()
{
string str ("1+10+1");
size_t found;
int n2, n11;
int l = 1, l1 = 1;
for(;;)
{
re:
found=str.find_first_not_of("12345678910");
if (int(found) != -1)
{
stringstream(str) >> n2;
str = str.substr(int(found+1));
if(l == 1)
{
n11 = n2;
l = 0;
goto re;
}
if(str[found] != '+')
{
re1:
n11 = n11 + n2;
}
}
else
{
stringstream(str) >> n2;
if(l1 == 1)
{
l1 = 0;
goto re1;
}
cout << "This is the finished number "<< n11 << endl;
system("pause");
break;
}
}
}
```

0

Is there a reason you are using `goto`

? It makes things much more complicated when trying to follow your logic. Also, and this is a personal preference thing, variable names such as l1, ll, nl1, nll and so on do not communicate much in the way of intent.

Here is an example that removes the `goto`

from your example and simplifies the logic a bit

```
#include <iostream>
#include <string>
#include <sstream>
int main ()
{
std::string equation ("1+10+1"), saved = equation;
long running_sum = 0;
while (true) {
size_t non_digit = equation.find_first_not_of("0123456789");
std::string part = equation.substr (0,non_digit);
long value = 0;
std::stringstream(part) >> value;
running_sum += value;
if (non_digit == std::string::npos)
break;
equation = equation.substr(non_digit + 1);
}
std::cout << "Input : " << saved << std::endl;
std::cout << "Result : " << running_sum << std::endl;
}
```

There is obviously plenty of error checking and other parts removed from the above but it will get you started on a non-goto approach at least.

0

ok, i just got rid of the first goto and it works great. Now, i cant figure out how to get the mathematical operations to work. I tried this:

`if(str[found] == '+')`

but that doesnt work. How would you do it?

0

How would you do it?

I'd start by removing *all* the `goto`

s. From there I would look at the Shunting-Yard algorithm.

0

ok, i got rid of all the gotos. I figured out how to do the mathematical operations. Its just string operators = str[found]; And the only problem is the program wont loop right anymore. Heres is the code posted by L7Sqr with the mathematical operations:

```
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
std::string equation ("10+5-15+7-5-3"), saved = equation;
long running_sum = 0;
string operators = "";
while (true) {
size_t non_digit = equation.find_first_not_of("0123456789");
std::string part = equation.substr (0,non_digit);
long value = 0;
std::stringstream(part) >> value;
operators = equation[non_digit];
if(operators == "+")
{
running_sum += value;
}
if(operators == "-")
{
running_sum -= value;
}
if(operators == "*")
{
running_sum *= value;
}
if(operators == "/")
{
running_sum /= value;
}
if (non_digit == std::string::npos)
break;
equation = equation.substr(non_digit + 1);
}
std::cout << "Input : " << saved << std::endl;
std::cout << "Result : " << running_sum << std::endl;
system("pause");
return 0;
}
```

Do you guys know whats wrong?

0

The problem is that you are passing the string in the wrong order. Consider just

the string "10-5". What you are doing is finding the first non-digit and setting that

to operator so you loop through doing `running_sum-=10`

Then you ignore the last number:

What you need is a quick read of the first number and put it into the running_number,

and then find the operator etc

0

What you need is a quick read of the first number and put it into the running_number,

and then find the operator etc

Thats where i get stuck. I have tried over and over and i cant find the right code.

0

here guys. I finally finished a working calculator thats works with paranthesis, order of operations, and unlimited operators.

```
#include<iostream>
#include<string>
#include<sstream>
#include<windows.h>
#include<conio.h>
#include<ctime>
using namespace std;
int main ()
{
cout << "No variables." << endl;
cout << "Type in cls to clear the screen, and type exit to exit." << endl;
SetConsoleTitle("Calculator");
for(;;)
{
re:
string equation = "";
string equation2 = "";
string equation3 = "";
string equation4 = "";
string equation5 = "";
int non_digit5 = 0;
int non_digit2 = -1;
int non_digit6 = 0;
int c = 0;
int i = 0;
float n1 = 0;
string operators = "";
float n2 = 0;
cin >> equation;
DWORD ticks = GetTickCount();
if(equation == "exit")
{
return 0;
}
if(equation == "cls")
{
system("cls");
goto re;
}
while(true)
{
c = equation.find('(', c);
if(c == -1)
{
break;
}
//PARENTHESIS SOLVING CODE
do
{
non_digit2 = equation.find('(', non_digit2+1);
if(non_digit2 == -1)
{
break;
}
non_digit5 = non_digit2;
}while(non_digit2 != -1);
non_digit5 = non_digit5 + 1;
int non_digit3 = 0;
//DOUBLE PARANTHESIS EQUATIONS
equation4 = equation.substr(non_digit5, 999999999);
int l = equation.length();
int l1 = equation4.length();
non_digit3 = equation4.find(")", non_digit3+1);
l1 = l1 - non_digit3;
l = l - l1;
non_digit3 = l;
equation3 = equation.substr(non_digit3+1, 999999999);
equation2 = equation.substr(non_digit5, non_digit3);
do
{
non_digit2 = equation2.find(')', non_digit2+1);
if(non_digit2 == -1)
{
break;
}
non_digit6 = non_digit2;
}while(non_digit2 != -1);
equation2 = equation2.substr(0, non_digit6);
equation = equation.substr(0, non_digit5-1);
n1 = 0;
operators = "";
n2 = 0;
int non_digit = equation2.find_first_not_of("12345677890.");
string part = equation2.substr(0, non_digit);
stringstream(part) >> n1;
operators = equation2[non_digit];
equation2 = equation2.substr(non_digit + 1);
while(true)
{
n2 = 0;
non_digit = equation2.find_first_not_of("1234567890.");
part = equation2.substr (0, non_digit);
stringstream(part) >> n2;
if(int(non_digit) == 0)
{
string part = equation2.substr (0, non_digit+3);
stringstream(part) >> n2;
equation2 = equation2.substr(non_digit + 1);
}
if(operators == "+")
{
n1 = n1 + n2;
}
if(operators == "-")
{
n1 = n1 - n2;
}
if(operators == "*")
{
n1 = n1 * n2;
}
if(operators == "/")
{
n1 = n1 / n2;
}
if(non_digit == -1)
{
break;
}
non_digit = equation2.find_first_not_of("0123456789.");
operators = equation2[non_digit];
equation2 = equation2.substr(non_digit + 1);
}
stringstream out;
out << n1;
equation = equation + out.str();
equation = equation + equation3;
}
//ORDER OF OPERATIONS
while(true)
{
c = equation.find_first_not_of("1234567890.-+");
if(c == -1)
{
break;
}
equation2 = "";
equation3 = "";
equation4 = "";
operators = "";
float n1 = 0;
float n2 = 0;
float n3 = 0;
int non_digit = equation.find_first_not_of("1234567890.-+");
operators = equation[non_digit];
equation2 = equation.substr(non_digit+1, 999999999);
stringstream(equation2) >> n1;
equation3 = equation.substr(0,non_digit);
reverse(equation3.begin(), equation3.end());
//BEGIN FIXING REVERSED EQUATION
non_digit = equation3.find_first_not_of("1234567890.");
if(non_digit == -1)
{
reverse(equation3.begin(), equation3.end());
stringstream(equation3) >> n2;
}
else
{
equation5 = equation3.substr(0, non_digit);
reverse(equation5.begin(), equation5.end());
stringstream(equation5) >> n2;
}
stringstream out;
out << n2;
equation4 = equation4 + out.str();
equation4 = equation4 + operators;
stringstream out1;
out1 << n1;
equation4 = equation4 + out1.str();
//END OF FIND OPERATOR
int l = equation4.length();
non_digit = equation.find(equation4);
equation2 = equation.substr(0, non_digit);
equation4 = equation.substr(non_digit+l, 999999999);
if(operators == "/")
{
n3 = n2 / n1;
}
if(operators == "*")
{
n3 = n2 * n1;
}
stringstream out2;
out2 << n3;
equation2 = equation2 + out2.str();
equation2 = equation2 + equation4;
equation = equation2;
}
//START FIXED EQUATION SOLVING
n1 = 0;
operators = "";
n2 = 0;
size_t non_digit = equation.find_first_not_of("12345677890.");
string part = equation.substr (0, non_digit);
stringstream(part) >> n1;
operators = equation[non_digit];
equation = equation.substr(non_digit + 1);
while (true)
{
n2 = 0;
size_t non_digit = equation.find_first_not_of("1234567890.");
string part = equation.substr (0, non_digit);
stringstream(part) >> n2;
if(int(non_digit) == 0)
{
string part = equation.substr (0, non_digit+3);
stringstream(part) >> n2;
equation = equation.substr(non_digit + 1);
}
if(operators == "+")
{
n1 = n1 + n2;
}
if(operators == "-")
{
n1 = n1 - n2;
}
if(operators == "*")
{
n1 = n1 * n2;
}
if(operators == "/")
{
n1 = n1 / n2;
}
if (int(non_digit) == -1)
{
break;
}
non_digit = equation.find_first_not_of("0123456789.");
operators = equation[non_digit];
equation = equation.substr(non_digit + 1);
}
cout << "Speed of calculation: " << GetTickCount() - ticks << " milliseconds." << endl;
cout << "Result : " << n1 << endl;
system("pause");
goto re;
}
}
```

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

I don’t want at this stage work on a big separate project as I've already got plenty ...