help with a c++ calculator

A

ali.sobh

Hi,
i am new in c++ and i need some help with a program
i need to do a universal unit converter. this calculator can only do :
+, *, /, and -.
it should read a string (the equation) and ask you to put the value of
that string. i wasn't able to do that, but i could do with
characters...
for ex :
if i write: CelsiusTemp * ConversionFactor + base....it should ask me
to input the value of celsiustemp and ...
i was only able to do a+b/d+c and input the value of each one.
and it still didn't work well.
TY for help and any advice!
*******************************************
the file:
#include <iostream>
#include <string>

using namespace::std;
using std::string;


int main()
{ string s;
//char *Exp;
int state = 0; //initial state
int site = 0;
int var1 = 0, var2 = 0, operation = 0;
char ch;



cout<<"Enter the equation you want to use: ";
getline(cin, s);


do{
for(int i=0;i<=s.size();i++)
{
ch=s[site];
site++;
if(ch==' '){
if(state==0)//looking for a variable
{
if(ch>='a' && ch<='z')//reading first character
{
cout<<"Enter value of "<<ch<<": ";
cin>>var1;
state=1;
}
else
{
cout<<"Error: No variable."<<endl;
state=-1;
}
}//end state 0
else if(state==1) // reading first operation:
{
if(ch=='*')
{
operation=1;
state=2;
}
else if(ch=='/')
{
operation=2;
state=2;
}
else if(ch=='-')
{
operation=3;
state=2;
}
else if(ch=='+')
{
operation=4;
state=2;
}
else
{
cout<<"Error: Unknown
Operation."<<endl;
state=-1;
}
}//end reading first operation--End of state 1
else if(state==2)//reading second variable
{
if(ch>='a' && ch<='z')//reading 2nd character
{
cout<<"Enter value of "<<ch<<": ";
cin>>var2;
state=3;
}
else
{
cout<<"Error: No variable."<<endl;
state=-1;
}
}//end of state 2: reading second variable
else if(state==3)
{
if(operation==1) //*
{
var1=var1*var2;

}
else if(operation==2)//(/)
{
var1=var1/var2;
}
else if(operation==3)//-
{
var1=var1-var2;
}
else if(operation==4)//(+)
{
var1=var1+var2;
}
else
{
cout<<"Error!"<<endl;
state=-1;
}
cout<<"The result so far is: "<<var1<<endl;


state=2;

}//end of state 3


else //end of string
{
if(state==0)
{
state=2;
//var1=var1++;
cout<<"FINAL RESULT: "<<var1<<endl;

}
else
{
return -1;
}


} //end
}
}//end of for loop


}while(ch!=0 && state!=-1);



}
 
A

ali.sobh

SORRY THE RIGHT PROGRAM IS THAT ONE:
I FORGOT TO ERASE IF(CH!=' ') BECAUSE IT SHOULD IGNORE SPACES OR BLANK
INLCUDED IN THE EQUATION
 
O

osmium

i am new in c++ and i need some help with a program
i need to do a universal unit converter. this calculator can only do :
+, *, /, and -.
it should read a string (the equation) and ask you to put the value of
that string. i wasn't able to do that, but i could do with
characters...
for ex :
if i write: CelsiusTemp * ConversionFactor + base....it should ask me
to input the value of celsiustemp and ...
i was only able to do a+b/d+c and input the value of each one.
and it still didn't work well.
TY for help and any advice!
*******************************************
the file:
#include <iostream>
#include <string>

using namespace::std;
using std::string;


int main()
{ string s;
//char *Exp;
int state = 0; //initial state
int site = 0;
int var1 = 0, var2 = 0, operation = 0;
char ch;



cout<<"Enter the equation you want to use: ";
getline(cin, s);


do{
for(int i=0;i<=s.size();i++)
{
ch=s[site];
site++;
if(ch==' '){
if(state==0)//looking for a variable
{
if(ch>='a' && ch<='z')//reading first character

The only way I can get here is if ch == ' '. Why ask questions to which the
answers are already known?
-----
I can't figure out what your goal is, but I think you might be trying to do
something pretty difficult.
Should the word "celsisus" be an acceptable input to your completed program?
If so, it's pretty hard, and you should set your sights a little lower..

I suggest you learn about the switch statement and also about functions.
Functions give you the opportunity to modularize your code so an ordinary
mortal could see what the intent of the code is.
 
J

Juha Nieminen

i need to do a universal unit converter. this calculator can only do :
+, *, /, and -.

If those operators should follow the regular order of precedence
(eg. in "a+b*c" the "b*c" is calculated first) then you can't do it
iteratively. You need a stack and recursive functions (the stack
plus an non-recursive function may be possible, but very complicated).
And parsing such a string is not one of the simplest tasks.

The question would be: Why do you need to do such a universal
converter? If it's just for learning C++ then you might want to start
with something simpler. If it's for a class assignment, I can't
believe they would give such a hard task at your level. Perhaps they
meant something simpler? If it's for hobby or necessity, maybe it
would be a better idea to use an existing library for this purpose?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top