C++ Programming - stack based calculator program
Can anyone see what I've done incorrectly with this program. You should be able to enter a value, press return, enter then next value, press return, enter an operator, press return and you'd get the answer, then you can just continue but would need to Ctrl+Z to exit.
[code]
#include <list>
#include <stack>
#include <string>
#include <iostream>
using namespace std;
void main (void)
{
cout << "enter value or operand" << endl;
stack <string> stk;
const int BUFLAN = 80;
char szbuf[BUFLEN+2] = {0};
for (;;)
{
string s;
cin >> s;
if (s== "+")
{
string op1 = stk.top();
stk.pop();
string op2 = stk.top();
stk.pop();
double result = atof(op1.c_str()) + atof(op2.c_str());
sprint (szbuf, "%f", result);
s=szbuf;
stk.push(s);
cout << "result: " << s << endl;
}
else if (s== "-")
{
string op1 = stk.top();
stk.pop();
string op2 = stk.top();
stk.pop();
double result = atof(op1.c_str()) - atof(op2.c_str());
sprint (szbuf, "%f", result);
s=szbuf;
stk.push(s);
cout << "result: " << s << endl;
}
else if (s== "*")
{
string op1 = stk.top();
stk.pop();
string op2 = stk.top();
stk.pop();
double result = atof(op1.c_str()) * atof(op2.c_str());
sprint (szbuf, "%f", result);
s=szbuf;
stk.push(s);
cout << "result: " << s << endl;
}
else if (s== "/ ")
{
string op1 = stk.top();
stk.pop();
string op2 = stk.top();
stk.pop();
double result = atof(op1.c_str()) / atof(op2.c_str());
sprint (szbuf, "%f", result);
s=szbuf;
stk.push(s);
cout << "result: " << s << endl;
}
else
{
stk.push(s);
}
}
}
[/code/
Re: C++ Programming - stack based calculator program
Quote:
Originally posted by tinkerbelle
[/code/
can you fix that up?
Still need help with this
I corrected my typos, sorry, but now I'm getting a linking error LNK2001, _WinMain@16: Unresolved External Symbol. I looked it up, they say to go into the Output category of the Link field in the Project Setting dialog box, set the Entry Point symbol to wWinMainCRTStartup. I did this and it still gives me the same linking error....anybody seen this before?
C++ Programming - stack based calculator program
Can anyone see what I've done incorrectly with this program. You should be able to enter a value, press return, enter then next value, press return, enter an operator, press return and you'd get the answer, then you can just continue but would need to Ctrl+Z to exit.
Code:
#include <list>
#include <stack>
#include <string>
#include <iostream>
using namespace std;
void main (void)
{
cout << "enter value or operand" << endl;
stack <string> stk;
const int BUFLEN = 80;
char szbuf[BUFLEN+2] = {0};
for (;;)
{
string s;
cin >> s;
if (s== "+")
{
string op1 = stk.top();
stk.pop();
string op2 = stk.top();
stk.pop();
double result = atof(op1.c_str()) + atof(op2.c_str());
sprint (szbuf, "%f", result);
s=szbuf;
stk.push(s);
cout << "result: " << s << endl;
}
else if (s== "-")
{
string op1 = stk.top();
stk.pop();
string op2 = stk.top();
stk.pop();
double result = atof(op1.c_str()) - atof(op2.c_str());
sprint (szbuf, "%f", result);
s=szbuf;
stk.push(s);
cout << "result: " << s << endl;
}
else if (s== "*")
{
string op1 = stk.top();
stk.pop();
string op2 = stk.top();
stk.pop();
double result = atof(op1.c_str()) * atof(op2.c_str());
sprint (szbuf, "%f", result);
s=szbuf;
stk.push(s);
cout << "result: " << s << endl;
}
else if (s== "/ ")
{
string op1 = stk.top();
stk.pop();
string op2 = stk.top();
stk.pop();
double result = atof(op1.c_str()) / atof(op2.c_str());
sprint (szbuf, "%f", result);
s=szbuf;
stk.push(s);
cout << "result: " << s << endl;
}
else
{
stk.push(s);
}
}
}
Re: Still need help with this
Quote:
Originally posted by tinkerbelle
I corrected my typos, sorry, but now I'm getting a linking error LNK2001, _WinMain@16: Unresolved External Symbol.
You need to change the target from a Win32 GUI app to a Win32 console application.