'Program to evaluate postfix expressions but there is a wierd problem with this program-it does not gives output and instead gives a long error
I was writing a code for postfix expression evaluation and encountered a weird error. it shows a big error which is really difficult for me to understand what is wrong in the code.
it would be really helpful if you please have a look and tell me what's wrong with it or what mistake I have made.
I have listed the code below to have a look.
thank you in advance
here is the code :
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int evaluate(string expression);
bool Isdigit(char c);
bool IsOprator(char c);
int performcalc(char opration,int op2,int op1);
int main(){
string expression;
cout<<"Start of the program ! "<<endl;
cout<<"Enter a postfix expression to evaluate: ";
cin>>expression;
int result = evaluate(expression);
cout<<"Result ="<<result<<endl;
cout<<"End of the program !!";
return 0;
}
int evaluate(string expression){
stack <char> S;
for (int i = 0; i < expression.length(); i++)
{
if (expression[i] == ' ' || expression[i] == ',')
{
continue;
}
else if (Isdigit(expression[i]))
{
int a = stoi(expression[i]);
S.push(a);
}
else if (IsOprator(expression[i]))
{
int op2 = S.top(); S.pop();
int op1 = S.top(); S.pop();
int result = performcalc(expression[i],op2,op1);
S.push(result);
}
}
return S.top();
}
bool Isdigit(char c){
if (c >= '0' && c<='9')
{
return true;
}
else
{
return false;
}
}
bool IsOprator(char c){
if (c == '+' || c == '*' || c == '/' || c == '-' )
{
return true;
}
else
{
return false;
}
}
int performcalc(char opration,int op2,int op1){
if (opration == '+')
{
return op1+op2;
}
else if (opration == '-')
{
return op1-op2;
}
else if (opration == '*')
{
return op1*op2;
}
else if (opration == '/')
{
return op1/op2;
}
return -1;
}
Error :
Postfix_evaluation_using_stack.cpp: In function 'int evaluate(std::string)':
Postfix_evaluation_using_stack.cpp:37:39: error: no matching function for call to 'stoi(__gnu_cxx::__alloc_traits<std::allocator<char>, ch
ar>::value_type&)'
37 | int a = stoi(expression[i]);
| ^
In file included from c:\ming\mingw\include\c++\9.2.0\string:55,
from c:\ming\mingw\include\c++\9.2.0\bits\locale_classes.h:40,
from c:\ming\mingw\include\c++\9.2.0\bits\ios_base.h:41,
from c:\ming\mingw\include\c++\9.2.0\ios:42,
from c:\ming\mingw\include\c++\9.2.0\ostream:38,
from c:\ming\mingw\include\c++\9.2.0\iostream:39,
from Postfix_evaluation_using_stack.cpp:1:
c:\ming\mingw\include\c++\9.2.0\bits\basic_string.h:6503:3: note: candidate: 'int std::__cxx11::stoi(const string&, std::size_t*, int)'
6503 | stoi(const string& __str, size_t* __idx = 0, int __base = 10)
| ^~~~
c:\ming\mingw\include\c++\9.2.0\bits\basic_string.h:6503:22: note: no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<st
d::allocator<char>, char>::value_type' {aka 'char'} to 'const string&' {aka 'const std::__cxx11::basic_string<char>&'}
6503 | stoi(const string& __str, size_t* __idx = 0, int __base = 10)
| ~~~~~~~~~~~~~~^~~~~
c:\ming\mingw\include\c++\9.2.0\bits\basic_string.h:6609:3: note: candidate: 'int std::__cxx11::stoi(const wstring&, std::size_t*, int)'
6609 | stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
| ^~~~
c:\ming\mingw\include\c++\9.2.0\bits\basic_string.h:6609:23: note: no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<st
d::allocator<char>, char>::value_type' {aka 'char'} to 'const wstring&' {aka 'const std::__cxx11::basic_string<wchar_t>&'}
6609 | stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
